-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
110 lines (99 loc) · 2.32 KB
/
index.ts
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
import run from "aocrunner";
import "../utils/index.js";
const parseInput = (rawInput: string) => {
return rawInput.split("\n").map(line => {
return line.split(" ").map(num => Number.parseInt(num))
});
}
const generateDeltaSequence = (sequence: number[]) => {
const next = [];
for (let i = 0; i < sequence.length - 1; i++) {
next.push(sequence[i + 1] - sequence[i]);
}
return next;
}
const sumSequence = (sequence: number[]) => {
return sequence.reduce((accum, next) => accum + next, 0);
}
const part1 = (rawInput: string) => {
const input = parseInput(rawInput);
const sequenceNexts = input.map(sequence => {
const deltaSeqs = [sequence];
while (sumSequence(deltaSeqs[deltaSeqs.length - 1]) !== 0) {
deltaSeqs.push(generateDeltaSequence(deltaSeqs[deltaSeqs.length - 1]));
}
return deltaSeqs.reduce((accum, curr) => {
return accum + curr[curr.length - 1];
}, 0);
});
return sequenceNexts.reduce((accum, curr) => accum + curr, 0);
};
const part2 = (rawInput: string) => {
const input = parseInput(rawInput);
const sequenceNexts = input.map(sequence => {
const deltaSeqs = [sequence];
while (sumSequence(deltaSeqs[deltaSeqs.length - 1]) !== 0) {
deltaSeqs.push(generateDeltaSequence(deltaSeqs[deltaSeqs.length - 1]));
}
deltaSeqs.reverse();
return deltaSeqs.reduce((accum, curr, index) => {
if (index !== 0) {
accum = curr[0] - accum;
}
return accum;
}, 0);
});
return sequenceNexts.reduce((accum, curr) => accum + curr, 0);
};
run({
part1: {
tests: [
{
input: `0 3 6 9 12 15`,
expected: 18
},
{
input: `1 3 6 10 15 21`,
expected: 28,
},
{
input: `10 13 16 21 30 45`,
expected: 68,
},
{
input: `
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45`,
expected: 114,
},
],
solution: part1,
},
part2: {
tests: [
{
input: `0 3 6 9 12 15`,
expected: -3
},
{
input: `1 3 6 10 15 21`,
expected: 0,
},
{
input: `10 13 16 21 30 45`,
expected: 5,
},
{
input: `
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45`,
expected: 2,
},
],
solution: part2,
},
trimTestInputs: true,
onlyTests: false,
});