Skip to content

Commit 5f293bb

Browse files
committed
day8
1 parent 008b115 commit 5f293bb

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

08/sample_input

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
LR
2+
3+
11A = (11B, XXX)
4+
11B = (XXX, 11Z)
5+
11Z = (11B, XXX)
6+
22A = (22B, XXX)
7+
22B = (22C, 22C)
8+
22C = (22Z, 22Z)
9+
22Z = (22B, 22B)
10+
XXX = (XXX, XXX)

08/sol.exs

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
defmodule Day08 do
2+
defp read_input(file_path) do
3+
case File.read(file_path) do
4+
{:ok, content} ->
5+
[directives | network_lines] =
6+
content
7+
|> String.split(~r/\n/, trim: true)
8+
9+
{directives |> String.graphemes(), network_lines |> Enum.map(&parse/1) |> Map.new()}
10+
11+
{:error, reason} ->
12+
raise "Oh no! #{reason}"
13+
end
14+
end
15+
16+
defp parse(line) do
17+
[_, from, left, right] = Regex.run(~r/(\w+) = \((\w+), (\w+)\)/, line)
18+
{from, %{"L" => left, "R" => right}}
19+
end
20+
21+
defp resolve(from, target, network, [], dir, acc),
22+
do: resolve(from, target, network, dir, dir, acc)
23+
24+
defp resolve(from, target, network, [dir | rest], directives_all, acc) do
25+
if String.match?(from, target) do
26+
acc
27+
else
28+
resolve(network[from][dir], target, network, rest, directives_all, acc + 1)
29+
end
30+
end
31+
32+
def partA(file_path) do
33+
{directives, network} = read_input(file_path)
34+
resolve("AAA", ~r/ZZZ/, network, directives, directives, 0)
35+
end
36+
37+
def partB(file_path) do
38+
{directives, network} = read_input(file_path)
39+
40+
Map.keys(network)
41+
|> Enum.filter(fn str -> String.ends_with?(str, "A") end)
42+
|> Enum.map(fn str ->
43+
resolve(str, ~r/Z\z/, network, directives, directives, 0)
44+
end)
45+
|> Enum.reduce(fn x, y -> div(x * y, Integer.gcd(x, y)) end)
46+
end
47+
end
48+
49+
IO.puts(Day08.partA("./input"))
50+
IO.puts(Day08.partB("./input"))

0 commit comments

Comments
 (0)