-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_CubeConundrum.js
More file actions
43 lines (33 loc) · 1.05 KB
/
02_CubeConundrum.js
File metadata and controls
43 lines (33 loc) · 1.05 KB
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
import { readInput, textToArray } from '../lib/utils.js';
const list = textToArray(readInput());
const part1 = (list) => {
const res = list.reduce((acc, [id, colors]) => {
if (colors["red"] <= 12 && colors["green"] <= 13 && colors["blue"] <= 14) {
return acc + id;
}
return acc;
}, 0);
return res;
};
const part2 = (list) => {
const res = list.reduce((acc, [, colors]) => {
return acc + (colors["red"] * colors["green"] * colors["blue"]);
}, 0);
return res;
};
const parsed = list.map(l => {
const [game, sets] = l.split(":");
const [, i] = game.split(" ");
const colors = sets
.split(/,|;/)
.map(f => f.trim().split(" "))
.reduce((colors, [a, color]) => {
if (!(color in colors)) colors[color] = [];
colors[color].push(parseInt(a));
return colors;
}, {});
const maxColors = Object.fromEntries(Object.entries(colors).map(([c, v]) => [c, Math.max(...v)]));
return [parseInt(i), maxColors];
});
console.log(part1(parsed));
console.log(part2(parsed));