diff --git a/src/2023/day02.js b/src/2023/day02.js new file mode 100644 index 00000000..8a9d557b --- /dev/null +++ b/src/2023/day02.js @@ -0,0 +1,41 @@ +function parse(input) { + const lines = input.split('\n'); + return lines.map(line => { + let [game, ...rounds] = line.split(/[:; ] /); + game = +game.split(' ')[1]; + rounds = rounds.map(round => { + return round.split(', ').map(d => { + const [count, color] = d.split(' '); + return { count: +count, color }; + }); + }); + return { game, rounds }; + }); +} + +export function part1(input) { + const games = parse(input); + const good = games.filter(({ rounds }) => + rounds.flat().every(({ color, count }) => { + return ( + (color === 'red' && count <= 12) || + (color === 'green' && count <= 13) || + (color === 'blue' && count <= 14) + ); + }), + ); + return good.reduce((acc, { game }) => acc + game, 0); +} + +export function part2(input) { + const games = parse(input); + const powers = games.map(({ rounds }) => { + const dice = rounds.flat(); + const max = ['red', 'blue', 'green'].map(c => { + const counts = dice.filter(({ color }) => color === c); + return Math.max(...counts.map(({ count }) => count), 0); + }); + return max[0] * max[1] * max[2]; + }); + return powers.reduce((acc, power) => acc + power, 0); +} diff --git a/src/2023/day02.spec.js b/src/2023/day02.spec.js new file mode 100644 index 00000000..ee31113d --- /dev/null +++ b/src/2023/day02.spec.js @@ -0,0 +1,46 @@ +import { part1, part2 } from './day02.js'; +import readInput from '../utils/read-input.js'; + +const input = readInput(import.meta.url); + +describe('day02 2023', () => { + describe('part1', () => { + it('should work for part 1 examples', () => { + expect( + part1( + [ + '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', + ].join('\n'), + ), + ).toEqual(8); + }); + + it('should work for part 1 input', () => { + expect(part1(input)).toEqual(2593); + }); + }); + + describe('part2', () => { + it('should work for part 2 examples', () => { + expect( + part2( + [ + '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', + ].join('\n'), + ), + ).toEqual(2286); + }); + + it('should work for part 2 input', () => { + expect(part2(input)).toEqual(54699); + }); + }); +}); diff --git a/src/2023/day02.txt b/src/2023/day02.txt new file mode 100644 index 00000000..01bcaa25 --- /dev/null +++ b/src/2023/day02.txt @@ -0,0 +1,101 @@ +Game 1: 13 green, 3 red; 4 red, 9 green, 4 blue; 9 green, 10 red, 2 blue +Game 2: 3 red, 8 green, 1 blue; 4 green, 11 blue, 2 red; 3 blue, 2 red, 6 green; 5 green, 15 blue, 1 red; 2 blue, 2 red, 5 green; 12 blue, 7 green, 2 red +Game 3: 1 red, 9 green, 3 blue; 8 green, 4 red, 11 blue; 6 red, 10 blue; 6 green, 6 red, 12 blue; 2 blue, 11 green, 7 red; 12 blue, 9 green, 8 red +Game 4: 7 red, 2 green, 1 blue; 12 green; 12 green +Game 5: 15 red, 3 green, 1 blue; 6 red, 2 blue, 2 green; 3 green, 3 red, 1 blue; 2 blue, 13 red, 5 green; 2 green, 15 red, 2 blue +Game 6: 3 blue, 15 red, 1 green; 8 green, 5 red, 6 blue; 9 green, 5 blue, 6 red; 9 green, 3 blue, 9 red; 10 green, 14 red, 2 blue +Game 7: 6 green, 1 red, 11 blue; 6 green, 1 red, 3 blue; 4 green, 20 blue; 2 red, 5 blue, 4 green; 10 green, 17 blue +Game 8: 10 blue, 9 green, 10 red; 9 green, 1 red; 8 red, 9 green, 9 blue; 5 green, 3 red, 7 blue +Game 9: 7 blue, 1 red; 5 red, 4 green; 4 green, 6 red, 5 blue; 2 green, 4 blue; 3 green, 6 blue, 4 red; 1 green, 3 red, 3 blue +Game 10: 2 red, 2 green, 2 blue; 10 blue, 2 red, 1 green; 2 green, 9 blue, 3 red +Game 11: 8 red, 4 blue, 1 green; 3 red; 1 green; 2 green, 3 blue +Game 12: 10 red, 2 green, 4 blue; 4 red, 2 green; 1 blue, 1 red, 1 green; 10 red, 1 green, 5 blue +Game 13: 20 blue, 9 green, 7 red; 13 red, 13 blue, 16 green; 17 blue, 6 red, 6 green; 1 red, 1 blue, 9 green; 9 blue, 18 green, 7 red +Game 14: 6 blue, 14 red; 9 red, 8 blue; 2 red, 1 green, 8 blue; 3 blue, 1 green, 9 red; 8 blue, 2 green, 1 red +Game 15: 3 red, 1 blue, 5 green; 2 red, 3 green; 5 red, 5 green, 1 blue; 2 green, 6 red; 4 red, 1 blue; 6 red, 1 blue, 1 green +Game 16: 5 blue, 7 red, 2 green; 7 red, 12 blue; 10 blue, 11 green, 5 red; 11 red, 11 blue, 10 green +Game 17: 3 red, 7 blue; 1 blue, 14 green, 4 red; 11 blue, 4 red, 11 green; 18 blue, 5 red, 11 green; 18 blue, 1 red, 8 green +Game 18: 8 green, 2 red, 6 blue; 8 blue, 11 green; 2 red, 11 blue, 9 green +Game 19: 11 red, 9 green, 3 blue; 19 green, 9 red, 2 blue; 19 green, 4 blue, 4 red; 1 green, 11 red; 10 red, 2 green, 4 blue +Game 20: 2 blue, 3 red, 1 green; 1 red, 3 green; 7 blue, 1 green, 4 red; 1 red, 8 blue, 7 green; 6 blue, 3 red; 5 red, 3 blue, 7 green +Game 21: 1 green, 1 blue, 10 red; 1 green, 5 red, 8 blue; 11 red, 4 blue; 6 blue, 6 red +Game 22: 6 blue, 6 green; 8 green, 15 blue; 8 green, 3 blue, 1 red; 11 blue, 2 red, 7 green +Game 23: 1 green, 3 blue, 7 red; 4 red, 1 green, 2 blue; 3 red, 2 blue, 2 green +Game 24: 4 green, 8 blue, 4 red; 2 green, 9 blue; 4 green, 1 red; 2 green, 5 blue, 1 red; 2 blue, 3 red, 3 green; 6 blue +Game 25: 7 blue; 15 blue, 5 red; 6 blue, 12 red; 1 green, 17 red; 13 blue, 5 red; 17 red +Game 26: 1 blue, 3 green, 7 red; 9 red, 4 green, 1 blue; 1 red, 2 green, 1 blue; 11 red, 3 green; 10 red, 4 green, 2 blue; 6 red, 4 green +Game 27: 4 blue, 6 red; 2 blue, 8 red, 1 green; 3 blue, 3 red; 2 red, 1 blue; 1 green, 3 blue, 6 red +Game 28: 1 red, 7 blue, 7 green; 2 green, 1 red, 4 blue; 8 green, 2 red; 2 red, 7 blue, 5 green; 12 green, 5 blue, 2 red; 1 red, 1 green, 2 blue +Game 29: 10 green, 3 red, 6 blue; 9 green, 6 blue, 4 red; 3 red, 2 blue, 17 green +Game 30: 8 blue; 15 blue, 1 red; 10 green, 2 red, 13 blue +Game 31: 10 green, 2 blue, 7 red; 2 green, 1 blue; 1 blue, 15 green, 2 red; 7 green, 2 blue; 3 blue, 6 green, 8 red; 6 red, 1 blue +Game 32: 2 blue, 2 red, 11 green; 10 green, 2 red, 1 blue; 1 green, 2 blue; 2 red, 9 green, 2 blue; 2 blue, 1 green; 5 green, 1 blue, 2 red +Game 33: 8 red, 6 blue; 2 green, 3 red, 2 blue; 1 green, 13 red, 18 blue +Game 34: 7 blue, 5 green; 5 green, 8 blue; 13 blue, 15 red, 2 green +Game 35: 1 blue, 2 green; 9 green; 4 red, 14 green; 1 red, 1 blue, 17 green +Game 36: 2 red, 14 green, 4 blue; 13 green, 3 blue; 1 blue, 7 green, 2 red; 4 blue, 9 green; 1 green, 3 blue, 1 red; 2 red, 4 blue, 10 green +Game 37: 2 blue, 7 green, 5 red; 5 green, 2 blue; 6 blue, 11 red +Game 38: 6 green, 6 red; 9 red, 10 green; 2 blue, 8 green, 8 red +Game 39: 10 red, 3 blue; 5 green, 3 red; 5 red, 7 green +Game 40: 5 red, 14 green, 2 blue; 5 red, 7 blue, 12 green; 2 green, 4 red; 1 red, 16 green, 3 blue; 16 green, 4 red, 7 blue; 9 green, 2 red +Game 41: 4 red, 3 green, 2 blue; 13 green, 6 blue; 2 red, 14 green, 1 blue; 7 blue, 2 red, 14 green +Game 42: 4 red; 1 blue, 5 red; 1 green, 6 red; 1 red, 1 blue; 3 blue, 8 red +Game 43: 7 blue, 16 red, 1 green; 2 red, 6 green, 1 blue; 5 green, 3 red; 5 green, 9 blue, 2 red; 3 red, 9 blue, 4 green; 7 red, 9 blue +Game 44: 2 red, 2 green; 5 red, 1 blue, 8 green; 7 green, 3 blue, 5 red +Game 45: 8 blue, 16 red; 8 blue; 4 blue, 1 green, 8 red +Game 46: 11 green, 9 blue, 1 red; 8 green, 7 blue; 10 blue, 1 red, 1 green; 12 green, 10 blue +Game 47: 3 green, 6 red, 1 blue; 2 blue, 2 green, 12 red; 3 red, 2 green, 1 blue +Game 48: 3 red, 3 green, 3 blue; 3 red, 4 green, 2 blue; 2 green, 7 red, 1 blue; 2 red, 3 blue, 5 green +Game 49: 5 red, 7 blue, 5 green; 10 red, 4 green, 7 blue; 9 red, 17 green; 6 green, 1 red, 2 blue; 7 green, 8 blue, 5 red +Game 50: 2 red, 4 green, 16 blue; 4 blue, 3 red, 8 green; 4 blue, 2 red, 6 green +Game 51: 16 green, 10 red, 14 blue; 8 red, 4 blue, 12 green; 14 green, 7 blue; 6 red, 20 green, 3 blue +Game 52: 1 red, 1 blue, 1 green; 9 green, 9 red; 4 green, 13 red; 7 red, 11 green; 4 red, 1 blue; 8 green, 3 red, 1 blue +Game 53: 4 green, 11 blue; 9 green, 2 red; 6 red, 18 green, 13 blue; 6 red, 2 blue, 14 green +Game 54: 1 green, 1 red, 1 blue; 2 green, 4 blue; 4 blue, 5 green; 3 blue, 1 red, 10 green +Game 55: 8 blue, 2 red, 3 green; 9 red, 11 blue; 1 green, 12 blue, 4 red; 3 green, 17 red; 3 red, 3 green, 15 blue; 7 blue, 7 red, 2 green +Game 56: 3 blue, 13 green; 9 green, 2 blue; 1 red, 2 blue, 16 green +Game 57: 6 blue, 4 red; 3 green, 6 red; 2 red, 3 blue, 3 green; 8 red, 5 blue +Game 58: 4 red, 15 green, 5 blue; 1 red, 16 blue, 14 green; 2 green, 17 blue, 6 red; 20 blue, 3 red, 7 green; 17 green, 1 red, 12 blue +Game 59: 3 blue, 14 red; 5 green, 10 red, 2 blue; 2 blue, 5 red, 6 green +Game 60: 4 red, 1 blue, 1 green; 15 blue; 8 green, 14 blue, 4 red; 9 blue, 3 green, 4 red; 4 green, 2 red, 11 blue; 4 blue, 7 green +Game 61: 5 green, 9 blue, 16 red; 4 blue, 12 green, 4 red; 17 red, 7 green, 5 blue; 19 blue, 12 red, 17 green; 8 green, 13 red +Game 62: 13 green, 1 red, 7 blue; 9 blue, 1 red, 4 green; 14 green, 2 red, 2 blue; 3 green +Game 63: 6 green; 7 red, 3 blue, 8 green; 5 blue, 1 green, 6 red; 6 green, 6 red, 2 blue; 8 green, 2 blue +Game 64: 16 blue, 1 red, 2 green; 4 green, 1 blue, 6 red; 6 green, 2 blue, 2 red; 17 blue; 1 red; 13 blue, 6 green, 1 red +Game 65: 8 red, 3 green, 7 blue; 6 blue, 8 red, 2 green; 2 blue, 3 green, 17 red +Game 66: 2 blue, 3 green, 3 red; 3 red, 2 blue; 5 red, 4 green, 3 blue; 1 blue, 3 green; 2 red, 1 green, 1 blue; 2 blue, 4 green +Game 67: 2 red, 3 blue, 15 green; 2 blue, 2 red, 17 green; 4 blue, 3 red, 2 green; 6 red; 3 red, 8 green +Game 68: 7 red, 1 blue, 12 green; 17 red, 1 green; 10 red, 8 green; 16 red, 5 green, 2 blue; 4 red, 1 blue, 8 green; 8 green, 7 red, 2 blue +Game 69: 17 green, 9 red, 2 blue; 1 blue, 14 green, 3 red; 9 red, 12 green, 2 blue; 11 green, 2 blue, 7 red +Game 70: 1 green, 8 blue, 2 red; 2 red, 10 green, 1 blue; 1 red, 12 green, 6 blue; 9 green, 4 blue, 4 red; 2 red, 6 green; 3 red, 8 green, 6 blue +Game 71: 1 red, 5 blue; 12 blue, 3 red; 3 red, 2 green, 4 blue; 5 blue, 3 green, 1 red +Game 72: 11 red, 6 blue; 1 red, 1 blue, 1 green; 2 blue, 7 red; 18 blue, 3 red; 1 green, 1 blue, 12 red +Game 73: 4 red, 2 blue, 1 green; 3 red; 5 red, 1 blue; 4 blue, 6 red +Game 74: 2 red; 2 red, 5 green; 4 green, 1 red, 1 blue; 1 blue, 5 green, 5 red; 7 red, 1 blue, 3 green; 8 red, 1 blue, 6 green +Game 75: 13 blue, 2 red, 2 green; 2 red, 9 blue; 2 red, 9 blue, 5 green +Game 76: 2 red, 3 green, 18 blue; 2 red, 11 green, 5 blue; 6 green, 8 blue, 2 red; 4 blue; 7 green, 14 blue +Game 77: 5 blue, 8 red, 1 green; 2 blue, 5 green, 12 red; 3 red, 4 blue +Game 78: 1 blue, 2 green, 16 red; 2 red, 3 green; 1 red, 4 green, 2 blue; 11 red; 2 green, 12 red, 2 blue; 11 red, 5 green, 3 blue +Game 79: 10 green, 3 blue, 2 red; 8 red, 3 blue, 8 green; 5 green, 3 red, 11 blue; 9 green, 16 blue +Game 80: 1 red, 4 blue; 6 green, 1 red; 6 green, 3 blue, 1 red; 6 green, 2 red; 7 green, 1 blue; 2 red, 2 blue, 2 green +Game 81: 10 blue, 4 red, 4 green; 5 green, 1 red, 7 blue; 11 blue, 8 green, 2 red; 8 green, 2 red +Game 82: 12 green, 1 red, 3 blue; 6 red, 1 blue; 16 green, 3 red, 4 blue; 8 blue; 7 blue, 7 green, 2 red; 4 red, 19 green +Game 83: 4 red, 4 blue, 3 green; 8 blue, 4 green, 6 red; 6 green, 7 blue, 6 red; 11 red, 6 green, 7 blue +Game 84: 11 red, 2 green, 2 blue; 20 green, 2 blue, 13 red; 15 red, 6 green, 3 blue; 17 green, 7 red +Game 85: 3 blue, 5 green, 2 red; 12 green, 2 blue, 1 red; 7 blue, 6 green, 5 red; 11 red, 2 blue, 17 green; 11 blue, 11 red, 17 green; 18 green, 9 red, 13 blue +Game 86: 1 blue, 14 red; 4 green, 1 blue, 3 red; 2 green, 1 blue, 13 red; 1 green, 1 blue, 10 red +Game 87: 2 red, 5 green, 4 blue; 3 blue, 9 red, 6 green; 7 blue, 9 red, 11 green; 10 green, 11 red, 9 blue; 7 green, 12 red, 4 blue; 5 blue, 1 red, 7 green +Game 88: 11 red, 1 green; 9 blue, 4 green, 7 red; 10 red, 4 green, 1 blue; 4 green, 1 red, 1 blue; 10 blue, 1 red, 3 green; 2 green, 12 blue, 11 red +Game 89: 3 green, 3 blue; 1 red, 7 green, 9 blue; 8 red, 11 blue, 11 green; 2 green, 6 blue, 5 red; 5 blue, 9 green +Game 90: 3 blue, 10 red, 2 green; 2 blue; 8 red +Game 91: 2 red, 10 green, 2 blue; 9 blue; 8 green, 5 red, 10 blue; 7 green, 6 blue, 5 red; 1 green, 6 red, 12 blue; 1 red, 4 green, 3 blue +Game 92: 12 blue, 5 red, 2 green; 4 blue, 1 red, 3 green; 6 red, 6 blue; 1 blue, 8 red, 6 green; 6 blue, 3 red, 2 green; 7 green, 4 red, 1 blue +Game 93: 3 blue; 8 blue; 3 blue, 2 red; 2 red, 1 green +Game 94: 5 red, 7 blue, 6 green; 15 red, 7 blue, 4 green; 6 blue, 1 red, 2 green; 7 green, 4 blue, 17 red; 12 red, 5 green, 1 blue +Game 95: 7 blue, 11 red, 9 green; 10 red, 6 blue, 7 green; 6 blue, 6 red, 7 green +Game 96: 2 red, 1 green, 3 blue; 3 blue, 1 green; 1 green, 1 blue; 1 red, 1 blue; 1 green, 1 red, 4 blue +Game 97: 6 red, 1 blue, 7 green; 2 blue, 5 red, 7 green; 8 red, 3 blue, 6 green; 6 green, 1 red, 3 blue; 5 red, 2 blue, 14 green; 3 green, 6 red, 6 blue +Game 98: 9 red, 14 blue; 19 red, 4 blue; 11 red, 17 blue; 14 blue, 1 green, 18 red +Game 99: 1 green, 1 red, 12 blue; 2 green, 4 red, 14 blue; 4 blue, 6 red; 10 red, 2 green, 1 blue +Game 100: 5 red, 9 green, 2 blue; 9 blue, 6 green, 1 red; 8 blue, 7 green, 3 red +