Day Two
This commit is contained in:
parent
578ff6d52e
commit
64a7e8fa79
|
@ -1,3 +1,4 @@
|
|||
node_modules
|
||||
inputs
|
||||
.idea
|
||||
.vscode
|
|
@ -0,0 +1,67 @@
|
|||
import { calculateMinimumCubePowers, calculatePossibleGames, parseGame } from "./day_two";
|
||||
|
||||
const partOneInput = `Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
|
||||
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
|
||||
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
|
||||
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
|
||||
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green`
|
||||
|
||||
describe('Day Two', () => {
|
||||
it('should parse a game', () => {
|
||||
const games = partOneInput.split('\n').map(parseGame);
|
||||
expect(games).toEqual([
|
||||
{
|
||||
id: 1,
|
||||
rounds: [
|
||||
{ blue: 3, green: 0, red: 4 },
|
||||
{ blue: 6, red: 1, green: 2 },
|
||||
{ blue: 0, red: 0, green: 2 }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
rounds: [
|
||||
{ blue: 1, green: 2, red: 0 },
|
||||
{ blue: 4, red: 1, green: 3 },
|
||||
{ blue: 1, red: 0, green: 1 }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
rounds: [
|
||||
{ blue: 6, green: 8, red: 20 },
|
||||
{ blue: 5, red: 4, green: 13 },
|
||||
{ blue: 0, red: 1, green: 5 }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
rounds: [
|
||||
{ blue: 6, green: 1, red: 3 },
|
||||
{ blue: 0, red: 6, green: 3 },
|
||||
{ blue: 15, red: 14, green: 3 }
|
||||
]
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
rounds: [
|
||||
{ blue: 1, green: 3, red: 6 },
|
||||
{ blue: 2, red: 1, green: 2 },
|
||||
]
|
||||
}
|
||||
]);
|
||||
});
|
||||
|
||||
it('should calculate which games should only have been possible with the number of cubes', () => {
|
||||
const games = partOneInput.split('\n').map(parseGame);
|
||||
expect(calculatePossibleGames(games, {
|
||||
blue: 14,
|
||||
red: 12,
|
||||
green: 13
|
||||
})).toEqual(8);
|
||||
});
|
||||
it('should find the minimum cubes in a set and sum the powers of the cubes', () => {
|
||||
const games = partOneInput.split('\n').map(parseGame);
|
||||
expect(calculateMinimumCubePowers(games)).toEqual(2286);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,70 @@
|
|||
import fs from 'fs';
|
||||
|
||||
export type Cubes = {
|
||||
blue: number;
|
||||
red: number;
|
||||
green: number;
|
||||
}
|
||||
|
||||
export type Game = {
|
||||
id: number;
|
||||
rounds: Cubes[];
|
||||
}
|
||||
|
||||
export const parseGame = (input: string): Game => {
|
||||
const [gameId, roundsInput] = input.split(':');
|
||||
const id = parseInt(gameId.split(' ')[1], 10);
|
||||
|
||||
const rounds = roundsInput.split(';').map(round => {
|
||||
return round.trim().split(',').map(cube => cube.trim()).reduce((total, cube) => {
|
||||
const [amount, color] = cube.split(' ');
|
||||
total[color as keyof Cubes] = parseInt(amount, 10);
|
||||
return total;
|
||||
}, {blue: 0, green: 0, red: 0} as Cubes);
|
||||
});
|
||||
return {
|
||||
id,
|
||||
rounds
|
||||
}
|
||||
}
|
||||
|
||||
export const calculatePossibleGames = (games: Game[], limits: Cubes): number => {
|
||||
const possibleGames = games.filter(game => {
|
||||
return game.rounds.every(round => {
|
||||
return Object.keys(round).every(color => {
|
||||
return round[color as keyof Cubes] <= limits[color as keyof Cubes];
|
||||
});
|
||||
});
|
||||
});
|
||||
return possibleGames.reduce((total, game) => total + game.id, 0);
|
||||
}
|
||||
|
||||
const minimumCubesRequiredForGame = (game: Game): Cubes => {
|
||||
return game.rounds.reduce((total, round) => {
|
||||
Object.keys(round).forEach(color => {
|
||||
total[color as keyof Cubes] = Math.max(total[color as keyof Cubes], round[color as keyof Cubes]);
|
||||
});
|
||||
return total;
|
||||
}, {blue: 0, green: 0, red: 0} as Cubes);
|
||||
}
|
||||
|
||||
export const calculateMinimumCubePowers = (games: Game[]): number => {
|
||||
const minimumCubes = games.map(minimumCubesRequiredForGame);
|
||||
return minimumCubes.reduce((total, cubes) => {
|
||||
return total + (cubes.blue * cubes.green * cubes.red);
|
||||
}, 0);
|
||||
}
|
||||
|
||||
export const runDayTwo = () => {
|
||||
const input = fs.readFileSync('./inputs/day_two_input.txt', 'utf8').split('\n').filter(Boolean);
|
||||
const games = input.map(parseGame);
|
||||
const limits = {
|
||||
blue: 14,
|
||||
red: 12,
|
||||
green: 13
|
||||
};
|
||||
console.log(calculatePossibleGames(games, limits));
|
||||
console.log(calculateMinimumCubePowers(games));
|
||||
}
|
||||
|
||||
runDayTwo();
|
|
@ -0,0 +1,5 @@
|
|||
/** @type {import('ts-jest').JestConfigWithTsJest} */
|
||||
module.exports = {
|
||||
preset: 'ts-jest',
|
||||
testEnvironment: 'node',
|
||||
};
|
|
@ -9,7 +9,10 @@
|
|||
"author": "",
|
||||
"license": "ISC",
|
||||
"devDependencies": {
|
||||
"@types/jest": "^29.5.10",
|
||||
"@types/node": "^20.10.1",
|
||||
"jest": "^29.7.0",
|
||||
"ts-jest": "^29.1.1",
|
||||
"ts-node": "^10.9.1",
|
||||
"typescript": "^5.3.2"
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue