-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.py
221 lines (160 loc) · 4.52 KB
/
day18.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
from itertools import permutations
from math import ceil, floor
class Node:
def __init__(self, level, ch1, ch2):
self.level = level
self.left = ch1
self.right = ch2
self.parent = None
def is_simple_node(self):
return is_int(self.left) and is_int(self.right)
def __repr__(self):
return f'[{self.left},{self.right}]'
def magnitude(self):
mag = 0
if is_int(self.left):
mag += 3 * self.left
else:
mag += 3 * self.left.magnitude()
if is_int(self.right):
mag += 2 * self.right
else:
mag += 2 * self.right.magnitude()
return mag
def is_node(v): return isinstance(v, Node)
def is_int(v): return isinstance(v, int)
def parse_snail(d):
stack = []
current_level = 0
for c in filter(lambda s: s not in list(', '), d):
if c == '[':
current_level += 1
elif c == ']':
v1 = stack.pop()
v2 = stack.pop()
n = Node(current_level, v2, v1)
if is_node(v1): v1.parent = n
if is_node(v2): v2.parent = n
stack.append(n)
current_level -= 1
else:
stack.append(int(c))
return stack[0]
def inc_level(p):
if not is_node(p): return
p.level += 1
inc_level(p.left)
inc_level(p.right)
def add_snails(s1, s2):
s = Node(0, s1, s2)
s1.parent = s
s2.parent = s
inc_level(s)
return s
def explode_left(p, v):
t = p.parent
if is_int(t.left):
t.left += v
return
prev = p
while t and prev == t.left:
prev = t
t = t.parent
if t is None: return
if is_int(t.left):
t.left += v
return
t = t.left
while is_node(t.right):
t = t.right
t.right += v
def explode_right(p, v):
t = p.parent
if is_int(t.right):
t.right += v
return
prev = p
while t and prev == t.right:
prev = t
t = t.parent
if t is None: return
if is_int(t.right):
t.right += v
return
t = t.right
while is_node(t.left):
t = t.left
t.left += v
def explode_node(n: Node):
if n is None: return
p = n.parent
explode_right(n, n.right)
explode_left(n, n.left)
if p.left == n:
p.left = 0
else:
p.right = 0
def split_node(n: Node):
if n is None: return
if is_int(n.left) and n.left > 9:
v = n.left
n.left = Node(n.level + 1, floor(v / 2), ceil(v / 2))
n.left.parent = n
return
if is_int(n.right) and n.right > 9:
v = n.right
n.right = Node(n.level + 1, floor(v / 2), ceil(v / 2))
n.right.parent = n
return
def check_for_explode(p):
if is_int(p): return None
if is_node(p) and p.level == 5: return p
for_explode = check_for_explode(p.left)
if for_explode: return for_explode
return check_for_explode(p.right)
def check_for_split(p):
if is_int(p.left) and is_int(p.right):
if p.left > 9 or p.right > 9: return p
return None
elif is_int(p.left):
if p.left > 9: return p
return check_for_split(p.right)
elif is_int(p.right):
found = check_for_split(p.left)
if found: return found
if p.right > 9: return p
return None
else:
found = check_for_split(p.left)
if found: return found
found = check_for_split(p.right)
if found: return found
return None
def reduce_snail(snail):
while True:
found = check_for_explode(snail)
if found:
explode_node(found)
continue
split = check_for_split(snail)
if split:
split_node(split)
if not found and not split:
break
return snail
def add_list_of_snails(list_snails):
snails = list(map(parse_snail, list_snails))
final_sum = snails[0]
for s in snails[1:]:
final_sum = add_snails(final_sum, s)
final_sum = reduce_snail(final_sum)
return final_sum
snails = open('input.txt').read().strip().splitlines()
# one star
print(add_list_of_snails(snails).magnitude())
# two stars
max_mag = -1
for c in permutations(snails, 2):
m = add_list_of_snails(list(c))
max_mag = max(max_mag, m.magnitude())
print(max_mag)