-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathday1.py
53 lines (39 loc) · 1.36 KB
/
day1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import pytest
@pytest.mark.parametrize('data, expected',
[
('(())', 0),
('()()', 0),
('(((', 3),
('(()(()(', 3),
('))(((((', 3),
('())', -1),
('))(', -1),
(')))', -3),
(')())())', -3),
])
def test_part1(data: str, expected: int):
assert part1(data) == expected
@pytest.mark.parametrize('data, expected',
[
(')', 1),
('()())', 5),
])
def test_part2(data: str, expected: int):
assert part2(data) == expected
def parse_input(filename: str) -> str:
return open(filename).read().strip()
def part1(directions: str) -> int:
return directions.count('(') - directions.count(')')
def part2(directions: str) -> int:
floor = 0
for i, ch in enumerate(directions):
floor += 1 if ch == '(' else -1
if floor == -1:
return i + 1
return -1
def main():
directions = parse_input('input/day1.txt')
print(f'Part 1: {part2(directions)}')
print(f'Part 2: {part2(directions)}')
if __name__ == "__main__":
main()