Introduction
Advent of Code is a calendar of short programming puzzles that may be completed in any programming language and are appropriate for a range of skill levels. It is used for practice problems, university coursework, company training, speed contests, interview preparation, and peer challenge. You can learn new programming languages or hone your abilities by solving the puzzles, which are presented every day in December.
I'll be working through the Rust challenges on this blog. I'll be discussing my methodology and the code I employed to solve the puzzles.
Day 1: Reconciling Lists
The challenges this year kicked off with an interesting problem: comparing two lists of numbers to calculate differences and similarities.
Part 1: Measuring Total Distance
We are tasked with pairing the smallest numbers from two lists and measuring the differences, summing them up across all pairs.
Example: Given these two lists:
Sorting them results in:
Left List: [1, 2, 3, 3, 3, 4] Right List: [3, 3, 3, 4, 5, 9]
Pairing smallest to smallest and calculating the distance:
Solution in code
Here’s my humble solution for Part 1:
Explanation
- The function takes two sorted lists (vec_a and vec_b).
- Using zip, we pair the elements in order and compute the absolute difference for each pair.
- The differences are summed up to get the total distance.
Part 2: Calculating Similarity Score
Now, we calculate a “similarity score” by multiplying each number in the left list by the number of times it appears in the right list.
Example: Using the same lists:
- 3 appears 3 times in the right list → 3 * 3 = 9
- 4 appears 1 time in the right list → 4 * 1 = 4
- 2 appears 0 times → 2 * 0 = 0
- And so on…
The total similarity score is 31.
Solution in code
Here’s my solution for Part 2:
Explanation
- The function takes lists (vec_a and vec_b).
- We create a HashMap to store the frequency of each number in the right list.
- We iterate over the right list and increment the frequency count for each number.
- We iterate over the left list and calculate the similarity score by multiplying each number by its frequency in the right list.
Putting It All Together
The following run function ties everything together:
Im using this kinda pattern for each day of the challenge.
What it does
- Reads input from a file, parsing each line into two lists: vec_a and vec_b.
- Sorts both lists to prepare them for pairing in Part 1.
- Calculates the total distance and similarity score using the two helper functions.
- Prints the results for both parts.
Day 2: Red-Nosed Reports
This challenge was about analyzing unusual data reports to determine their safety under specific conditions. Let's break down the problem and my solution in Rust.
Part 1: Analyzing Safe Reports
Each report is a list of levels (numbers). A report is considered safe if:
- The levels are either all increasing or all decreasing.
- The difference between any two adjacent levels is at least 1 and at most 3.
Example Data:
Task: Count how many reports are safe according to these rules.
Solution in code
The is_safe function determines if a report meets the safety criteria.
Explanation
- Loops through pairs of adjacent levels using windows(2).
- Tracks the “direction” (increasing, decreasing, or no change).
- Ensures adjacent levels differ by at least 1 and at most 3.
Heres the code for part one:
Part 2: Using the Problem Dampener
The “Problem Dampener” allows the removal of one level from an otherwise unsafe report, potentially making it safe.
Example Data:
Task: Count how many reports are safe with the Problem Dampener.
Solution in code
This part adds the flexibility to remove a single level and recheck the safety of the report.
Explanation
- Iterates over each report and each level in the report.
- Creates a new list without the current level.
- Checks if the new list is safe.
- Increments the safe count if the report is safe after removing a level.
The run function ties everything together:
Reflection
The Advent of Code challenges are a great way to practice problem-solving and learn new programming languages. Rust is a powerful language with a strong emphasis on safety and performance. I enjoyed solving these puzzles in Rust and and am eager for the upcoming challenges.