-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathday_04.ex
More file actions
67 lines (54 loc) · 1.47 KB
/
day_04.ex
File metadata and controls
67 lines (54 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
defmodule AdventOfCode.Y2024.Day04 do
@moduledoc """
--- Day 4: Ceres Search ---
Problem Link: https://adventofcode.com/2024/day/4
Difficulty: s
Tags: grid
"""
alias AdventOfCode.Helpers.{InputReader, Transformers}
alias Yog.Builder.Grid
def input, do: InputReader.read_from_file(2024, 4)
def run(input \\ input()) do
grid = parse_to_map(input)
{run_1(grid), run_2(grid)}
end
defp run_1(grid) do
for {{x, y}, "X"} <- grid,
{dx, dy} <- Grid.queen(),
xmas?(grid, x, y, dx, dy),
reduce: 0 do
acc -> acc + 1
end
end
defp run_2(grid) do
for {{x, y}, "A"} <- grid,
x_mas?(grid, x, y),
reduce: 0 do
acc -> acc + 1
end
end
defp xmas?(grid, x, y, dx, dy) do
grid[{x + dx, y + dy}] == "M" and
grid[{x + 2 * dx, y + 2 * dy}] == "A" and
grid[{x + 3 * dx, y + 3 * dy}] == "S"
end
defp x_mas?(grid, x, y) do
d1 = {grid[{x - 1, y - 1}], grid[{x + 1, y + 1}]}
d2 = {grid[{x + 1, y - 1}], grid[{x - 1, y + 1}]}
valid_diag?(d1) and valid_diag?(d2)
end
defp valid_diag?({"M", "S"}), do: true
defp valid_diag?({"S", "M"}), do: true
defp valid_diag?(_), do: false
defp parse_to_map(data) do
lines = Transformers.lines(data)
for {line, y} <- Enum.with_index(lines),
{char, x} <- Enum.with_index(String.graphemes(line)),
into: %{} do
{{x, y}, char}
end
end
def parse(data \\ input()) do
data |> Transformers.lines()
end
end