This year, I’m diving into Advent of Code for the first time, and I couldn’t be more excited! As a newcomer to this coding challenge, I wasn’t sure what to expect, but Day 1 proved to be the perfect introduction. The puzzles were engaging yet approachable, providing a fantastic opportunity to flex my problem-solving muscles while building confidence in my skills.
Day 1 Puzzle: Reconciling Historians’ Notes
The scenario for Day 1 was intriguing: two mismatched lists of location IDs needed to be reconciled to help a group of Elvish Senior Historians track down their missing Chief Historian. The task was split into two parts:
- Calculate the total distance between the two lists by pairing the numbers from smallest to largest and summing their absolute differences.
- Determine a similarity score by calculating how many times each number from the first list appears in the second and summing weighted contributions.
Pseudocode for My Solutions
Here’s how I approached each part of the puzzle:
Part 1: Total Distance
1. Parse Input
Split the raw input into two separate lists of integers.
Function: parse_input(input_string)
Initialize list1 and list2
For each line in input_string:
If line is not empty:
Split the line into two numbers
Append the first number to list1
Append the second number to list2
Return list1 and list2
2. Sort the Lists:
Sorting ensures that the smallest numbers from each list are paired together.
Function: sort_list(list)
Return list sorted in ascending order
3. Compute Total Distance:
For each index, compute the absolute difference between paired numbers and sum them.
Function: calculate_total_distance(list1, list2)
Initizialize total_distance to 0
For i from 0 to length of list1:
Calculate absolute difference between list1[i] and list2[i]
Add difference to total_distance
Return total_distance
Part 2: Similarity Score
Calculate Occurrence Similarity:
1. Count how many times each number in the first list appears in the second list and create a list.
Function: similarity_list(list1, list2)
Initialize similarity_list as empty
For each num1 in list1:
Count occurrences of num1 in list 2
Append count to similarity_list
Return similarity_list
2. Use these counts to compute the weighted similarity score.
Function: similarity_score(list1, similarity_list)
Initialize similarity_score to 0
For i from 0 to length of list1:
similarity_score += list1[i] * similarity_list{i}
Return similarity_score
Implementing with TDD
For both parts, I used Test-Driven Development (TDD) to structure my work:
1. Write Tests First:
Before writing any functional code, I created tests for each function. For example:
- parse_input should correctly parse the input into two lists
- calculate_total_distance should compute the correct total for paired absolute differences
- similarity_list should return the correct count of occurrences for each number
Example test for part 2:
def test_similarity_score():
list1 = [1, 2, 3, 4, 5]
list2 = [1, 2, 1, 1, 0]
expected_similarity_score = 1 + 4 + 3 + 4 + 0
assert similarity_score(list1, similarity_list(list1, list2)) == expected_similarity_score
2. Develop Code to Pass Tests:
Functions were implemented iteratively, ensuring each passed its corresponding tests before moving on.
3. Refactor for Clarity:
Once all tests passed, I revisited the code to clean up and optimize where possible.
4. Continuous Testing
I ran tests frequently to catch errors early and verify that new changes didn’t break existing functionality.
Lessons from Day 1
- TDD Keeps You Focused: Writing tests first clarified my goals and made debugging easier.
- Breaking Down Problems Helps: Writing pseudocode and taking the problem step by step reduced overwhelm.
- Python is a Great Tool for Problem-Solving: Its readability and powerful standard library made implementation smooth.
Final Thoughts
Participating in Advent of Code for the first time has already been both challenging and rewarding. Day 1 taught me the value of a systematic approach, and I can’t wait to see what the rest of the month holds. Whether you’re a seasoned Advent of Code participant or just starting like me, I’d love to hear about your experiences!
Are you taking part in Advent of Code this year? Let me know in the comments!