-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproblem10.py
85 lines (71 loc) · 2.1 KB
/
problem10.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""
ADVENT OF CODE 2021
Contestant: Kevin Wood
Problem: 10
"""
import argparse
from math import floor
from typing import List, Optional
OPENERS = "([{<"
CLOSERS = ")]}>"
SCORES = [3, 57, 1197, 25137]
def solve_part1(lines: List[str]) -> int:
score = 0
for line in lines:
state = []
for c in line:
if c in OPENERS:
i = OPENERS.index(c)
state.append(i)
if c in CLOSERS:
i = CLOSERS.index(c)
z = state.pop()
if i != z:
score += SCORES[i]
break
return score
def solve_part2(lines: List[str]) -> int:
scores = []
for line in lines:
completion = find_completion(line)
if completion:
score = 0
for c in completion:
score *= 5
score += CLOSERS.index(c) + 1
scores.append(score)
scores.sort()
return scores[floor(len(scores) / 2)]
def find_completion(line: str) -> Optional[str]:
state = []
for c in line:
if c in OPENERS:
state.append(c)
if c in CLOSERS:
z = state.pop()
if CLOSERS.index(c) != OPENERS.index(z):
return None
if state:
state.reverse()
result = ""
for c in state:
result += CLOSERS[OPENERS.index(c)]
return result
return None
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Advent of Code 2021, problem 10")
parser.add_argument(
"-i", "--input_file", help="path to input file", default="input/problem10.txt"
)
parser.add_argument("part", help="part (1|2)", type=int)
args = parser.parse_args()
with open(args.input_file, "r") as file:
lines = [line.strip() for line in file.readlines()]
filtered_lines = list(filter(lambda line: bool(line), lines))
if args.part == 1:
output = solve_part1(filtered_lines)
elif args.part == 2:
output = solve_part2(filtered_lines)
else:
raise ValueError("Unknown part")
print(output)