From 4f8b77beb7020bfcefc4407c7094f3794c6d7804 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Sat, 2 Dec 2023 18:05:52 +0100 Subject: [PATCH] advent of code 2023 day 2 --- 2023/02/assignment.md | 28 ++++++++++++++++++++++++++++ 2023/02/impl.js | 37 +++++++++++++++++++++++++++++++++++++ 2023/02/input.txt | 5 +++++ 2023/02/output.txt | 1 + 2023/02/test.js | 16 ++++++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 2023/02/assignment.md create mode 100644 2023/02/impl.js create mode 100644 2023/02/input.txt create mode 100644 2023/02/output.txt create mode 100644 2023/02/test.js diff --git a/2023/02/assignment.md b/2023/02/assignment.md new file mode 100644 index 0000000..e9968ed --- /dev/null +++ b/2023/02/assignment.md @@ -0,0 +1,28 @@ +# --- Day 2: Cube Conundrum --- + +You're launched high into the atmosphere! The apex of your trajectory just barely reaches the surface of a large island floating in the sky. You gently land in a fluffy pile of leaves. It's quite cold, but you don't see much snow. An Elf runs over to greet you. + +The Elf explains that you've arrived at Snow Island and apologizes for the lack of snow. He'll be happy to explain the situation, but it's a bit of a walk, so you have some time. They don't get many visitors up here; would you like to play a game in the meantime? + +As you walk, the Elf shows you a small bag and some cubes which are either red, green, or blue. Each time you play this game, he will hide a secret number of cubes of each color in the bag, and your goal is to figure out information about the number of cubes. + +To get information, once a bag has been loaded with cubes, the Elf will reach into the bag, grab a handful of random cubes, show them to you, and then put them back in the bag. He'll do this a few times per game. + +You play several games and record the information from each game (your puzzle input). Each game is listed with its ID number (like the 11 in Game 11: ...) followed by a semicolon-separated list of subsets of cubes that were revealed from the bag (like 3 red, 5 green, 4 blue). + +For example, the record of a few games might look like this: + +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 + +In game 1, three sets of cubes are revealed from the bag (and then put back again). The first set is 3 blue cubes and 4 red cubes; the second set is 1 red cube, 2 green cubes, and 6 blue cubes; the third set is only 2 green cubes. + +The Elf would first like to know which games would have been possible if the bag contained only 12 red cubes, 13 green cubes, and 14 blue cubes? + +In the example above, games 1, 2, and 5 would have been possible if the bag had been loaded with that configuration. However, game 3 would have been impossible because at one point the Elf showed you 20 red cubes at once; similarly, game 4 would also have been impossible because the Elf showed you 15 blue cubes at once. If you add up the IDs of the games that would have been possible, you get 8. + +Determine which games would have been possible if the bag had been loaded with only 12 red cubes, 13 green cubes, and 14 blue cubes. What is the sum of the IDs of those games? + diff --git a/2023/02/impl.js b/2023/02/impl.js new file mode 100644 index 0000000..ca0d35e --- /dev/null +++ b/2023/02/impl.js @@ -0,0 +1,37 @@ + + +module.exports.solve = function(input, bagConfig) { + + // I don't think modeling the game like this would have been needed + // However, it helps my thinking as the domain's model equals the model explained in the assignment. + const parseGame = (line) => { + // example line: Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue + const grabs = line.split(":")[1].split(";").map(descr => { + const descrSeparated = descr.split(",") // e.g. " 1 blue" or " 3 green" + const descrToInt = (color) => { + const found = descrSeparated.find(d => d.indexOf(color) >= 0) + return found === undefined ? 0 : parseInt(found.replace(color, "").trim()) + } + + return { + 'r': descrToInt("red"), + 'g': descrToInt("green"), + 'b': descrToInt("blue") + } + }) + return { + id: parseInt(line.split(":")[0].replace("Game ", "")), + grabs + } + } + const isGamePossible = (game) => { + return !game.grabs.some(grab => grab.r > bagConfig.r || grab.g > bagConfig.g || grab.b > bagConfig.b) + } + + return input.split('\n') + .filter(line => line.length > 1) + .map(parseGame) + .filter(isGamePossible) + .map(game => game.id) + .reduce((a, b) => a + b) +} diff --git a/2023/02/input.txt b/2023/02/input.txt new file mode 100644 index 0000000..295c36d --- /dev/null +++ b/2023/02/input.txt @@ -0,0 +1,5 @@ +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 diff --git a/2023/02/output.txt b/2023/02/output.txt new file mode 100644 index 0000000..301160a --- /dev/null +++ b/2023/02/output.txt @@ -0,0 +1 @@ +8 \ No newline at end of file diff --git a/2023/02/test.js b/2023/02/test.js new file mode 100644 index 0000000..cd2a769 --- /dev/null +++ b/2023/02/test.js @@ -0,0 +1,16 @@ +const test = require('node:test') +const assert = require('node:assert').strict; +const { readFileSync } = require('fs') +const { solve } = require('./impl.js') + +test('with only 12 red cubes, 13 green cubes, and 14 blue cubes, output is 8', (t) => { + const input = readFileSync('input.txt').toString() + const expected_output = parseInt(readFileSync('output.txt').toString()) + + const out = solve(input, { + 'r': 12, + 'g': 13, + 'b': 14 + }) + assert.equal(out, expected_output) +})