aoc/23/elixir/lib/days/day8.ex
2023-12-08 10:00:27 +01:00

81 lines
2.5 KiB
Elixir

defmodule AOC.Day8 do
# use AOC.Day, day: 8, input: "example1"
# use AOC.Day, day: 8, input: "example2"
# use AOC.Day, day: 8, input: "example3"
use AOC.Day, day: 8
@start "AAA"
@stop "ZZZ"
def parse_input([instructions, _ | nodes]) do
instructions = instructions |> String.to_charlist() |> Stream.cycle()
nodes = Enum.map(nodes, fn s ->
%{"id" => id, "left" => left, "right" => right} = Regex.named_captures(~r/^(?<id>[[:alnum:]]{3}) = \((?<left>[[:alnum:]]{3}), (?<right>[[:alnum:]]{3})\)$/, s)
{id, {left, right}}
end)
|> Enum.into(%{})
{instructions, nodes}
end
def reach_end(instructions, nodes) do
Enum.reduce_while(instructions, {@start, 0}, fn instruction, {current_node, step_count} ->
if current_node == @stop do
{:halt, step_count}
else
{left, right} = Map.fetch!(nodes, current_node)
next_node = case instruction do
?L -> left
?R -> right
end
{:cont, {next_node, step_count + 1}}
end
end)
end
def part1({instructions, nodes}) do
reach_end(instructions, nodes)
end
def find_cycle(instructions, nodes, start) do
Enum.reduce_while(instructions, {start, 0, nil}, fn instruction, {current_node, step_count, last_end_count}->
if String.ends_with?(current_node, "Z") and last_end_count != nil do
{:halt, {last_end_count, step_count - last_end_count}}
else
last_end_count = if String.ends_with?(current_node, "Z"), do: step_count, else: last_end_count
{left, right} = Map.fetch!(nodes, current_node)
next_node = case instruction do
?L -> left
?R -> right
end
{:cont, {next_node, step_count + 1, last_end_count}}
end
end)
end
def brute_force_find_cycle(cycles) do
Stream.repeatedly(fn -> nil end) |> Enum.reduce_while(cycles, fn _, cycles ->
all_ended = cycles |> Enum.map(&elem(&1, 0)) |> Enum.uniq() |> length() == 1
if all_ended do
{:halt, cycles |> hd() |> elem(0)}
else
[{count, cycle_count} | tl] = Enum.sort(cycles)
IO.inspect(count)
{:cont, [{count + cycle_count, cycle_count} | tl]}
end
end)
end
def part2({instructions, nodes}) do
IO.puts("Use LCM for the following numbers :)")
Enum.filter(nodes, fn {k, _v} -> String.ends_with?(k, "A") end)
|> Enum.map(fn {k, _v} -> find_cycle(instructions, nodes, k) end)
|> Enum.map(&elem(&1, 0))
|> Enum.map(&Integer.to_string/1)
|> Enum.join(" ")
end
end