-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.js
85 lines (71 loc) · 2.1 KB
/
02.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
import path from 'node:path';
import {fileURLToPath} from 'node:url';
import {readInput, sum} from '../../../util.js';
const directoryPath = path.dirname(fileURLToPath(import.meta.url));
const input = await readInput(directoryPath);
const gameRegex = /Game (\d+): (.+)/;
const games = input
.trim()
.split('\n')
.map(line => {
const regexMatch = line.match(gameRegex);
const gameId = Number(regexMatch[1]);
const gameCubeSets = regexMatch[2]
.split(';')
.map(set => {
const splittedSet = set.split(',');
const cubes = splittedSet.map(cube => {
const splittedCube = cube.trim().split(' ');
return {[splittedCube[1]]: Number(splittedCube[0])};
});
const combinedCubes = {};
for (const cube of cubes) {
const color = Object.keys(cube)[0];
const number = cube[color];
combinedCubes[color] = number;
}
return combinedCubes;
});
return {
gameId,
gameCubeSets,
};
});
const maxCubeCheck = {
red: 12,
green: 13,
blue: 14,
};
const possibleGameIds = [];
for (const game of games) {
let gameIsImpossible = false;
for (const gameCubeSet of game.gameCubeSets) {
for (const color of Object.keys(gameCubeSet)) {
if (gameCubeSet[color] > maxCubeCheck[color]) {
gameIsImpossible = true;
break;
}
}
if (gameIsImpossible) break;
}
if (gameIsImpossible) continue;
possibleGameIds.push(game.gameId);
}
const sumPossibleGamesIds = sum(possibleGameIds);
console.log('Sum possible game IDs:', sumPossibleGamesIds);
let sumFewestPossibleCubes = 0;
for (const game of games) {
const currentFewestPossibleCubes = {};
for (const gameCubeSet of game.gameCubeSets) {
for (const color of Object.keys(gameCubeSet)) {
currentFewestPossibleCubes[color] = currentFewestPossibleCubes[color] ? Math.max(
currentFewestPossibleCubes[color],
gameCubeSet[color],
) : gameCubeSet[color];
}
}
const powerOfCubes = Object.values(currentFewestPossibleCubes)
.reduce((accumulator, current) => accumulator * current, 1);
sumFewestPossibleCubes += powerOfCubes;
}
console.log('The sum of fewest possible cubes for each game:', sumFewestPossibleCubes);