-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
106 lines (82 loc) · 2.26 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const { getInput, sum } = require('../utils')
const data = getInput(__dirname)
const formatInput = input =>
input
.trim()
.split('\n')
.reduce((acc, line) => {
const { id, results } = getGameResults(line)
acc[id] = results
return acc
}, {})
/**
* Parse a game (a line from the input) into its `id` and into objects
* representing each pull from the bag of cubes.
*
* Input: 'Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green'
* Output: {
* id: '1',
* results: [
* { blue: 3, green: 0, red: 4 },
* { blue: 0, green: 2, red: 1 },
* { blue: 6, green: 2, red: 0 }
* ],
* }
*/
const getGameResults = game => {
const [gameStr, pullsStr] = game.split(': ')
const id = gameStr.replace('Game ', '')
const pulls = pullsStr.split('; ')
const results = pulls.map(pull => {
const cubes = pull.split(', ')
const result = {
blue: 0,
green: 0,
red: 0,
}
cubes.forEach(cube => {
const [int, color] = cube.split(' ')
result[color] = parseInt(int, 10)
})
return result
})
return { id, results }
}
const BAG = {
blue: 14,
green: 13,
red: 12,
}
const isPullPossible = ({ blue, green, red }) =>
blue <= BAG.blue && green <= BAG.green && red <= BAG.red
const isGamePossible = pulls => pulls.every(isPullPossible)
function solution1(input) {
const formatted = formatInput(input)
const possibleGames = Object.entries(formatted).filter(([_key, pulls]) =>
isGamePossible(pulls)
)
const nums = possibleGames.map(([key]) => parseInt(key, 10))
return sum(nums)
}
const getMinimumRequired = pulls => {
const result = { blue: 0, green: 0, red: 0 }
for (const pull of pulls) {
if (pull.blue > result.blue) result.blue = pull.blue
if (pull.green > result.green) result.green = pull.green
if (pull.red > result.red) result.red = pull.red
}
return result
}
const getPower = ({ blue, green, red }) => blue * green * red
function solution2(input) {
const formatted = formatInput(input)
const minimums = Object.values(formatted).map(getMinimumRequired)
const powers = minimums.map(getPower)
return sum(powers)
}
// console.log(solution1(data)) // 2317
// console.log(solution2(data)) // 74804
module.exports = {
solution1,
solution2,
}