-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathday18.js
288 lines (264 loc) · 6.81 KB
/
day18.js
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const assert = require("assert");
const fs = require("fs");
const lines = fs
.readFileSync("day18.txt", { encoding: "utf-8" }) // read day??.txt content
.trim()
.replace(/\r/g, "") // remove all \r characters to avoid issues on Windows
.split("\n") // Split on newline
.map((line) => JSON.parse(line));
function explode(snailfish, level = 1) {
for (let i = 0; i < snailfish.length; i++) {
const s = snailfish[i];
if (
level === 4 &&
s.length &&
s.length === 2 &&
typeof s[0].value !== "undefined" &&
typeof s[1].value !== "undefined"
) {
// Explode
console.log("explode");
// console.log(s[0].previous, s[1].next);
let prev = s[0].previous;
if (prev) {
prev.value += s[0].value;
}
let next = s[1].next;
if (next) {
next.value += s[1].value;
}
const object = {
value: 0,
previous: s[0].previous,
next: s[1].next,
parent: s[0].parent.parent,
};
if (s[0].previous) {
s[0].previous.next = object;
}
if (s[1].next) {
s[1].next.previous = object;
}
snailfish[i] = object;
return true;
}
const res = explode(s, level + 1);
if (res) {
return true;
}
}
}
function split(snailfish) {
for (let i = 0; i < snailfish.length; i++) {
const s = snailfish[i];
if (typeof s.value !== "undefined" && s.value >= 10) {
//split
console.log("split");
const one = {
value: Math.floor(s.value / 2),
previous: s.previous,
};
const two = {
value: Math.ceil(s.value / 2),
previous: one,
next: s.next,
};
one.next = two;
snailfish[i] = [one, two];
snailfish[i].parent = s.parent;
one.parent = snailfish[i];
two.parent = snailfish[i];
if (s.next) {
s.next.previous = two;
}
if (s.previous) {
s.previous.next = one;
}
return true;
}
const res = split(s);
if (res) {
return true;
}
}
}
function createObject(array) {
let previous = null;
let last = null;
function doThing(value, parent) {
value.parent = parent;
for (let i = 0; i < value.length; i++) {
const element = value[i];
if (typeof element === "number") {
//it's a number
const object = {
value: element,
previous,
parent: value,
};
value[i] = object;
previous = object;
last = object;
if (!array.first) {
array.first = object;
}
} else {
doThing(element, value);
}
}
}
doThing(array, null);
array.last = last;
let next = null;
while (last) {
last.next = next;
next = last;
last = last.previous;
}
return array;
}
function toString(snailfish) {
if (typeof snailfish.value !== "undefined") {
return snailfish.value;
}
let str = "[";
for (let i = 0; i < snailfish.length; i++) {
const e = snailfish[i];
str += toString(e);
if (i < snailfish.length - 1) {
str += ",";
}
}
str += "]";
return str;
}
function reduce(o) {
// console.log(toString(o));
let modified;
do {
modified = false;
modified = explode(o);
if (!modified) {
modified = split(o);
}
if (modified) {
console.log(toString(o));
}
} while (modified);
}
function add(one, two) {
reduce(one);
reduce(two);
const res = [one, two];
one.parent = res;
two.parent = res;
one.last.next = two.first;
two.first.previous = one.last;
res.first = one.first;
res.last = two.last;
reduce(res);
return res;
}
function processList(lines) {
let sum = createObject(lines[0]);
for (let i = 1; i < lines.length; i++) {
const current = createObject(lines[i]);
sum = add(sum, current);
}
return sum;
}
function check(lines, expectedResult) {
const sum = processList(lines.split("\n").map((x) => JSON.parse(x)));
assert.equal(toString(sum), expectedResult);
}
// check(
// `[[[0,[4,5]],[0,0]],[[[4,5],[2,6]],[9,5]]]
// [7,[[[3,7],[4,3]],[[6,3],[8,8]]]]`,
// "[[[[4,0],[5,4]],[[7,7],[6,0]]],[[8,[7,7]],[[7,9],[5,0]]]]"
// );
check(
`[[[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]]]]`,
"[[[[6,7],[6,7]],[[7,7],[0,7]]],[[[8,7],[7,7]],[[8,8],[8,0]]]]"
);
// check(
// `[[[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,0],[7,7]],[[7,7],[7,8]]],[[[7,7],[8,8]],[[7,7],[8,7]]]]"
// );
// check(
// `[[[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]]]]`,
// "[[[[7,7],[7,8]],[[9,5],[8,7]]],[[[6,8],[0,8]],[[9,9],[9,0]]]]"
// );
// check(
// `[[[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]]]`,
// "[[[[6,6],[6,6]],[[6,0],[6,7]]],[[[7,7],[8,9]],[8,[8,1]]]]"
// );
// check(
// `[[[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]
// `,
// "[[[[6,6],[7,7]],[[0,7],[7,7]]],[[[5,5],[5,6]],9]]"
// );
// check(
// `[[[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]]]]`,
// "[[[[7,8],[6,7]],[[6,8],[0,8]]],[[[7,7],[5,0]],[[5,5],[5,6]]]]"
// );
// check(
// `[[[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]`,
// "[[[[7,7],[7,7]],[[8,7],[8,7]]],[[[7,0],[7,7]],9]]"
// );
// check(
// `[[[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]]`,
// "[[[[8,7],[7,7]],[[8,6],[7,7]]],[[[0,7],[6,6]],[8,7]]]"
// );
// function part1() {
// const sum = processList(lines);
// console.log(toString(sum));
// }
// part1();
function part2() {
//do something here
}
part2();