Skip to content

Commit

Permalink
Add day 2 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
lundgren2 committed Dec 8, 2023
1 parent 5c84006 commit a54ed59
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/2023/day02/solution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,28 @@ export function part1(input: string): number {
// [ true, true, false, false, true ]
return validGames.reduce((acc, val, i) => (val ? acc + (i + 1) : acc), 0);
}

export function part2(input: string): number {
const games = input.split('\n');

const validGames = games.map((game, i) => {
const maxCubes = {red: 0, green: 0, blue: 0};
const subsets = game.split(': ')[1].split('; ');

subsets.forEach(subset => {
const sets = subset.split(', ');
return sets.map(set => {
const [amount, color] = set.split(' ');
const currentMax = maxCubes[color as keyof typeof maxCubes];
if (Number(amount) > currentMax) {
maxCubes[color as keyof typeof maxCubes] = Number(amount);
}
});
});

return maxCubes.red * maxCubes.green * maxCubes.blue;
});

const result = validGames.reduce((acc, val) => acc + val, 0);
return result;
}
11 changes: 10 additions & 1 deletion src/2023/day02/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import example from './example.txt';
import input from './input.txt';
import {part1} from './solution';
import {part1, part2} from './solution';

describe('Advent of Code 2023 - Day 2', () => {
describe('part 1', () => {
Expand All @@ -11,4 +11,13 @@ describe('Advent of Code 2023 - Day 2', () => {
expect(part1(input)).toBe(2685);
});
});

describe('part 2', () => {
test('test round', () => {
expect(part2(example)).toBe(2286);
});
test('solution', () => {
expect(part2(input)).toBe(83707);
});
});
});

0 comments on commit a54ed59

Please sign in to comment.