From 02502b07880f35473d614a18930455ad5b1c5477 Mon Sep 17 00:00:00 2001 From: "Ruidy (aider)" Date: Wed, 4 Dec 2024 13:22:24 +0100 Subject: [PATCH] fix: Resolve Day04 grid processing and X-MAS pattern matching --- lib/advent_code2024/solutions/day04/READme.md | 38 +++++++++++++++++++ lib/advent_code2024/solutions/day04/day4.ex | 27 ++++++++----- .../solutions/day04/day4_test.exs | 4 ++ 3 files changed, 59 insertions(+), 10 deletions(-) diff --git a/lib/advent_code2024/solutions/day04/READme.md b/lib/advent_code2024/solutions/day04/READme.md index 1ff6f85..e3e3a84 100644 --- a/lib/advent_code2024/solutions/day04/READme.md +++ b/lib/advent_code2024/solutions/day04/READme.md @@ -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. 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? diff --git a/lib/advent_code2024/solutions/day04/day4.ex b/lib/advent_code2024/solutions/day04/day4.ex index d5d8e77..27f8c56 100644 --- a/lib/advent_code2024/solutions/day04/day4.ex +++ b/lib/advent_code2024/solutions/day04/day4.ex @@ -3,10 +3,10 @@ defmodule AdventCode2024.Solutions.Day04 do @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 |> Enum.map(&String.graphemes/1) - |> find_word(word) + |> find_word(String.graphemes(word)) end defp find_word(grid, word) do @@ -51,9 +51,8 @@ defmodule AdventCode2024.Solutions.Day04 do end) end - defp find_word?(grid, word, {row, col}, {d_row, d_col}) do - word - |> String.codepoints() + defp find_word?(grid, word_chars, {row, col}, {d_row, d_col}) do + word_chars |> Enum.with_index() |> Enum.all?(fn {char, i} -> new_row = row + i * d_row @@ -86,7 +85,7 @@ defmodule AdventCode2024.Solutions.Day04 do def solve_part2(_input_file) do end - def count_x_mas(grid, word \\ "MAS") do + def count_x_mas(grid) do directions = [ # Down {1, 0}, @@ -98,18 +97,20 @@ defmodule AdventCode2024.Solutions.Day04 do {1, -1} ] - grid + grid_chars = Enum.map(grid, &String.graphemes/1) + + grid_chars |> Enum.with_index() |> Enum.reduce(0, fn {row, row_idx}, acc -> row |> Enum.with_index() |> 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 - defp count_xmas_from(grid, _word, {row, col}, directions) do + defp count_xmas_from(grid, {row, col}, directions) do directions |> Enum.reduce(0, fn direction, acc -> 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" Enum.with_index(positions) |> 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 diff --git a/test/advent_code2024/solutions/day04/day4_test.exs b/test/advent_code2024/solutions/day04/day4_test.exs index 0fd5457..11bb967 100644 --- a/test/advent_code2024/solutions/day04/day4_test.exs +++ b/test/advent_code2024/solutions/day04/day4_test.exs @@ -20,4 +20,8 @@ defmodule AdventCode2024.Solutions.Day04Test do actual_count = Day04.count_word_occurrences(@example_grid, "XMAS") assert actual_count == expected_count end + + test "finds all occurrences of X-MAS in the grid" do + assert Day04.count_x_mas(@example_grid) == 9 + end end