fix: Resolve Day04 grid processing and X-MAS pattern matching

This commit is contained in:
Ruidy (aider) 2024-12-04 13:22:24 +01:00 committed by Ruidy
parent 6aec2849a1
commit bacce02f21
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
3 changed files with 59 additions and 10 deletions

View file

@ -69,3 +69,41 @@ For the given puzzle input, calculate and return the total number of times the w
- Input will be a rectangular grid of letters. - Input will be a rectangular grid of letters.
Good luck helping the little Elf! 🌟 Good luck helping the little Elf! 🌟
## Part 2 Challenge
The Elf looks quizzically at you. Did you misunderstand the assignment?
Looking for the instructions, you flip over the word search to find that this
isn't actually an XMAS puzzle; it's an X-MAS puzzle in which you're supposed to
find two MAS in the shape of an X. One way to achieve that is like this:
```txt
M.S
.A.
M.S
```
Irrelevant characters have again been replaced with `.` in the above diagram.
Within the X, each MAS can be written forwards or backwards.
Here's the same example from before, but this time all of the X-MASes have been
kept instead:
```txt
.M.S......
..A..MSMS.
.M.S.MAA..
..A.ASMSM.
.M.S.M....
..........
S.S.S.S.S.
.A.A.A.A..
M.M.M.M.M.
..........
```
In this example, an X-MAS appears 9 times.
Flip the word search from the instructions back over to the word search side and
try again. How many times does an X-MAS appear?

View file

@ -3,10 +3,10 @@ defmodule AdventCode2024.Solutions.Day04 do
@default_input "priv/inputs/day04/input.txt" @default_input "priv/inputs/day04/input.txt"
def count_word_occurrences(grid, word) do def count_word_occurrences(grid, word) when is_list(grid) do
grid grid
|> Enum.map(&String.graphemes/1) |> Enum.map(&String.graphemes/1)
|> find_word(word) |> find_word(String.graphemes(word))
end end
defp find_word(grid, word) do defp find_word(grid, word) do
@ -51,9 +51,8 @@ defmodule AdventCode2024.Solutions.Day04 do
end) end)
end end
defp find_word?(grid, word, {row, col}, {d_row, d_col}) do defp find_word?(grid, word_chars, {row, col}, {d_row, d_col}) do
word word_chars
|> String.codepoints()
|> Enum.with_index() |> Enum.with_index()
|> Enum.all?(fn {char, i} -> |> Enum.all?(fn {char, i} ->
new_row = row + i * d_row new_row = row + i * d_row
@ -86,7 +85,7 @@ defmodule AdventCode2024.Solutions.Day04 do
def solve_part2(_input_file) do def solve_part2(_input_file) do
end end
def count_x_mas(grid, word \\ "MAS") do def count_x_mas(grid) do
directions = [ directions = [
# Down # Down
{1, 0}, {1, 0},
@ -98,18 +97,20 @@ defmodule AdventCode2024.Solutions.Day04 do
{1, -1} {1, -1}
] ]
grid grid_chars = Enum.map(grid, &String.graphemes/1)
grid_chars
|> Enum.with_index() |> Enum.with_index()
|> Enum.reduce(0, fn {row, row_idx}, acc -> |> Enum.reduce(0, fn {row, row_idx}, acc ->
row row
|> Enum.with_index() |> Enum.with_index()
|> Enum.reduce(acc, fn {_, col_idx}, acc_inner -> |> Enum.reduce(acc, fn {_, col_idx}, acc_inner ->
acc_inner + count_xmas_from(grid, word, {row_idx, col_idx}, directions) acc_inner + count_xmas_from(grid_chars, {row_idx, col_idx}, directions)
end) end)
end) end)
end end
defp count_xmas_from(grid, _word, {row, col}, directions) do defp count_xmas_from(grid, {row, col}, directions) do
directions directions
|> Enum.reduce(0, fn direction, acc -> |> Enum.reduce(0, fn direction, acc ->
if find_xmas?(grid, {row, col}, direction) do if find_xmas?(grid, {row, col}, direction) do
@ -132,7 +133,13 @@ defmodule AdventCode2024.Solutions.Day04 do
# Check if all positions contain the correct letters for "MAS" # Check if all positions contain the correct letters for "MAS"
Enum.with_index(positions) Enum.with_index(positions)
|> Enum.all?(fn {{r, c}, idx} -> |> Enum.all?(fn {{r, c}, idx} ->
within_bounds?(grid, r, c) && Enum.at(Enum.at(grid, r), c) == String.at("MAS", idx) within_bounds?(grid, r, c) &&
case idx do
0 -> Enum.at(Enum.at(grid, r), c) == "M"
1 -> Enum.at(Enum.at(grid, r), c) == "A"
2 -> Enum.at(Enum.at(grid, r), c) == "S"
3 -> Enum.at(Enum.at(grid, r), c) == "S"
end
end) end)
end end
end end

View file

@ -20,4 +20,8 @@ defmodule AdventCode2024.Solutions.Day04Test do
actual_count = Day04.count_word_occurrences(@example_grid, "XMAS") actual_count = Day04.count_word_occurrences(@example_grid, "XMAS")
assert actual_count == expected_count assert actual_count == expected_count
end end
test "finds all occurrences of X-MAS in the grid" do
assert Day04.count_x_mas(@example_grid) == 9
end
end end