-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.ts
67 lines (52 loc) · 1.47 KB
/
day2.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
import Solution from '../../model/solution.ts';
export default class Day2 extends Solution {
/* ---- Day Init ---- */
year = 2023;
day = 2;
example = false;
/* ------------------ */
maxCubes: any = {
red: 12,
green: 13,
blue: 14,
};
solve(input: string) {
const possibleGames: number[] = [];
const powers: number[] = [];
for (const game of input.split('\n')) {
const parts = game.split(': ');
const gameId = parseInt(parts[0].split(' ')[1], 10);
const rounds = parts[1].trim().split('; ');
let possible = true;
const minCubes: any = {
red: 0,
green: 0,
blue: 0,
};
for (const round of rounds) {
const cubes = round.split(', ');
for (const cube of cubes) {
const cubeParts = cube.split(' ');
const amount = parseInt(cubeParts[0], 10);
const color = cubeParts[1];
if (minCubes[color] < amount) {
minCubes[color] = amount;
}
if (this.maxCubes[color] < amount) {
possible = false;
}
}
}
powers.push(
Object.values(minCubes)
.map((v) => parseInt(`${v}`, 10))
.reduce((partialSum, a) => partialSum * a, 1),
);
if (possible) {
possibleGames.push(gameId);
}
}
this.a = possibleGames.reduce((partialSum, a) => partialSum + a, 0);
this.b = powers.reduce((partialSum, a) => partialSum + a, 0);
}
}