Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

AoC 2023 Day 8

2023/08/aoc2023_day08.livemd

AoC 2023 Day 8

Section

defmodule HauntedWasteland do
  def steps(map, directions, start) do
    steps(map, directions, start, directions, 0)
  end

  defp steps(_map, _directions, "ZZZ", _remaining_directions, steps), do: steps

  defp steps(map, directions, location, [], steps),
    do: steps(map, directions, location, directions, steps)

  defp steps(map, directions, location, [next_dir | rest], steps) do
    next_location = next_direction(map[location], next_dir)
    steps(map, directions, next_location, rest, steps + 1)
  end

  defp next_direction({left, _right}, ?L), do: left
  defp next_direction({_left, right}, ?R), do: right
end
map = %{
  "AAA" => {"BBB", "CCC"},
  "BBB" => {"DDD", "EEE"},
  "CCC" => {"ZZZ", "GGG"},
  "DDD" => {"DDD", "DDD"},
  "EEE" => {"EEE", "EEE"},
  "GGG" => {"GGG", "GGG"},
  "ZZZ" => {"ZZZ", "ZZZ"}
}
directions = ~c"RL"
HauntedWasteland.steps(map, directions, "AAA")
map = %{
  "AAA" => {"BBB", "BBB"},
  "BBB" => {"AAA", "ZZZ"},
  "ZZZ" => {"ZZZ", "ZZZ"}
}
directions = ~c"LLR"
HauntedWasteland.steps(map, directions, "AAA")