-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18a.py
177 lines (127 loc) · 5 KB
/
day18a.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
from math import ceil
f = open("input.txt", "r")
#f = open("testInput.txt", "r")
dat = f.read().splitlines()
def giveIntStartingFromLeft(s, i):
assert(0 <= i < len(s))
assert(s[i].isnumeric())
while i < len(s) and s[i].isnumeric():
i += 1
return i
def giveIntStartingFromRight(s, i):
assert(0 <= i < len(s))
assert(s[i].isnumeric())
while i >= 0 and s[i].isnumeric():
i -= 1
return i
def add(a,b):
new = "[" + a + ',' + b + ']'
#print(new)
# if can explode, explode. else, split
while True:
broken = False
s = 0
for i,c in enumerate(new):
if c == "[":
s += 1
elif c == "]":
s -= 1
if s > 4 and c.isnumeric():
commaIndex = giveIntStartingFromLeft(new, i)
# now we have a pair
# EXPLODE here
if new[commaIndex+1].isnumeric():
newStr = ""
pairLeftNum = new[i:commaIndex]
endOfRightNumIndex = giveIntStartingFromLeft(new, commaIndex+1)
pairRightNum = new[commaIndex+1:endOfRightNumIndex]
#print("going to break this pair", new[i:endOfRightNumIndex])
# find nearest number on left
leftPairNum_leftIndex = i
i -= 1
while i >= 0 and not new[i].isnumeric():
i -= 1
# found number on left
if i >= 0:
left_rightIndex = i
left_leftIndex = giveIntStartingFromRight(new,left_rightIndex)
left_leftNum = new[left_leftIndex+1:left_rightIndex+1]
# update new
newStr = new[:left_leftIndex+1] + str(int(left_leftNum) + int(pairLeftNum)) + new[left_rightIndex+1:leftPairNum_leftIndex-1]
else:
newStr = new[:leftPairNum_leftIndex-1]
# find nearest number on right
a = endOfRightNumIndex
while a < len(new) and not new[a].isnumeric():
a += 1
# found number on the right
if a < len(new):
right_leftIndex = a
right_rightIndex = giveIntStartingFromLeft(new, right_leftIndex)
right_rightNum = new[right_leftIndex:right_rightIndex]
newStr += "0" + new[endOfRightNumIndex+1 :right_leftIndex] + str(int(right_rightNum) + int(pairRightNum)) + new[right_rightIndex:]
else:
newStr += "0" + new[endOfRightNumIndex+1 :]
new = newStr
broken = True
#print("just broke:", new)
#print()
break
if not broken:
for i,c in enumerate(new):
if c.isnumeric():
rightIndex = giveIntStartingFromLeft(new,i)
num = int(new[i:rightIndex])
# split in progress
if num >= 10:
#print("going to split this num", num)
left,right = num//2, ceil(num/2)
new = new[:i] + "[" + str(left) + "," + str(right) + ']' + new[rightIndex:]
broken = True
#print('just split', new)
#print()
break
if not broken:
return new
prev = dat[0]
for i in range(1,len(dat)):
prev = add(prev, dat[i])
#print('finished an addition')
#print(prev)
#print()
# [[1,2],[[3,4],5]]
def magnitude(string):
#print("got", string)
if string.isnumeric():
#print('its an int!', string)
return int(string)
s = 0
for i,c in enumerate(string):
if c == "[":
s += 1
elif c == "]":
s -= 1
if c == "]" and s == 1:
#print('a',string[1:i+1])
#print('b',string[i+2:-1])
#print()
left = magnitude(string[1:i+1])
right = magnitude(string[i+2:-1])
#print('processed, got', left,right)
#print()
return 3*left + 2*right
#print("got to bottom with", string)
# of the form [num1,num2]
a,b = string.split(",")
val = 3*int(a[1:]) + 2*int(b[:-1])
#print('returning', val)
#print()
return val
assert(magnitude("[[1,2],[[3,4],5]]") == 143)
assert(magnitude("[[[[0,7],4],[[7,8],[6,0]]],[8,1]]") == 1384)
assert(magnitude("[[[[1,1],[2,2]],[3,3]],[4,4]]") == 445)
assert(magnitude("[[[[3,0],[5,3]],[4,4]],[5,5]]") == 791)
assert(magnitude("[[[[5,0],[7,4]],[5,5]],[6,6]]") == 1137)
assert(magnitude("[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]") == 3488)
print(prev)
print(magnitude(prev))