-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
89 lines (75 loc) · 2.29 KB
/
index.ts
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
import { readFile } from 'fs/promises';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const input = await readFile(join(__dirname, './input.txt'), 'utf8');
// #region Part One
const maxColorAmounts = {
red: 12,
green: 13,
blue: 14,
};
const getPartOneAnswer = (input: string) => {
const games = input.split('\n');
const possible = [];
mainLoop: for (const game of games) {
if (!game.trim().length) continue;
const [gameName, cubeSetsStr] = game.split(':');
const cubeSets = cubeSetsStr.trim().split(';');
for (const cubeSet of cubeSets) {
const cubes = cubeSet.split(',').map((x) => x.trim());
for (const [amountStr, color] of cubes.map((coloredCubes) =>
coloredCubes.split(' ')
) as Array<[string, keyof typeof maxColorAmounts]>) {
const amount = Number(amountStr);
if (amount > maxColorAmounts[color]) {
// Impossible game, we move on.
continue mainLoop;
}
}
}
// We've reached this far into the loop and haven't moved on, game is possible.
const [, gameId] = gameName.split(' ');
possible.push(Number(gameId));
}
return possible.reduce((acc, id) => (acc += id), 0);
};
console.log('Part 1 answer:', getPartOneAnswer(input));
// #endregion
// #region Part Two
const getPartTwoAnswer = (input: string) => {
const games = input.split('\n');
const powers = [];
for (const game of games) {
if (!game.trim().length) continue;
const minColorAmounts = {
red: 0,
green: 0,
blue: 0,
};
const [, cubeSetsStr] = game.split(':');
const cubeSets = cubeSetsStr.trim().split(';');
for (const cubeSet of cubeSets) {
const cubes = cubeSet.split(',').map((x) => x.trim());
for (const [amountStr, color] of cubes.map((coloredCubes) =>
coloredCubes.split(' ')
) as Array<[string, keyof typeof minColorAmounts]>) {
const amount = Number(amountStr);
if (amount > minColorAmounts[color]) {
minColorAmounts[color] = amount;
}
}
}
const mins = Object.values(minColorAmounts);
powers.push(
mins.slice(1).reduce((acc, min) => {
acc *= min;
return acc;
}, mins[0])
);
}
return powers.reduce((acc, power) => (acc += power), 0);
};
console.log('Part 2 answer:', getPartTwoAnswer(input));
// #endregion