-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaoc201501.py
38 lines (26 loc) · 861 Bytes
/
aoc201501.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
"""AoC 1, 2015: Not Quite Lisp."""
# Standard library imports
import itertools
import pathlib
import sys
STEPS = {"(": 1, ")": -1}
def parse_data(puzzle_input):
"""Parse input."""
return [STEPS[paren] for paren in puzzle_input]
def part1(data):
"""Solve part 1."""
return sum(data)
def part2(data):
"""Solve part 2."""
steps = enumerate(itertools.accumulate(data), start=1)
return next(itertools.dropwhile(lambda d: d[1] >= 0, steps))[0]
def solve(puzzle_input):
"""Solve the puzzle for the given input."""
data = parse_data(puzzle_input)
yield part1(data)
yield part2(data)
if __name__ == "__main__":
for path in sys.argv[1:]:
print(f"\n{path}:")
solutions = solve(puzzle_input=pathlib.Path(path).read_text().strip())
print("\n".join(str(solution) for solution in solutions))