Metadata
-
Date
-
Tagged
-
Part of series
- Advent of Code 2024 Day 1
- Advent of Code 2024 Day 2
- Advent of Code 2024 Day 3
- Advent of Code 2024 Day 4
- Advent of Code 2024 Day 5
- Advent of Code 2024 Day 6
- Advent of Code 2024 Day 7
- Advent of Code 2024 Day 8
- Advent of Code 2024 Day 9
- Advent of Code 2024 Day 10
- Advent of Code 2024 Day 11
- Advent of Code 2024 Day 12
- Advent of Code 2024 Day 13
- Advent of Code 2024 Day 14
- Advent of Code 2024 Day 15
- Advent of Code 2024 Day 16
- Advent of Code 2024 Day 17
- Advent of Code 2024 Day 18
- Advent of Code 2024 Day 19
- Advent of Code 2024 Day 20
- Advent of Code 2024 Day 21
- Advent of Code 2024 Day 22
- Advent of Code 2024 Day 23
- Advent of Code 2024 Day 24
- Advent of Code 2024 Day 25
-
Older post
-
Newer post
Advent of Code 2024 Day 4
Day 4: Ceres Search
https://adventofcode.com/2024/day/4
Today’s location to search for the chief historian is familiar (Again! Makes sense, you’ve been to many interesting places.). You remember visiting it.
As is tradition, the an elf there asks for your help with something.
With a word search puzzle!
That’s your input, the puzzle.
An example input looks like this:
You can form words in all 8 directions, horizontals, verticals, and diagonals. Words can overlap.
Part 1
You might have already guessed by looking at the example input, the word you have to search for is “XMAS”.
The question asks how many times “XMAS” appears in the puzzle. Remember overlapping letters is allowed!
I coded two option per part again.
A good ol reliable for
loop, and a fancy-schmancy iterator chain.
In both methods, I look for an “X” first. Then look in all directions, and if I find “MAS”, I increment the counter.
This brings out my love-hate relationship with number data types that can’t become negative. On one hand, they prevent so many issue, but on the other LET ME CALCULATE OFFSETS IN PEACE.
The iterator example is commented better, so if you want an idea of which steps are executed, it might be better to look at that first.
A teeny tiny lil helping constant
I chose to represent the directions in a list of offsets. Each item in the list is what should happen to the row and column indices to take a step in that direction.
Option 1: for
loop
Option 2: Iterator chain
Part 2
You misunderstood, this isn’t an “XMAS” puzzle, it’s an X-”MAS” puzzle.
You are supposed to count the amount of times “MAS” appears in an X pattern, like this:
The dots here are characters that are irrelevant.
The question asks how many times “MAS” appears in an X pattern in the puzzle.
For this part, I start by looking for an “A”. Then I note the 4 adjacent corner letters.
If they are in a valid order, I increment the count.
The iterator chain example is commented better again.
Option 1: for
loop
Option 2: Iterator chain
Final code
Linelength, nice.