-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10.py
executable file
·199 lines (183 loc) · 11 KB
/
10.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/bin/env python3
# INPUT
def parser(text) -> list:
return text.strip().splitlines()
def read_input() -> str:
with open(__file__, encoding="utf-8") as f:
c = f.read()
data = c[c.rindex("🎅") + 1: c.rindex("⛄")].strip()
return data
# MAIN
close_to_open = {
")": "(",
"]": "[",
"}": "{",
">": "<"
}
open_to_close = {v: k for k, v in close_to_open.items()}
def part1(lines: list) -> tuple:
scores = {
")": 3,
"]": 57,
"}": 1197,
">": 25137
}
ans = 0
incomplete = []
for mes in lines:
stack = []
valid = True
for br in mes:
if br in close_to_open:
if stack and close_to_open[br] == stack[-1]:
stack.pop()
else:
# invalid bracket
valid = False
ans += scores[br]
break
else:
stack.append(br)
if valid and stack:
incomplete.append(stack)
return ans, incomplete
def part2(incomplete_lines: list) -> int:
values = {
")": 1,
"]": 2,
"}": 3,
">": 4
}
scores = []
for stack in incomplete_lines:
ans = 0
while stack:
last = stack.pop()
ans = ans * 5 + values[open_to_close[last]]
scores.append(ans)
return sorted(scores)[len(scores) // 2]
# TEST
def test():
# GIVEN
sample_input = """
[({(<(())[]>[[{[]{<()<>>
[(()[<>])]({[<{<<[]>>(
{([(<{}[<>[]}>{[]{[(<()>
(((({<>}<{<{<>}{[]{[]{}
[[<[([]))<([[{}[[()]]]
[{[{({}]{}}([{[{{{}}([]
{<[[]]>}<{[{[{[]{()[[[]
[<(<(<(<{}))><([]([]()
<{([([[(<>()){}]>(<<{{
<{([{{}}[<[[[<>{}]]]>[]]
"""
given = parser(sample_input)
score1, given2 = part1(given)
assert score1 == 26397
# part 2
assert part2(given2) == 288957
return True
if __name__ == '__main__':
assert test()
input_ = parser(read_input())
# ONE #1
part_1, incomplete = part1(input_)
print(part_1)
assert part_1 == 193275
# TWO #2
part_2 = part2(incomplete)
print(part_2)
assert part_2 == 2429644557
# INPUT
"""🎅
({{[<{[{[[<([[<><>][{}()]]<([]{})[<>()]>)><<<([]())>>{([()<>][{}{}])<[<>()]{()()}>}>]<(<[[[]<>]({}{})]{{{}<>}
[(<<<[[[{({(([{}{}]<()<>>)[<()<>>[[]<>]])}([{{<>()}}]))}<[{{[<[]()>][[<>()][[][]]]}{{(()()){{}{})}}
<(<(<<{<(({(<[()()]<<>[]>>((()[])(<><>)))}))[{<{<[(){}]>}><[<{{}[]}{[][]}>(<{}{}>)]([(<><>)(<>{})]{{(){}}[<>(
[{<[<{[[[<{<(<[]<>><<><>>)<{{}()}>>{[<()>{[]<>}]}}>[(<{[{}()]<<>()>}{(<>[])({}<>)}>)]]}{({<
[<[(<<<([[[<(<<>{}><()>)(<[]<>>([][]))>(<{{}<>}{{}[]}>)]](<(<({}){<>()}>{<{}<>>})[{[{}<>][<
[([(<([[[([([<[]()>[()()]]{{<>{}}([][])})<{<[][]>{()[])}[{<>{}}<<>()>]>][<[<{}<>>[[]{}]](<()
{({{<{[[(<[[{[()()][()[]]}<{<>[]}<{}()>>]({<<>>}(({}{})(<>)))]<([[()[]]<(){}>]{{{}<>}([]<>)})([{[][]}{<>
<(<{{([[<<({[<(){}>[()<>]]{[{}<>]<(){}>}}(<[[]{}]<[][]>>))<<[{{}()}][{<>[]}[{}<>]]>[([<>{}]{[]{}})
[<{<((([{<(<(<{}>[{}()])>)<{[{<>()}[{}[]]][{[]}<{}()}]}((<{}>{[]<>})({()()}<[]{}>))>>}])))
<{(<{((([((({({}<>)([]<>)}({()<>}[{}<>])){(<[][]><()[]>)<[{}[]]<[][]>>})[[{{<>{}}(<><>)}{[()()][()[]]}]])[[{
{<[<[[[(({(<<{[][]}(<>{})>{<[]()><()()>}>(({[]<>}{()<>}){[()()]}))})<({{<(<>{})>[(<>[]){()
([[[{{[<([<<({[]{}}{(){}})(<[]<>><[]<>>)>{{{[]}{[]{}}}}><[[[<><>]{{}[]}]]<<[{}{}][(){}]>{<()[]>[{}[]]}>>]
{[[[<<({([<<(((){})<{}()>)[({}[])<()[]>]><<[[]{}]<<>()>>{(<>())}>](<<{[]{}}[<>()]>>[{({}<>)[()[]]}
[<{{<({<<[(<<<{}<>>{[]{}}><((){})((){})>>(<[[]<>][(){}]>[<{}()>({}<>)]])]<[<{[[]()]({}[])}>(<
<{(({{(({({<[<{}<>><[][]>]<{(){}}>>}[(<[[]{}](()())>[[()()][{}[]]])])<[(<[<><>](()[])><{{}{}
[[(({(({([[<{<{}<>>{(){}}}{(<>[])<[]()>}>][[[<[]()>[()]][<<>{}><{}()>]]([([][])])]]<({[[<>[]][{}<>]]<{
<<[{{{<<[<{<<[<><>]{[]}>>{<([]<>)<<>[]>>[(<>{})(()())]}}{<[{{}<>}(<>[])]>[[{{}{}}<{}<>>](([
((({{[[<[([[<{()<>}({}{})><<<>[]><()<>>>]{{<[]{}>[{}()]}(<[]<>><()[]>)}][{{<[][]>}<[()[]]<[]()>>}])]>]]}([{{[
[([{({<<[{[[{{()()}}[[<>()]([]())]]{{(()<>)[{}()]}{[(){}][<>()]}}]}]>(([{[{[[]{}]{[]<>]}]{{<[]<
<(<(<({{<<([<{[]{}}[{}<>]>{<<><>>{{}{}}}])({(<<><>>((){}))({[]<>}<{}<>>)})>{{[<<()<>><[]()>><((){})<{}<>>>]<
([{<{{[{([{[<[(){}]{(){}}><{[]()}[{}{}]>]({<<>{}>({}[])})}[{[{[]()}<(){}>]({[]{}}{()()})}(
(<[([[(<([([<[[]()]>][<[()<>]([][]}>[(()[])<[]()>]])<{[<[][]>(()[])](([]()))}{[<{}<>>[[]()]]((()<>)
[([((([<<{[({{{}()}(<>())}([()<>]<{}()>))<<(()[])[()<>]>{<()>}>]}{{((({}{})[{}<>])[{[]()}([]{})])<<{<>[]}(
<([{(<<[(<({<<{}{}>{{}[]})<<<>[]><[]<>>>}(<([]<>)<(){}>>[[{}{}]<[]{}>]))>{([<<{}{}>>({[]()})])})](
[(<[((<{[(((([[]]<<>[]>){<{}<>><()()>}))<[[{{}()}<<>()>]]>)]<<(<{(<>{})}{({}<>){{}()}}>([{()<>}{{}()
<{(<<[{<<[({<<{}{}><()[]>>}){[([{}{}]([]{}])(((){})[[]<>])]<<<()<>><<><>>>([{}()]<[][]>)>}]>>(<(<({({}
(([{{<{{(<{[{{<>[]}}({{}[]}<(){}>)]{(([]())[()<>])<[[]][[]()]>}}{<({(){}})<[{}{}]>>[((()()))]}
[(({[<{([{[[{[[]()]<[]{}>}]{{<{}[]>{<>{}}}{(()<>)<()<>>}}][<<<{}()>(()())>>]}(([{{<>[]}[(){}]}[([]<>)<
[<(<{{(<[(<<({()<>}<()()>)([<><>]<(){}>)>>)]>{<<[([[(){})<<>[]>])({<()>(()[])})][[({{}}(<>())){
(((<<[<(([<[<[()()][{}()]>[([]())[[]()]]][<<<><>>{<><>}>{<<>[]>[<>()]}]>}{<{[[()[]]<()()>]{[[][]][[]
{{(<<{(<[<({{<<>()>{{}[]}}([[]{}]<[][]>)}<(({}()){()()})>)>({<[[<><>]{[]}]{(<>())([]{})}>[[<<>[]>({}
<<<[[[<[<{[[<({}())[[]()]><{(){}}{{}}>]][[[[{}[]]<(){}>]]<[{()()}<[]{}]]<(<>())[<><>]>>]}<[[({<>}<{}{}>)<
(<(<[<<{<[({([[]{}]{[]}){[(){}](<>{})}}{[[[]][[]()]]})<[<[<>()]{{}()}>][((()[]){(){}})[[()
<[{{{({(([{[{<{}<>>}<[{}<>]>]}]<([{{(){}}{[][]}}[<<>()><{}<>>]][{<{}[]>{()()}}])[({[{}<>][[][]]}{[{
<[<{[([<<[[<[<()[]>[<>{}]]{([][]){[]<>}}>({{()<>}[()<>]}[{<>()}{<>{}}])]<<{({}())(<>)}[[()()][[]{}]]>>]>><(
{[{<([(<<[[<[[<>{}}{{}{}}][{{}{}}[{}{}]]>][{([[][]](()[]))(([][])(()<>))}{[<<>{}>{[]}]{((){})(<>)}}]]{[<<<
(({<<[<[<(<[<[<>{}]([]{})>(<[]<>><()>)][<[<>()]{()[]}>]>)[[[({{}{}){<><>})[(<>()){{}<>}]]]<<[<()[]>([][])]<<[
<[({<<<<[{<<([{}[]][[]<>])[({}())<()<>>]>{<{{}<>}{[]}>[([]())({}()))}>}]{[<<<(<>())(()<>)>>{[<<><>>({}{})][
<<((<{<{[{<<{<()<>>{<>{}}}>>}{<{{<<>>(()())}<{[]()}[()<>]>}{[<()>{<><>}]}>([<{[]<>}[{}<>]>{[[]<>]<<
([<[<[<<{{{{{(()<>)[<>()]}[({}{})[{}{}]]}(((()()))<{<><>}>)}<[<[<>()]{<><>}>[{<><>}{()}]](
[([[((<({{([[([]{}){{}<>}]([[]()]<<>[]>)][[<()()>{{}}]<((){})[<>()]>])<<{<[][]>}[[[]<>]<{}<>>]>{{({}[])(
<(<<[<{{[([([(<>()}{()()}][{{}{}}[<>]]){<{<><>}<[]<>>>{{<><>}[[]()]}}]<<[([]<>)<()[]>]><[<[][]>]<[{}{}]{{}[]}
[({(({(({{([{[{}()]<()<>>}(([][])({}))])({(<{}()>)}{{([]<>)(<>{})}})}({<[<{}<>>{[][]}]({<>[]}<{
[([[<[[{{<(<{<()<>><<>()]}<<[]{}>([]())>><<[[]<>]{{}<>}>>)><([<({}())>((<>{})[(){}])][<<[]()>(
[[(((<{({<<(<({}[])({}[])>[{[]{}}{<>{}}])[<<[][]>{<><>}>([[]()](<>[]))]>({<<<><>><[]()>>[([][])[[]{}]]})>}<
{[<(<<[{{{{<<{{}{}}{()<>}>(<[]()>({}<>))>}({<{{}()}{()()}>(<[]{}>({}<>))}([[<>()]({}())}((
{[{([{<([({{{[{}<>){{}}}(<<>{}>{<><>})}[<<()()><<>[]>>]})(<<{([]<>)<{}()>}>>)])(<{<((<()[]>{{}{}})){({[]<>}(
<[[<(<{{{{{((<()<>>(<>{}))([{}{}]<<>[]>))<{<[]>(<>{})}>}}[(((([]<>){<>[]}))(({<>()}({}[]))))]}([{[<{[]
{<({[{[{<{{{[[<><>]{{}<>})<<[]()>([])>}[{{<>[]}{()<>}}]}<<{<{}<>>({}<>)}[<{}()>[[]<>]]>[<<<>{}>[(){}
<({({{([<[[[<{<>{}}([])>](<{[]}<[]<>>>)]]>)<[[<([<{}[]>[()()]]<(<>{})<{}[]>>){{({}<>)[[]<>]}<{{}{}}{()<
<{<{({<<{{{(<{[]{}}(()())>({<>[]}(<>[])))<{([][]){{}()}}(<{}[]>)>}{[<<()<>><{}[]>>{{[]{}}[[]()
{[<({<<{[(<{([{}[]]<[]<>>)<<{}[]>[()()]>}<<{()[]}{<>{}}>[[{}()]([]<>)]>>({[([]{})]<<[]{}><[][]>>}))][{[[<{[]
[<<{<({<(<{{[{()()}<[][]>]{{<><>}({}{})}}}<<(<()[]>{[][]})[[{}()]]><[<<>[]>(()<>)][(<>{})([]())]>>>[[<({
{{((([[<[<[[[{()<>}(<><>)](<{}()>({}()))][{<{}()>[{}[]]}{<{}[]>[{}<>]}]]({{[<>[]]<<><>>}([<>]{[]})}([({
<<([[{[{[<(({[{}{}]<()<>}}[(<>{})[(){}]])([[{}{}]]{[()<>]<()<>>})){{[([][])<()[]>]}}>]((<(<({
(<({[{[{(<<<<([]<>)<[]()>><{()[]}{{}()}>>{{[[]()](<><>)}({<><>}{<>{}})}>{([((){})(<>())]{<<><>>({}()}})([[{}
<<<{(([(((({{<()())}<((){})[<>{}]>})(<[{<>{}}<{}()>]([{}[]][{}()])>[{{[]{}}{[]<>}}{{<><>}({}<>)}])
(<{{[{{<([[[[([]()){(){}}][<[]()>{<><>}]]{[([])(<><>)]}]])((<[[({}{})[[]{}]]<({}){()}>][[(
{(((({([<[<<<[()()](<>{})>[{{}[]}<<>{}>]>>([{<{}()>[[]()]}{({}())<{}()>}])]{({<{[]{}}<[][]>><[[]
(<{[[<{{[((((<(){}><{}[]>)<{()()}<{}()>>)<<[(){}]>>}){(({{[]()}}<<<>[]>([]<>)>){{[<>{}]{[]()
<<{{{<<[{<[<[{[]{}}[{}<>]]<([]{})[<>{}]>>({[()()]}(([][])[<>()]>)][{<<<><>>(()<>)>[{<>()}<[]{
[[<[[[<([((({{{}<>}[()[]]}{([]<>)<{}()>})[(<<>()>{<><>})]))<[({(()()){{}<>}}(({}())<[]{}>))({<<>{}>[[]{}]}(
{[[({[(<<{<((<{}()>({}<>))<<{}()><{}[]>>)>(<[<()()>(<>)]{([][])[(){}]}){([{}()]({}()))[[()
(<({[{<{<({{{({}<>)}{[()<>]<{}{}>}}{<<{}{}>([]<>)>(<{}<>>{<>{}})}}{[{<(){}>><[[]()](<>())>
[<{(({[{[<(<({[]{}}[<>])[{()()}(()<>)]>{{[[]{}]}<({}[])[{}{}]>})[{[[()]{<>{}}][<()[]>[[]()]]
(<[{{<(<({{{({[][]}{()()}){([]())<[]{}>}}}{(<<{}()>{{}{}}>[(<>[])])}]{[<[<(){}><{}<>>]<(<>[])(()[])>>[[{()
<<[<{[(<{({(((<><>))[[{}{}}(()())])<{<()[]>}>})<[{<<{}<>>(()())>[<<>[]>[{}()]]}{{<{}()>[<>{}
<({((<([{{<[(<{}[]><<>()>)(({}()){[]{}})]{{<()[]>[()<>]}<{()[]}{[]<>}>}><<{((){})[{}<>]}{(()[]
<{{[[<{<{<{{{[[]<>]}{{{}{}}<[]()>}}[<{()<>}(()())><<{}<>><()[]>>]}<([[<>()]([]{})]{{()}{<>[]}}){<<[]<>>>((
<<({<{{[<{(((<(){}>{[]<>}){[(){}]})<({[]})[<()<>>{{}<>}]>)}><[({{<[][]>}}<{{[][]}<[]{}>}>>([(<{}<>>(()(
{<([{((({<((<<[]{}><{}<>>>[{{}()}[[][]]]))[<{[{}[]][<>[]]}>[<<{}()>{{}<>}>((<><>))]]>}(((<([[][]){[](
[[<{(<(([<[{(<{}[]>[{}<>])<<{}<>><<>()>>]<{[()<>]{{}{}}}{<<><>>[[]{}]}>]<(<{()()}<<>[]>><({
([{<(<<<([[{[[[]()]({}[])]({[]{}}(<>[]))}<{[[]{}]({}())}>]{([([][])[{}<>]]([<>()]<()[]>))}](<({({}[])((){})}
[<[{{[[[[[<({({}<>){{}{}}}{{[]<>}{<>}}){((<>())<[]<>>)[<()>[{}[]]]}>[{[[{}<>]]{{()<>}<()()>}>[[{[]<
(<{{{<([([([[([]())<[]{}>]<<()><[]()>>])])(<{[<(<>())<{}>>]<{[{}<>]<{}[]>}(([]<>)(<>()))>}(({<<>{}>[[]
{[(<[<{<<[{<<(()<>)<(){}>>[[<>[]](<>())]>{{[[]()][<>[]]}({{}[]}<<><>>)}}<<(([]<>)[()()])[[()<>][
(<<[(([<([<<[[()[]]([]<>)]{(<>())([][])}>>{((<{}[]>{<>{}})[<<>()>])}]{([{{()()}<<>()]}{[[][]][(){}]}]
{{<[<(((<(<{{([]())<{}()>}<<<>[]>{{}<>}>}<<<[]{}>>(<[]{}>({}<>))>><<{[(){}][[]()]}{{<>[]}([]
[{{<[[([{[({[({}<>){{}()}]([{}<>]({}()))}(({()<>}<[][]>)({{}{}}{<>[]})))([([()[]][[]{}])]<<([
([<(({<<(<([<([]()){(){}}>[([]{}){()[]})]{{(<>())(<>[])}<[<>[]][{}[]]>})><[[{(()<>){{}()}}{
<((<<[{[[[[{[[<>{}][[]()]]{{[][]}<[][]>}>]]{({{[()[]]<{}[]>}<[<><>]<[]<>>>}<<<()<>>>[{()()}]>)(<<{<>()}>([{}
([(([{([((<{<([]<>)<{}<>>>(<{}{}>)}{{[{}]{<><>}}[[{}<>]([][]}]}>){<<<[{}()][{}{}]>>>[[{<{}{}>[{}[]]}]<[
{[[(<([([{([<[()[]]<(){}>>[(<><>)[()()]]])}<{[[(<>[])([]{}>]{[[]<>]<{}[]>}]}[(((()<>)<()[]>
{{<(({([<<<<{([]()){()[]}}({()()})><(<()[]>[<>[]]}({<>{}}{[][]})>>{({<()[]>[{}<>]}((()[])(()<>)))}>>[(
{([[(({{{[[{<[{}()]<<>()>>[[()<>]{[]()}]}<[<{}()>{{}()}]{[[]<>](<>())}>]{({<(){}>(<>{})}{<[]>[[]()]}){<(
(<{<[(<[[<[{[[(){}][()()]]<[()<>]((){})>}[(<(){}>{<>{}>)]]{({{()}}{[{}<>]<<><>>})(({<>()}<{}()>)[{()}<<
[([([<{<((([[<[][]>{[]()}][<()<>><{}()>]]({[{}()]}[<()()>{[]<>}]))<[(<()()><()[]>){{[]<>}[()()]}]
{([<({[<((({(<{}<>>[{}()])<[<>{}]>})){{<<<[]()><[]<>>>{{<>()}[<>()]}>{[<()[]>[(){}]]})})<({
<<<[[[((<{[{[([])[()<>]]((<>{}}(()()))}<{<[][]><[]()>}{<(){}>{{}<>}}>]<([<<>>]<([][])({}{})>
<[[({[<([{<<<(()[])>{{(){}}<[]<>>)>>[{{{()<>}[{}()]}<{()[]}[()<>]>}<<{()()}([]())>[(<>()){<>{}}]>]}])[((((<{
⛄"""