Skip to content

Commit 86e51fe

Browse files
committed
ref: Reorganize Day 2 files
1 parent 37e652e commit 86e51fe

File tree

11 files changed

+323
-205
lines changed

11 files changed

+323
-205
lines changed

lib/puzzles/2023/day01/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Day 1: Trebuchet?!
22

3-
## Part 1
3+
## Part One
44

55
Something is wrong with global snow production, and you've been selected to take a look. The Elves have even given you a map; on it, they've used stars to mark the top fifty locations that are likely to be having problems.
66

@@ -29,7 +29,7 @@ Consider your entire calibration document. **What is the sum of all of the calib
2929

3030
Your puzzle answer was `54450`.
3131

32-
## Part 2
32+
## Part Two
3333

3434
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".
3535

lib/puzzles/2023/day02.rb

Lines changed: 0 additions & 194 deletions
This file was deleted.

lib/puzzles/2023/day02/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Day 2: Cube Conundrum
2+
3+
## Part One
4+
5+
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.
6+
7+
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?
8+
9+
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.
10+
11+
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.
12+
13+
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`).
14+
15+
For example, the record of a few games might look like this:
16+
17+
```
18+
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
19+
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
20+
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
21+
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
22+
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
23+
```
24+
25+
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.
26+
27+
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**?
28+
29+
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`**.
30+
31+
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?**
32+
33+
Your puzzle answer was `2278`.
34+
35+
## Part Two
36+
37+
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!
38+
39+
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?
40+
41+
Again consider the example games from earlier:
42+
43+
```
44+
Game 1: 3 blue, 4 red; 1 red, 2 green, 6 blue; 2 green
45+
Game 2: 1 blue, 2 green; 3 green, 4 blue, 1 red; 1 green, 1 blue
46+
Game 3: 8 green, 6 blue, 20 red; 5 blue, 4 red, 13 green; 5 green, 1 red
47+
Game 4: 1 green, 3 red, 6 blue; 3 green, 6 red; 3 green, 15 blue, 14 red
48+
Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green
49+
```
50+
51+
- 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.
52+
- Game 2 could have been played with a minimum of 1 red, 3 green, and 4 blue cubes.
53+
- Game 3 must have been played with at least 20 red, 13 green, and 6 blue cubes.
54+
- Game 4 required at least 14 red, 3 green, and 15 blue cubes.
55+
- Game 5 needed no fewer than 6 red, 3 green, and 2 blue cubes in the bag.
56+
57+
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`**.
58+
59+
For each game, find the minimum set of cubes that must have been present. **What is the sum of the power of these sets?**
60+
61+
Your puzzle answer was `67953`.

lib/puzzles/2023/day02/day02.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
require_relative "part1"
4+
require_relative "part2"
5+
6+
module AdventOfCode
7+
module Puzzles2023
8+
##
9+
# Advent of Code 2023 - Day 2
10+
# https://adventofcode.com/2023/day/2
11+
# {include:file:lib/puzzles/2023/day02/README.md}
12+
module Day02
13+
end
14+
end
15+
end

lib/puzzles/2023/day02/game.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
module AdventOfCode
4+
module Puzzles2023
5+
module Day02
6+
##
7+
# Game class to store the game id and its sets.
8+
class Game
9+
##
10+
# @return [Integer] game id
11+
attr_reader :id
12+
13+
##
14+
# @return [Array<Set>] sets of cubes
15+
attr_reader :sets
16+
17+
##
18+
# @param id [Integer] game id
19+
# @param sets [Array<Set>] sets of cubes
20+
def initialize(id:, sets:)
21+
@id = id
22+
@sets = sets
23+
end
24+
25+
##
26+
# Check if the game is valid given the number of blue, green,
27+
# and red cubes.
28+
#
29+
# @param blue [Integer] number of blue cubes
30+
# @param green [Integer] number of green cubes
31+
# @param red [Integer] number of red cubes
32+
#
33+
# @return [Boolean] true if the game is valid, false otherwise
34+
def valid?(blue:, green:, red:)
35+
# A game is valid only if every set has the same or less
36+
# number of blue, green, and red cubes as the set it is
37+
sets.each do |set|
38+
return false if set.blue > blue
39+
return false if set.green > green
40+
return false if set.red > red
41+
end
42+
true
43+
end
44+
end
45+
end
46+
end
47+
end
File renamed without changes.

0 commit comments

Comments
 (0)