Advent of Code 2024 - Day 4: "Ceres Search"
Joshua Wortz
Cybersecurity Strategist | IT Leader | Master's Student in Cyber Security Operations & Leadership | Driving Innovation & Resilience
Day 4 of #AdventOfCode 2024 took us to the Ceres monitoring station, where the challenge revolved around an intriguing word search puzzle. Titled "Ceres Search," this task tested spatial reasoning, pattern matching, and efficient coding techniques. Let’s dive into the details of the puzzle and how I approached it.
?? The Challenge: Word Searches and Hidden Patterns
The Elf at the Ceres monitoring station presented us with a word search puzzle containing two parts:
Part 1: Find all occurrences of the word XMAS in the grid. Words could appear horizontally, vertically, diagonally, forwards, or backwards—overlapping characters were fair game.
Part 2: Instead of searching for XMAS, the goal shifted to finding a pattern where the word MAS appeared twice, forming an "X" shape within a 3x3 sub grid.
?? My Approach: Breaking Down the Problem
You can find my complete solutions on GitHub here:
Here’s how I tackled each part:
Part 1:
Key Functionality: The recursive search function was the heart of this solution. It handled boundary checks and matched the word letter-by-letter in the specified direction. The overall solution efficiently identified all occurrences of XMAS.
Here’s a snippet from my solution:
def search(x, y, index, dx, dy):
"""Recursive search for the word."""
if index == len(word): # Found the complete word
return 1
if not is_valid(x, y) or grid[x][y] != word[index]: # Out of bounds or mismatch
return 0
return search(x + dx, y + dy, index + 1, dx, dy)
领英推荐
Part 2:
Key Functionality: A helper function was used to check each subgrid. By leveraging NumPy's capabilities, I kept the logic concise and the execution fast.
Here’s a snippet from my solution:
def check_subarray(subarray):
check = ["MAS", "SAM"]
s1 = "".join(subarray.ravel()[[0, 4, 8]])
s2 = "".join(subarray.ravel()[[2, 4, 6]])
return (s1 in check) and (s2 in check)
?? Key Takeaways: Lessons from Day 4
?? Reflections on "Ceres Search"
This puzzle highlighted the importance of modular code design and effective use of libraries like NumPy. Tackling both parts required a mix of spatial reasoning and programming, making this one of the most satisfying challenges so far.
I particularly enjoyed the problem’s transition from a straightforward search in Part 1 to a more intricate pattern-matching exercise in Part 2. It’s these progressive layers of complexity that make Advent of Code such an engaging experience.
?? What’s Next?
Day 4 was a blast, and I can’t wait to see what Day 5 brings! How did you approach today’s challenge? Did you use similar strategies, or did you take a completely different route? Share your thoughts and solutions in the comments—I’d love to hear from you!
For those following along, you can explore my code for Day 4 here:
Let’s keep learning and solving together. ????
#AdventOfCode2024 #CodingChallenges #ProblemSolving #Python #NumPy