-
Notifications
You must be signed in to change notification settings - Fork 0
/
day04.js
53 lines (40 loc) · 1.34 KB
/
day04.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
const readFrom = require("./read-input");
const args = process.argv;
const input = readFrom(args);
if (!input) return;
const lines = input.split("\n");
const partOneResult = lines.reduce((sum, line) => {
const [winningRaw, numbersRaw] = line.split(" | ");
const winningNumbers = winningRaw
.split(": ")[1]
.split(" ")
.filter((n) => n !== "");
const points = numbersRaw.split(" ").reduce((points, number) => {
if (winningNumbers.includes(number)) {
return points > 0 ? points + points : 1;
}
return points;
}, 0);
return sum + points;
}, 0);
console.log(partOneResult);
const partTwoResult = lines
.reduce((copies, line, lineI) => {
const [winningRaw, numbersRaw] = line.split(" | ");
const winningNumbers = winningRaw
.split(": ")[1]
.split(" ")
.filter((n) => n !== "");
const points = numbersRaw.split(" ").reduce((points, number) => {
if (winningNumbers.includes(number)) {
return points + 1;
}
return points;
}, 0);
for (let i = 0; i < points; i++) {
copies[lineI + i + 1] += copies[lineI];
}
return copies;
}, Array(lines.length).fill(1))
.reduce((sum, copy) => sum + copy);
console.log(partTwoResult);