Skip to content

Commit fb0650f

Browse files
committed
Solve 2025/4
1 parent 4fa8eef commit fb0650f

3 files changed

Lines changed: 233 additions & 0 deletions

File tree

lib/2025/day_04.ex

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
defmodule AdventOfCode.Y2025.Day04 do
2+
@moduledoc """
3+
--- Day 4: Printing Department ---
4+
Problem Link: https://adventofcode.com/2025/day/4
5+
Difficulty: m
6+
Tags: simulation grid graph
7+
"""
8+
alias AdventOfCode.Helpers.{InputReader, Transformers}
9+
alias Yog.{Builder.Grid, Model, Transform}
10+
11+
def input, do: InputReader.read_from_file(2025, 4)
12+
13+
def run(input \\ input()) do
14+
graph = parse(input)
15+
16+
{run_1(graph), run_2(graph)}
17+
end
18+
19+
def parse(data \\ input()) do
20+
data
21+
|> Transformers.lines()
22+
|> Enum.map(&String.graphemes/1)
23+
|> Grid.from_2d_list_with_topology(:undirected, Grid.queen(), Grid.always())
24+
|> Grid.to_graph()
25+
|> Transform.filter_nodes(fn char -> char == "@" end)
26+
end
27+
28+
@spec run_1(Yog.Graph.t()) :: non_neg_integer()
29+
def run_1(graph) do
30+
graph
31+
|> Enum.count(fn {id, _} -> length(Model.neighbor_ids(graph, id)) < 4 end)
32+
end
33+
34+
def run_2(graph) do
35+
initial_removable =
36+
graph
37+
|> Enum.filter(fn {id, _} -> length(Model.neighbor_ids(graph, id)) < 4 end)
38+
|> Enum.map(fn {id, _} -> id end)
39+
40+
process_removals(graph, initial_removable, 0)
41+
end
42+
43+
defp process_removals(graph, removable, count) do
44+
case removable do
45+
[] ->
46+
count
47+
48+
[curr | rest] ->
49+
if Model.has_node?(graph, curr) do
50+
neighbors = Model.neighbor_ids(graph, curr)
51+
new_graph = Model.remove_node(graph, curr)
52+
53+
newly_removable =
54+
Enum.filter(neighbors, fn nid ->
55+
Model.has_node?(new_graph, nid) and length(Model.neighbor_ids(new_graph, nid)) < 4
56+
end)
57+
58+
process_removals(new_graph, newly_removable ++ rest, count + 1)
59+
else
60+
process_removals(graph, rest, count)
61+
end
62+
end
63+
end
64+
end

0 commit comments

Comments
 (0)