-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.py
68 lines (62 loc) · 2.04 KB
/
day10.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
import statistics
def part1():
with open('resources/input10.txt') as f:
lines = f.readlines()
total = 0
for line in lines:
chunk = []
for char in line.strip():
if char == '[':
chunk.append(']')
elif char == '(':
chunk.append(')')
elif char == '<':
chunk.append('>')
elif char == '{':
chunk.append('}')
elif char != chunk.pop():
if char == ')':
total += 3
elif char == ']':
total += 57
elif char == '}':
total += 1197
elif char == '>':
total += 25137
print(total)
def part2():
with open('resources/input10.txt') as f:
lines = f.readlines()
totals = list()
for line in lines:
chunk = []
total = 0
for char in line:
if char == '[':
chunk.append(']')
elif char == '(':
chunk.append(')')
elif char == '<':
chunk.append('>')
elif char == '{':
chunk.append('}')
elif char == '\n':
while len(chunk) != 0:
total = total * 5
value = chunk.pop()
if value == ']':
total += 2
elif value == ')':
total += 1
elif value == '>':
total += 4
elif value == '}':
total += 3
totals.append(total)
elif char != chunk.pop():
# corrupted line
break
print(statistics.median(totals))
if __name__ == '__main__':
part1()
part2()