-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday1question2.ts
More file actions
37 lines (28 loc) · 876 Bytes
/
day1question2.ts
File metadata and controls
37 lines (28 loc) · 876 Bytes
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
import { readFileSync } from "fs";
let inputs: number[];
const rawData = readFileSync("./day1inputs.txt", "utf8");
inputs = rawData.split("\n").map(Number);
const windowedInputs = inputs.reduce<number[]>(
(partialReduction, currentValue, currentIndex, array): number[] => {
if (currentIndex + 2 >= array.length) {
return partialReduction;
}
partialReduction.push(
currentValue + array[currentIndex + 1] + array[currentIndex + 2]
);
return partialReduction;
},
[]
);
const numberOfIncreases = windowedInputs.reduce(
(partialReduction, currentValue, currentIndex, array) => {
if (currentIndex === 0) {
return partialReduction;
}
return currentValue > array[currentIndex - 1]
? partialReduction + 1
: partialReduction;
},
0
);
console.log(numberOfIncreases);