-
Notifications
You must be signed in to change notification settings - Fork 0
/
day02.js
54 lines (42 loc) · 1.19 KB
/
day02.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
const readFrom = require("./read-input");
const args = process.argv;
const input = readFrom(args);
if (!input) return;
const possibleMax = {
red: 12,
green: 13,
blue: 14,
};
const partOneResult = input.split("\n").reduce((idSums, line, i) => {
const cubes = line
.split(": ")[1]
.split("; ")
.map((set) => set.split(", ").map((cube) => cube.split(" ")))
.flat();
for (let [val, color] of cubes) {
if (possibleMax[color] < parseInt(val)) {
return idSums;
}
}
return idSums + i + 1;
}, 0);
console.log(partOneResult);
const partTwoResult = input.split("\n").reduce((powerSums, line) => {
const possibleMin = {
red: 1,
green: 1,
blue: 1,
};
const cubes = line
.split(": ")[1]
.split("; ")
.map((set) => set.split(", ").map((cube) => cube.split(" ")))
.flat();
for (let [val, color] of cubes) {
possibleMin[color] =
parseInt(val) > possibleMin[color] ? parseInt(val) : possibleMin[color];
}
const power = Object.values(possibleMin).reduce((a, b) => a * b);
return powerSums + power;
}, 0);
console.log(partTwoResult);