Skip to content

Commit

Permalink
feat: Day 2: Cube Conundrum
Browse files Browse the repository at this point in the history
  • Loading branch information
mariotacke committed Dec 2, 2023
1 parent 1d9a463 commit 4a7abaf
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/badges/completion.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"schemaVersion": 1,
"label": "completion",
"message": "2/50",
"message": "4/50",
"color": "yellow"
}
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

strategy:
matrix:
node-version: [14.x]
node-version: [20.x]

steps:
- uses: actions/checkout@v1
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Collect stars by solving puzzles. Two puzzles will be made available on each day
## Days

- [Day 1: Trebuchet?!](day-01-trebuchet/)
- [Day 2: TBD](day-02/)
- [Day 2: Cube Conundrum](day-02-cube-conundrum/)
- [Day 3: TBD](day-03/)
- [Day 4: TBD](day-04/)
- [Day 5: TBD](day-05/)
Expand Down
4 changes: 2 additions & 2 deletions day-01-trebuchet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ In this example, the calibration values of these four lines are `12`, `38`, `15`

Consider your entire calibration document. **What is the sum of all of the calibration values?**

### Part Two
## Part Two

Your calculation isn't quite right. It looks like some of the digits are actually **spelled out with letters**: `one`, `two`, `three`, `four`, `five`, `six`, `seven`, `eight`, and `nine` **also** count as valid "digits".

Expand All @@ -39,5 +39,5 @@ In this example, the calibration values are `29`, `83`, `13`, `24`, `42`, `14`,

**What is the sum of all of the calibration values?**

### References
## References
- https://adventofcode.com/2023/day/1
58 changes: 58 additions & 0 deletions day-02-cube-conundrum/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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?**

## Part Two

The Elf says they've stopped producing snow because they aren't getting any **water**! He isn't sure why the water stopped; however, he can show you how to get to the water source to check it out for yourself. It's just up ahead!

As you continue your walk, the Elf poses a second question: in each game you played, what is the **fewest number of cubes of each color** that could have been in the bag to make the game possible?

Again consider the example games from earlier:

```
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, the game could have been played with as few as 4 red, 2 green, and 6 blue cubes. If any color had even one fewer cube, the game would have been impossible.
* Game 2 could have been played with a minimum of 1 red, 3 green, and 4 blue cubes.
* Game 3 must have been played with at least 20 red, 13 green, and 6 blue cubes.
* Game 4 required at least 14 red, 3 green, and 15 blue cubes.
* Game 5 needed no fewer than 6 red, 3 green, and 2 blue cubes in the bag.

The **power** of a set of cubes is equal to the numbers of red, green, and blue cubes multiplied together. The power of the minimum set of cubes in game 1 is `48`. In games 2-5 it was `12`, `1560`, `630`, and `36`, respectively. Adding up these five powers produces the sum **`2286`**.

For each game, find the minimum set of cubes that must have been present. **What is the sum of the power of these sets?**

## References
- https://adventofcode.com/2023/day/2
26 changes: 26 additions & 0 deletions day-02-cube-conundrum/part1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = (input) => {
const load = {
r: 12,
g: 13,
b: 14,
};

return input
.split('\n')
.reduce((sum, line) => {
const [, id, cubes] = /Game (\d+): (.*)/.exec(line);
const sets = cubes.split('; ').map((set) => ({
r: +(/(\d+) red/.exec(set) || ['', '0'])[1],
g: +(/(\d+) green/.exec(set) || ['', '0'])[1],
b: +(/(\d+) blue/.exec(set) || ['', '0'])[1],
}));

if (sets.every((set) => load.r >= set.r &&
load.g >= set.g &&
load.b >= set.b)) {
return sum + +id;
}

return sum;
}, 0);
};
24 changes: 24 additions & 0 deletions day-02-cube-conundrum/part2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = (input) => {
return input
.split('\n')
.reduce((sum, line) => {
const [, cubes] = /: (.*)/.exec(line);
const min = cubes
.split('; ')
.reduce((min, set) => {
const colors = {
r: +(/(\d+) red/.exec(set) || ['', '0'])[1],
g: +(/(\d+) green/.exec(set) || ['', '0'])[1],
b: +(/(\d+) blue/.exec(set) || ['', '0'])[1],
};

return {
r: colors.r >= min.r ? colors.r : min.r,
g: colors.g >= min.g ? colors.g : min.g,
b: colors.b >= min.b ? colors.b : min.b,
};
}, { r: 0, g: 0, b: 0 });

return sum + min.r * min.g * min.b;
}, 0);
};
30 changes: 30 additions & 0 deletions day-02-cube-conundrum/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const assert = require('node:assert');

const part1 = require('./part1');
const part2 = require('./part2');

describe('Day 2: Cube Conundrum', () => {
it('should sum IDs of possible games', () => {
const input =
`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`;

assert.strictEqual(part1(input), 8);
});

describe('Part Two', () => {
it('should sum power of minium cube sets', () => {
const input =
`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`;

assert.strictEqual(part2(input), 2286);
});
});
});

0 comments on commit 4a7abaf

Please sign in to comment.