-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday10.py
executable file
·116 lines (95 loc) · 1.7 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
from utils.all import *
advent.setup(2021, 10)
if 'debug' not in map(str.lower, sys.argv):
fin = advent.get_input()
else:
fin = io.StringIO('''\
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]
''')
eprint(*fin, sep='', end='----- end of input -----\n\n'); fin.seek(0, 0)
timer_start()
ans = 0
try: ints = read_ints(fin); fin.seek(0, 0)
except: pass
try: lines = get_lines(fin); fin.seek(0, 0)
except: pass
try: mat = read_char_matrix(fin); fin.seek(0, 0)
except: pass
scores = {
')': 3,
']': 57,
'}': 1197,
'>': 25137,
}
scores2 = {
')': 1,
']': 2,
'}': 3,
'>': 4,
}
revmap = {
'(': ')',
'[': ']',
'{': '}',
'<': '>',
}
def check(s):
stack = deque()
for c in s:
if c in '([{<':
stack.append(revmap[c])
else:
x = stack.pop()
if x != c:
return scores[c]
return 0
ans = 0
for s in lines:
s = s.strip()
ans += check(s)
advent.print_answer(1, ans)
# wait('Submit? ')
# advent.submit_answer(1, ans)
def check2(s):
stack = deque()
for c in s:
if c in '([{<':
stack.append(revmap[c])
else:
x = stack.pop()
if x != c:
return 0 # bug, not incomplete
score = 0
# if stack:
# print(stack)
while stack:
c = stack.pop()
# print(c, scores2[c])
score *= 5
score += scores2[c]
return score
scores = []
for s in lines:
s = s.strip()
ss = check2(s)
# print(ss)
if ss > 0:
scores.append(ss)
# if ss > 0:
# print(s)
scores.sort()
# print(scores)
ans = scores[len(scores)//2]
advent.print_answer(2, ans)
wait('Submit? ')
advent.submit_answer(2, ans)