-
Notifications
You must be signed in to change notification settings - Fork 2
/
day18.js
157 lines (144 loc) · 3.46 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
'use strict';
const fs = require('fs');
const cl = console.log;
fs.readFile('day18.txt', 'utf-8', (err, input) => {
if (err) throw err;
let lines = input.trim().split(/\r?\n/);
cl(snailfish1(lines.map(JSON.parse)));
cl(snailfish2(lines.map(JSON.parse)));
});
function snailfish1(numbers) {
let r = numbers.reduce(add);
return magnitude(r);
}
function snailfish2(numbers) {
let dup = n => JSON.parse(JSON.stringify(n));
let max = 0;
for (let a of numbers) {
for (let b of numbers) {
if (a !== b) {
max = Math.max(max, magnitude(add(dup(a), dup(b))));
}
}
}
return max;
}
function magnitude(n) {
if (isRegularNumber(n)) {
return n;
}
let [left, right] = n;
return 3 * magnitude(left) + 2 * magnitude(right);
}
function add(a, b) {
let n = [a, b];
reduce(n);
return n;
}
function reduce(n) {
do {
while (explode(n));
}
while (split(n));
}
function split(pair) {
function numToPair(rn) {
let left = Math.floor(rn / 2);
return [left, rn - left]
}
let [left, right] = pair;
if (isRegularNumber(left)) {
if (left > 9) {
pair[0] = numToPair(left);
return true;
}
} else {
if (split(left)) {
return true;
}
}
if (isRegularNumber(right)) {
if (right > 9) {
pair[1] = numToPair(right);
return true;
}
return false;
}
else {
return split(right);
}
}
function explode(n) {
let findResult = findExplosive(n);
if (findResult) {
let [explosive, parent] = findResult;
distribute(explosive, n);
if (parent[0] === explosive) {
parent[0] = 0;
} else {
parent[1] = 0;
}
return true;
}
return false;
}
function findExplosive(n, parents = []) {
if (isRegularNumber(n)) {
return false;
}
else if (parents.length == 4) {
return [n, parents[3]];
}
else {
let parentsPlus = [].concat(parents, [n]);
return findExplosive(n[0], parentsPlus)
|| findExplosive(n[1], parentsPlus);
}
}
function isPair(n) {
return Array.isArray(n);
}
function isRegularNumber(n) {
return !isPair(n);
}
function distribute(pair, root) {
function orderPairs(pair) {
let [left, right] = pair;
let leftPairs = isPair(left) ? orderPairs(left) : [];
let rightPairs = isPair(right) ? orderPairs(right) : [];
return leftPairs.concat([pair], rightPairs);
}
function updateLeft(n, pair) {
if (isRegularNumber(pair[0])) {
pair[0] = pair[0] + n;
return true;
}
else {
return false;
}
}
function updateRight(n, pair) {
if (isRegularNumber(pair[1])) {
pair[1] = pair[1] + n;
return true;
}
else {
return false;
}
}
let [lVal, rVal] = pair;
let orderedPairs = orderPairs(root);
let pairPos = orderedPairs.indexOf(pair);
let left = orderedPairs.slice(0, pairPos);
let right = orderedPairs.slice(pairPos + 1);
for (let pair of left.reverse()) {
if (updateRight(lVal, pair) || updateLeft(lVal, pair)) {
break;
}
}
for (let pair of right) {
if (updateLeft(rVal, pair) || updateRight(rVal, pair)) {
break;
}
}
}