-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay18.py
274 lines (247 loc) · 7.36 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from functools import reduce
input = """[[[[4,6],4],[1,7]],[[[1,6],[8,4]],[1,1]]]
[[[[8,5],[9,2]],1],[[2,5],[[9,4],[5,9]]]]
[[[[7,3],0],[8,9]],6]
[[6,[[7,2],[6,2]]],[[[9,8],9],[9,6]]]
[2,[[[9,2],6],[[5,3],[6,7]]]]
[[[5,[9,6]],0],[[[2,8],[7,0]],[7,[4,4]]]]
[[[[5,0],2],[0,1]],4]
[2,[8,8]]
[[[[2,5],[6,8]],[[9,8],4]],[[[2,3],[5,8]],[9,5]]]
[[[[0,7],[9,4]],[[1,0],9]],[[[8,8],[7,2]],[3,[6,5]]]]
[[[[3,2],8],1],[[4,[3,4]],[[6,5],[0,6]]]]
[[[7,8],8],[0,[5,2]]]
[[3,[3,3]],[[[6,9],[1,1]],[6,[2,9]]]]
[[[[9,7],[6,8]],4],[[[8,2],[2,9]],[8,[6,2]]]]
[[[[7,3],2],[9,6]],[[[1,7],[0,0]],[4,9]]]
[[8,[7,[1,0]]],7]
[[[7,[5,1]],0],[[8,[5,3]],4]]
[1,[[[2,6],2],[1,[6,0]]]]
[[[5,8],[[9,1],1]],[[3,[5,0]],5]]
[[[[1,5],[4,9]],8],[[7,0],6]]
[9,[[0,[1,0]],6]]
[[[[6,8],6],9],[[7,3],2]]
[[9,[[8,7],4]],[[[4,0],[9,0]],[8,1]]]
[[[2,[4,4]],[7,[0,1]]],[8,[[8,6],[4,0]]]]
[0,9]
[[[[1,8],[7,4]],[[5,0],[6,1]]],[5,7]]
[[[[8,2],[9,2]],[8,[8,4]]],[0,4]]
[[[[0,7],[5,8]],3],6]
[[[7,[3,4]],[3,[1,5]]],2]
[[[1,[4,2]],5],[[1,2],1]]
[[[[8,2],[0,9]],1],[[[9,0],[3,5]],[8,[8,0]]]]
[[[0,5],[1,[3,3]]],[[[1,0],[5,2]],[7,5]]]
[[[4,[7,3]],[0,9]],[[2,0],8]]
[[[[2,2],8],[7,1]],5]
[[1,[[3,8],7]],[[7,[5,8]],[4,[1,7]]]]
[[[[2,7],4],[8,[9,1]]],[[5,2],[4,3]]]
[[2,[7,2]],[[8,[0,8]],[0,[4,2]]]]
[[6,[6,[7,4]]],[[7,[2,0]],[[8,2],8]]]
[[[7,[1,7]],[[4,1],4]],[1,[4,6]]]
[1,[[1,0],[[0,3],[6,9]]]]
[[[[8,6],0],[[2,8],[3,0]]],[[[8,2],7],[[3,0],5]]]
[[[[2,8],4],[2,[0,7]]],[[3,[1,2]],[[8,0],[4,2]]]]
[1,8]
[[5,6],6]
[[[[1,0],[3,6]],[[4,0],1]],[0,7]]
[[[5,[9,6]],[7,[1,2]]],2]
[[[6,4],[[5,6],[1,8]]],[[[9,0],[7,7]],[[5,8],[6,8]]]]
[8,5]
[5,[[[6,8],8],0]]
[[[[5,7],[0,0]],[6,[0,0]]],[[[5,5],3],[[1,1],[3,4]]]]
[[[4,0],[[8,6],2]],[[3,[3,1]],[[2,8],[7,2]]]]
[[[8,7],[[5,5],[5,3]]],4]
[[[[5,4],1],[3,4]],[3,5]]
[[[6,5],[[6,3],6]],4]
[[[[2,2],[7,1]],[6,6]],[[8,[8,7]],[[1,6],[3,0]]]]
[[4,[[5,0],[7,4]]],[3,1]]
[[[3,[5,8]],5],[1,[[9,6],3]]]
[[0,[[3,0],[8,7]]],[[1,3],3]]
[5,[[3,[3,3]],[3,6]]]
[[[[7,3],8],3],[2,[[9,8],2]]]
[[[2,4],[[1,2],5]],[[[1,2],[6,0]],3]]
[[9,[[1,1],[1,7]]],[1,[2,[9,1]]]]
[[[5,[0,0]],5],[6,[0,1]]]
[[3,[[6,5],7]],[[7,8],3]]
[[5,[2,6]],8]
[[6,[0,[3,0]]],[1,2]]
[3,[[[3,7],2],[[4,0],6]]]
[[[8,[2,7]],[4,1]],[[2,[4,2]],3]]
[[3,2],[[[8,8],[8,6]],[[5,3],1]]]
[1,[2,[[3,2],[2,9]]]]
[8,[[9,1],[[8,4],[9,9]]]]
[[[4,[4,6]],[1,8]],[[7,7],[[7,4],3]]]
[[[8,2],[[9,7],[0,8]]],[[4,4],[[6,1],5]]]
[[[3,[6,6]],[[8,6],[3,7]]],[[7,9],[[5,3],8]]]
[[[8,9],[8,6]],[[[3,3],[2,9]],[[6,6],9]]]
[8,[[[3,0],5],2]]
[[[[1,3],1],[[1,9],4]],[7,[3,1]]]
[[[[9,3],3],[[6,8],7]],[[[2,0],3],[8,[3,6]]]]
[[[[7,1],[8,1]],[[4,6],[5,9]]],[[[4,5],3],5]]
[6,[[3,[0,0]],[6,6]]]
[[[[8,8],[7,6]],3],[[[7,7],[1,1]],[[1,8],[1,4]]]]
[[9,[8,[3,4]]],[[6,2],[1,5]]]
[[5,[3,3]],[5,[0,5]]]
[[[[8,9],5],[1,9]],[[5,[2,8]],[[6,4],[9,4]]]]
[2,6]
[[[[1,4],8],5],[5,[0,[1,7]]]]
[[[[1,0],[9,9]],[0,9]],[[[5,4],[1,6]],[9,[6,7]]]]
[[[7,1],5],[[3,2],5]]
[[9,[[8,8],[7,0]]],[5,[3,[1,3]]]]
[[[[5,2],[7,5]],[4,[6,7]]],[[[8,1],6],[2,[6,6]]]]
[[[5,7],[6,[8,2]]],[8,2]]
[[[[5,7],8],[[9,8],2]],[[2,8],[[7,6],3]]]
[[1,[[1,6],1]],[0,[[5,9],[9,1]]]]
[[[[1,4],[5,0]],[[5,5],[9,3]]],[[6,4],[4,[4,6]]]]
[7,[[5,[4,8]],[[5,9],2]]]
[[[[2,9],[1,8]],[4,2]],0]
[[5,[[0,9],[3,7]]],[2,[6,[4,8]]]]
[[0,[5,5]],0]
[[[5,0],[[0,5],8]],[6,[[8,7],[6,5]]]]
[[[5,[8,2]],[8,4]],[[6,2],[8,[7,0]]]]"""
input_test = """[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
[7,[[[3,7],[4,3]],[[6,3],[8,8]]]]
[[2,[[0,8],[3,4]]],[[[6,7],1],[7,[1,6]]]]
[[[[2,4],7],[6,[0,5]]],[[[6,8],[2,8]],[[2,1],[4,5]]]]
[7,[5,[[3,8],[1,4]]]]
[[2,[2,2]],[8,[8,1]]]
[2,9]
[1,[[[9,3],9],[[9,0],[0,7]]]]
[[[5,[7,4]],7],1]
[[[[4,2],2],6],[8,7]]"""
def explode(lis, index):
res = lis[:index]
ind = index
while lis[ind] != ']':
ind += 1
item = lis[index:ind + 1]
it = item[1:-1].split(",")
res2 = lis[ind + 1:]
found1 = True
for x in range(1, len(res)):
if res[-x].isdigit():
y = x
while res[-y].isdigit():
y += 1
dig = int(res[-y + 1:-x + 1]) + int(it[0])
res = res[:len(res) - y + 1] + str(dig) + res[len(res) - x + 1:]
break
elif res[-x] == '[':
found1 = False
found2 = True
for x in range(len(res2)):
if res2[x].isdigit():
y = x
while res2[y].isdigit():
y += 1
dig = int(res2[x:y]) + int(it[1])
res2 = res2[:x] + str(dig) + res2[y:]
break
elif res2[x] == ']':
found2 = False
st = ""
if not found1 or not found2:
st = "0"
res = res + st + res2
return res
def check_explode(lis, index):
size_parenth = 0
ind = 0
while ind < index:
if lis[ind] == '[':
size_parenth += 1
elif lis[ind] == ']':
size_parenth -= 1
ind += 1
if size_parenth >= 4:
return explode(lis, index)
return lis
def splitting(lis, index):
res = lis[:index - 1]
res2 = lis[index + 1:]
dig = int(lis[index - 1:index + 1])
r = res + f"[{dig // 2},{(dig + 1) // 2}]" + res2
return check_explode(r, len(res))
def check_number(lis, index):
ind = index
while lis[ind] != ']':
ind += 1
item = lis[index:ind + 1]
it = item[1:-1].split(",")
if int(it[0]) >= 10 or int(it[1]) >= 10:
return False
return True
def reduction(lis):
res = " "
stack = []
size_parenth = 0
previous = lis
while (res != ""):
res = ""
size_parenth = 0
previous_d = False
for index, item in enumerate(previous):
if item == '[':
size_parenth += 1
if size_parenth == 5:
# to_explode = check_number(previous, index)
# if to_explode:
res = explode(previous, index)
break
elif item == ']':
size_parenth -= 1
if res != "":
previous = res
else:
for index, item in enumerate(previous):
if item in "1234567890":
if previous_d:
res = splitting(previous, index)
break
previous_d = True
else:
previous_d = False
if res != "":
previous = res
return previous
def add_two(lis1, lis2):
sol = f"[{lis1},{lis2}]"
op = reduction(sol)
return op
def replace_mag(lis, index):
ind = index
while lis[ind] != '[':
ind -= 1
item = lis[ind + 1: index]
it = list(map(int, item.split(',')))
dig = 3 * it[0] + 2 * it[1]
return lis[:ind] + str(dig) + lis[index + 1:]
def compute_mag(lis):
prev = lis
res = " "
while res != "":
res = ""
for index, item in enumerate(prev):
if item == ']':
res = replace_mag(prev, index)
break
if res != "":
prev = res
return int(prev)
def first_star():
input_list = input.split('\n')
final_sum = reduce(lambda x, y: add_two(x, y), input_list)
print(f"First star: {compute_mag(final_sum)}")
def second_star():
input_list = input.split('\n')
ref = 0
for index, i in enumerate(input_list):
for index2, j in enumerate(input_list):
if index != index2:
x = compute_mag(add_two(i, j))
if x > ref:
ref = x
print(f"Second star: {ref}")
if __name__ == '__main__':
first_star()
second_star()