-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.jl
224 lines (215 loc) · 5.91 KB
/
day18.jl
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
mutable struct SnailNode
ch_left::Union{Integer,SnailNode,Nothing}
ch_right::Union{Integer,SnailNode,Nothing}
par::Union{SnailNode,Nothing}
end
SnailNode() = SnailNode(nothing,nothing,nothing)
##
function parse_snailnumber(st::String)
root_node = nothing
cur_node = nothing
left = true
for ch in st
if ch == '['
new_node = SnailNode()
if isnothing(root_node)
root_node = new_node
else
new_node.par = cur_node
if left
cur_node.ch_left = new_node
else
cur_node.ch_right = new_node
end
end
cur_node = new_node
left = true
elseif ch == ','
left = false
elseif ch == ']'
left = true
cur_node = cur_node.par
else
if left
cur_node.ch_left = ch - '0'
else
cur_node.ch_right = ch - '0'
end
end
end
root_node
end
##
function snail_to_string(sn::Union{SnailNode,Integer})
if typeof(sn)<:Integer
return string(sn)
end
"["*snail_to_string(sn.ch_left)*","*snail_to_string(sn.ch_right)*"]"
end
##
function split(sn::SnailNode)
if typeof(sn.ch_left)<:Integer
if sn.ch_left > 9
spl = sn.ch_left / 2
sn.ch_left = SnailNode(floor(Int,spl), ceil(Int,spl), sn)
return true
end
else
if split(sn.ch_left)
return true
end
end
if typeof(sn.ch_right)<:Integer
if sn.ch_right > 9
spl = sn.ch_right / 2
sn.ch_right = SnailNode(floor(Int,spl), ceil(Int,spl), sn)
return true
end
else
if split(sn.ch_right)
return true
end
end
false
end
##
function explode(sn::SnailNode, d::Integer)
if d == 4
if typeof(sn.ch_left)==SnailNode
lval = sn.ch_left.ch_left
rval = sn.ch_left.ch_right
sn.ch_left = 0
#right value will always go to the right child
#if we call explode in the right order
#the right child will at most be one level deep
if typeof(sn.ch_right)<:Integer
sn.ch_right += rval
else
sn.ch_right.ch_left += rval
end
#find the leftmost neighbour
cur_node = sn
while ~isnothing(cur_node.par) && cur_node == cur_node.par.ch_left
cur_node = cur_node.par
end
cur_node = cur_node.par
if ~isnothing(cur_node)
if typeof(cur_node.ch_left)<:Integer
cur_node.ch_left += lval
return true
end
cur_node = cur_node.ch_left
while typeof(cur_node.ch_right) == SnailNode
cur_node = cur_node.ch_right
end
cur_node.ch_right += lval
end
return true
elseif typeof(sn.ch_right) == SnailNode
lval = sn.ch_right.ch_left
rval = sn.ch_right.ch_right
sn.ch_right = 0
#left value will always go to the left child
#if we call explode in the right order
#the left child will at most be one level deep
if typeof(sn.ch_left)<:Integer
sn.ch_left += lval
else
sn.ch_left.ch_right += lval
end
#find the leftmost neighbour
cur_node = sn
while ~isnothing(cur_node.par) && cur_node == cur_node.par.ch_right
cur_node = cur_node.par
end
cur_node = cur_node.par
if ~isnothing(cur_node)
if typeof(cur_node.ch_right)<:Integer
cur_node.ch_right += rval
return true
end
cur_node = cur_node.ch_right
while typeof(cur_node.ch_left) == SnailNode
cur_node = cur_node.ch_left
end
cur_node.ch_left += rval
end
return true
end
else
if typeof(sn.ch_left) == SnailNode && explode(sn.ch_left,d+1)
return true
elseif typeof(sn.ch_right) == SnailNode && explode(sn.ch_right, d+1)
return true
end
end
false
end
##
tree = parse_snailnumber("[[[[[4,3],4],4],[7,[[8,4],9]]],[1,1]]")
explode(tree,1)
explode(tree,1)
snail_to_string(tree)
##
function add(sn1::SnailNode, sn2::SnailNode)
sn_root = SnailNode(sn1,sn2,nothing)
sn1.par = sn_root
sn2.par = sn_root
while true
if explode(sn_root,1)
continue
end
if ~split(sn_root)
break
end
end
sn_root
end
##
t1 = parse_snailnumber("[[[[4,3],4],4],[7,[[8,4],9]]]")
t2 = parse_snailnumber("[1,1]")
tres = add(t1,t2)
##
snail_to_string(tres)
##
function sum_file(file)
snails = readlines(file)
cur_snail = parse_snailnumber(snails[1])
for sstr in snails[2:end]
new_snail = parse_snailnumber(sstr)
cur_snail = add(cur_snail, new_snail)
end
cur_snail
end
##
sum_file("test18-3.txt")
##
function magnitude(sn::Union{SnailNode,Integer})
if typeof(sn)<:Integer
return sn
else
return 3 * magnitude(sn.ch_left) + 2 * magnitude(sn.ch_right)
end
end
##
magnitude(sum_file("test18-3.txt"))
magnitude(sum_file("input18.txt"))
##
function max_mag(file)
snailstrings = readlines(file)
max_m = 0
for (i,st1) in enumerate(snailstrings), (j,st2) in enumerate(snailstrings)
(i == j) && continue
sn1 = parse_snailnumber(st1)
sn2 = parse_snailnumber(st2)
mag = magnitude(add(sn1,sn2))
if mag > max_m
max_m = mag
end
end
max_m
end
##
max_mag("test18-4.txt")
##
max_mag("input18.txt")