# Solving Cube Conundrum in Rust (Advent of Code)

Advent of Code is a site that provides programming puzzles every Christmas. It’s often used as a way to learn a new programming language or prepare for technical interviews. In this post, I’d like to explore the “Cube Conundrum” problem from 2023. Each problem on Advent of Code typically has two parts.

Before looking at my solution, you might want to try the problem yourself. If you get stuck, you can always refer back to this post for guidance. Find the problem statement for “Cube Conundrum” here.

## Part 1

## Story - Part 1

--- 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 Islandand 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. […]

The story begins with you being catapulted onto “Snow Island,” where you meet an Elf. This Elf wants to play a game with you.

Here’s how a single game works:

- The Elf has a bag of cubes that are either red, green, or blue.
- The Elf will reach into the bag, grab a random handful of cubes, and show them to you. He does this several times per game.

You play multiple games and record the information from these games along with a game ID.

**Example:**

### Problem Statement

The Elf wants to know which of these games would be possible if you had **only 12 red cubes, 13 green cubes, and 14 blue cubes.**

We need to collect all the `game id`

of valid games and then return the **sum** of these games.

In the given example, games 1, 2, and 5 could have occurred with the specified bag configuration. However, game 3 is not possible because the Elf displayed 20 red cubes at once, and game 4 is also impossible since the Elf displayed 15 blue cubes at once. Adding up the IDs of the feasible games results in a total of 8.

### How to solve this problem.

To solve this problem there are steps we need to do:

- Import and parse the given input file
- Filter out valid games
- Collect the
`game id`

of these games - Return the sum of the
`game id`

of all games.

## Detailed Steps

**Step 1: Import and Parse the Input File**

- In Rust, we can use the
`std::fs`

module to read from the file system API. - We store the input file in a variable.
- Then we split the input file by lines since each line represents a single game to be processed.

**Step 2: Parse Each Line to Retrieve the game id and List of Sets**

- We need to go through each line to extract the
`game id`

and a list of`sets`

in each game. - Each set contains the number of
`red`

,`green`

, and/or`blue`

cubes.

**Step 3: Filter Out Valid Games Using a Match Statement**

- The only valid colors are
`red`

,`green`

, and`blue`

. - The amount of each color should be less than or equal to:
- 12 for
`red`

- 13 for
`green`

- 14 for
`blue`

- 12 for
- We use a
`match`

statement to filter out the valid games.

**Step 4: Filter Out Valid Games and Sum the game id**

- We collect the
`game id`

of the games that satisfy the conditions. - Finally, we return the sum of the
`game id`

of all valid games.

The code above will output the sum of the `game id`

of all valid games. In the given example, the output will be `8`

.

## Part 2

## Story - Part 2

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?

In Part 2, we need to find the minimum number of cubes of each color that could have been in the bag to make each game possible. Then, we calculate the product of these minimums for each game and return the sum of these products.

**For the input:**

### Problem Statement

Given the list of games, we need to:

- Determine the fewest number of red, green, and blue cubes needed for each game.
- Compute the product of these minimum counts for each game.
- Return the sum of these products.

In the given example that would be `2286`

.

### How to solve this problem

First, we parse the input (like in part 1) and extract the number of each color cube needed in every game. Then, we find the maximum number of cubes needed for each color in each game. Finally, we compute the product of these values and sum them for all games.

The code above will output the sum of the product of the minimum number of cubes of each color needed for each game. In the given example, the output will be `2286`

.

## Conclusion

In this post, we solved the “Cube Conundrum” problem from Advent of Code 2023 using Rust. I learned basics of the rust language and iterators methods like `map`

, `filter`

, `fold`

, and `sum`

. I hope you found this post helpful and informative. If you have any questions or suggestions, feel free to write me via email or twitter.

Happy coding!