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

Day 3: Perfectly Spherical Houses in a Vacuum

day_3_perfectly_spherical_houses_in_a_vacuum.livemd

Day 3: Perfectly Spherical Houses in a Vacuum

Section

defmodule P1 do
  def recurse(list, curr \\ {0, 0}, acc \\ %{})

  def recurse([], _curr, acc) do
    Kernel.map_size(acc)
  end

  def recurse([h | t], curr, acc) do
    {x, y} = curr

    new_curr =
      case h do
        "^" -> {x, y + 1}
        "v" -> {x, y - 1}
        "<" -> {x - 1, y}
        ">" -> {x + 1, y}
      end

    recurse(t, new_curr, Map.update(acc, new_curr, 1, &amp;(&amp;1 + 1)))
  end
end

{:ok, data} = File.read("/Users/eli/Desktop/input.txt")

data
|> String.graphemes()
|> P1.recurse()
2572
defmodule P2 do
  def recurse(list, turn \\ 0, poses \\ {{0, 0}, {0, 0}}, acc \\ %{})

  def recurse([], _turn, _poses, acc) do
    Kernel.map_size(acc)
  end

  def recurse([h | t], turn, poses, acc) do
    {{xs, ys}, {xr, yr}} = poses
    {x, y} = if turn == 1, do: {xr, yr}, else: {xs, ys}

    new_curr =
      case h do
        "^" -> {x, y + 1}
        "v" -> {x, y - 1}
        "<" -> {x - 1, y}
        ">" -> {x + 1, y}
      end

    new_poses = if turn == 1, do: {{xs, ys}, new_curr}, else: {new_curr, {xr, yr}}
    recurse(t, rem(turn + 1, 2), new_poses, Map.update(acc, new_curr, 1, &amp;(&amp;1 + 1)))
  end
end

{:ok, data} = File.read("/Users/eli/Desktop/input.txt")

data
|> String.graphemes()
|> P2.recurse()
2631