-
Notifications
You must be signed in to change notification settings - Fork 0
/
day09.js
88 lines (77 loc) · 2.12 KB
/
day09.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
import { read, readEx, time } from './util.js';
import Vec from './util/Vec.js';
await import('./util/bad-but-great.js');
await readEx();
await read();
let data;
// 10:06
const partOne = () => {
// Can't believe that I got this on the first try >:D
const bubble_up = (steps) => {
if (steps.length == 1) {
return;
}
let next = steps.at(-2);
next.push(next.at(-1) + steps.at(-1).at(-1));
bubble_up(steps.slice(0, steps.length - 1));
};
let sum = 0;
for (const line of data.lines()) {
let steps = [];
let nums = line.split` `.nums();
steps.push(nums);
while (nums.some(s => s !== 0)) {
let step = [];
for (let i = 1; i < nums.length; ++i) {
step.push(nums[i] - nums[i - 1]);
}
steps.push(nums = step);
}
steps.at(-1).push(0);
bubble_up(steps);
console.log(steps);
sum += steps[0].at(-1);
}
console.log({ sum });
};
// 15:00
const partTwo = () => {
const bubble_up = (steps) => {
if (steps.length == 1) {
return;
}
let next = steps.at(-2);
console.log({next});
next.unshift(next[0] - steps.at(-1)[0]);
bubble_up(steps.slice(0, steps.length - 1));
};
let sum = 0;
for (const line of data.lines()) {
let steps = [];
let nums = line.split` `.nums();
steps.push(nums);
while (nums.some(s => s !== 0)) {
let step = [];
for (let i = 1; i < nums.length; ++i) {
step.push(nums[i] - nums[i - 1]);
}
steps.push(nums = step);
}
steps.at(-1).unshift(0);
bubble_up(steps);
console.log(steps);
sum += steps[0][0];
}
console.log({ sum });
};
if (process.argv[2]) {
console.log('--- --- Running Sample Data --- ---');
data = await readEx(); // Sample Data
time(partOne);
time(partTwo);
} else {
console.log('--- --- Running Real Data --- ---');
data = await read(); // Real Data
time(partOne);
time(partTwo);
}