-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.js
50 lines (40 loc) · 1.44 KB
/
day2.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
import { asLines } from './util.js';
const getGames = () => {
return asLines('../input/day2_a.txt').map(line => toGame(line));
};
const toGame = (line) => {
const [gameStr, setsStr] = line.split(':');
const id = parseInt(gameStr.split(' ')[1]);
const sets = setsStr.split(';').map(setStr => {
const set = {};
const colors = setStr.split(',');
colors.forEach(color => {
const [count, colorName] = color.trim().split(' ');
set[colorName] = parseInt(count);
});
return set;
});
return { id, sets };
};
const isPossibleGame = (game, red, green, blue) => {
return game.sets.filter(set => !isPossibleSet(set, red, green, blue)).length === 0;
};
const isPossibleSet = (set, red, green, blue) => {
return (set.red || 0) <= red && (set.green || 0) <= green && (set.blue || 0) <= blue;
}
const minimumSet = (sets) => {
const red = sets.reduce((min, set) => Math.max(min, set.red || 0), 0);
const green = sets.reduce((min, set) => Math.max(min, set.green || 0), 0);
const blue = sets.reduce((min, set) => Math.max(min, set.blue || 0), 0);
return { red, green, blue };
};
const sum1 = getGames()
.filter(game => isPossibleGame(game, 12, 13, 14))
.map(game => game.id)
.reduce((a,b) => a + b, 0);
console.log(`part 1: ${sum1}`);
const sum2 = getGames()
.map(game => minimumSet(game.sets))
.map(minSet => minSet.red * minSet.green * minSet.blue)
.reduce((a,b) => a + b, 0);
console.log(`part 2: ${sum2}`);