-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheighteen.js
More file actions
244 lines (213 loc) · 5.35 KB
/
Copy patheighteen.js
File metadata and controls
244 lines (213 loc) · 5.35 KB
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
import { readFile } from 'fs/promises';
// 18a
async function init() {
// console.time('0');
const input = await readFile('eighteen.txt', 'utf8');
const lines = input.split('\n').map((line) => JSON.parse(line));
// const final = lines.reduce((acc, line) => {
// let current;
// if (acc) {
// current = [acc, line];
// // console.log(JSON.stringify(current));
// } else {
// return line;
// }
// while (true) {
// let path = findExplode(current);
// if (path) {
// explode(current, path);
// // console.log(JSON.stringify(current));
// } else {
// path = findSplit(current);
// if (path) {
// split(current, path);
// // console.log(JSON.stringify(current));
// } else {
// // done
// // console.log(JSON.stringify(current));
// break;
// }
// }
// }
// return current;
// }, false);
// console.log(magnitude(final));
let biggest = 0;
for (let i = 0; i < lines.length; i++) {
for (let j = 0; j < lines.length; j++) {
if (i !== j) {
const left = JSON.parse(JSON.stringify(lines[i]));
const right = JSON.parse(JSON.stringify(lines[j]));
let current = [left, right];
while (true) {
let path = findExplode(current);
if (path) {
explode(current, path);
} else {
path = findSplit(current);
if (path) {
split(current, path);
} else {
// done
break;
}
}
}
const score = magnitude(current);
if (score > biggest) {
biggest = score;
}
}
}
}
console.log(biggest);
}
function magnitude(line) {
if (typeof line === 'object') {
return 3 * magnitude(line[0]) + 2 * magnitude(line[1]);
} else {
return line;
}
}
function findExplode(line) {
const path = [0];
while (path[0] === 0 || path[0] === 1) {
const current = path.reduce((acc, i) => acc[i], line);
if (typeof current === 'object' && path.length > 3) {
return path;
}
if (typeof current === 'object') {
path.push(0);
} else {
// :)
path[path.length - 1]++;
if (path[path.length - 1] === 2) {
path.pop();
path[path.length - 1]++;
if (path[path.length - 1] === 2) {
path.pop();
path[path.length - 1]++;
if (path[path.length - 1] === 2) {
path.pop();
path[path.length - 1]++;
}
}
}
}
}
return false;
}
function explode(line, path) {
const i = path.pop();
let current = path.reduce((acc, i) => acc[i], line);
// console.log({
// line: JSON.stringify(line),
// path: [...path, i],
// cur0: current[i][0],
// cur1: current[i][1],
// });
addLeft(line, [...path, i], current[i][0]);
addRight(line, [...path, i], current[i][1]);
current[i] = 0;
}
function addLeft(line, path, num) {
if (path[path.length - 1] === 1) {
path[path.length - 1] = 0;
} else {
// need to go up until you find a 1
while (path.length) {
path.pop();
if (path[path.length - 1] === 1) {
path[path.length - 1] = 0;
break;
}
}
}
if (path.length) {
// go down and right
while (true) {
const current = path.reduce((acc, i) => acc[i], line);
if (typeof current === 'object') {
path.push(1);
} else {
// found it!
const i = path.pop();
const parent = path.reduce((acc, i) => acc[i], line);
parent[i] += num;
break;
}
}
} else {
// console.log('none');
}
}
function addRight(line, path, num) {
if (path[path.length - 1] === 0) {
path[path.length - 1] = 1;
} else {
// need to go up until you find a 0
while (path.length) {
path.pop();
if (path[path.length - 1] === 0) {
path[path.length - 1] = 1;
break;
}
}
}
if (path.length) {
// go down and left
while (true) {
const current = path.reduce((acc, i) => acc[i], line);
if (typeof current === 'object') {
path.push(0);
} else {
// found it!
const i = path.pop();
const parent = path.reduce((acc, i) => acc[i], line);
parent[i] += num;
break;
}
}
} else {
// console.log('none');
}
}
function findSplit(line) {
const path = [0];
while (path[0] === 0 || path[0] === 1) {
const current = path.reduce((acc, i) => acc[i], line);
if (current !== 'object' && current >= 10) {
return path;
}
if (typeof current === 'object') {
path.push(0);
} else if (current >= 10) {
return path;
} else {
// :)
path[path.length - 1]++;
if (path[path.length - 1] === 2) {
path.pop();
path[path.length - 1]++;
if (path[path.length - 1] === 2) {
path.pop();
path[path.length - 1]++;
if (path[path.length - 1] === 2) {
path.pop();
path[path.length - 1]++;
}
}
}
}
}
return false;
}
function split(line, path) {
const i = path.pop();
const current = path.reduce((acc, i) => acc[i], line);
const num = current[i];
const left = Math.floor(num / 2);
const right = num - left;
current[i] = [left, right];
}
init();