2 KiB
2 KiB
AOC 2024 Day 4
Section
input = """
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
"""
lines = input |> String.trim() |> String.split("\n")
g = Grid2D.new(
lines |> Enum.at(0) |> String.trim() |> String.graphemes() |> Enum.count(),
lines |> Enum.count
)
input = input |> String.replace("\n", "")
find_word = fn
_, w, pl, w, _, _ -> pl
input, word, [pos | _] = poslist, curr, dir, f ->
["", <<next_letter::binary-size(1)>> <> _] = String.split(word, curr)
coord = Grid2D.index_to_coord(g, pos)
next = Grid2D.add(coord, dir)
if Grid2D.in_grid?(next, g) and (next_letter == String.at(input, index)) do
index = Grid2D.coord_to_index(g, next)
f.(input, word, index, [index | poslist], curr <> next_letter, dir, f)
else
nil
end
end
for [{idx, _}] <- Regex.scan(~r/X/, input, return: :index),
i <- -1..1//1,
j <- -1..1//1,
j != 0 or i != 0 do
find_word.(input, "XMAS", [idx], "X", {i, j}, find_word)
end |> Enum.reject(&(&1 == nil)) |> Enum.count()
is_xmas? =
fn grid, coord->
corners = [{1,1}, {1,-1}, {-1, -1}, {-1, 1}] |> Enum.map(&Grid2D.add(coord, &1))
has_corners? = Enum.all?(
corners,
fn p -> Grid2D.in_grid?(p, g) end
)
corner_letters = (corners |> Enum.map(fn c -> String.at(grid, Grid2D.coord_to_index(g, c)) end))
is_mas? = fn -> corner_letters |> Enum.chunk_every(2, 1, :discard) |> Enum.any?(fn i -> i == ["S", "S"] or i == ["M", "M"] end) end
has_m? = fn -> (corner_letters |> Enum.count(&(&1 == "M"))) == 2 end
has_s? = fn -> (corner_letters |> Enum.count(&(&1 == "S"))) == 2 end
has_corners? and has_m?.() and has_s?.() and is_mas?.()
end
for [{i, _}] <- Regex.scan(~r/A/, input, return: :index),
coord = Grid2D.index_to_coord(g, i) do
{ coord, is_xmas?.(input, coord)}
end |> Enum.count(fn {_, t} -> t end)