Powered by AppSignal & Oban Pro

🎄 Year 2020 🔔 Day 12

elixir/notebooks/2020/day12.livemd

🎄 Year 2020 🔔 Day 12

Parse input

filename = "#{__DIR__}/../../../inputs/2020/day12.txt"

parsed_input_stream =
  File.stream!(filename)
  |> Enum.map(&String.trim/1)
  |> Enum.map(&String.split_at(&1, 1))
  |> Enum.map(fn {instruction, amount} -> {instruction, String.to_integer(amount)} end)

Part One

rem(4, 4)
defmodule Helper do
  def follow_instruction({{x, y}, dir}, {"N", amount}), do: {{x, y + amount}, dir}
  def follow_instruction({{x, y}, dir}, {"S", amount}), do: {{x, y - amount}, dir}
  def follow_instruction({{x, y}, dir}, {"E", amount}), do: {{x + amount, y}, dir}
  def follow_instruction({{x, y}, dir}, {"W", amount}), do: {{x - amount, y}, dir}

  def follow_instruction({{x, y}, dir = "N"}, {"F", amount}), do: {{x, y + amount}, dir}
  def follow_instruction({{x, y}, dir = "S"}, {"F", amount}), do: {{x, y - amount}, dir}
  def follow_instruction({{x, y}, dir = "E"}, {"F", amount}), do: {{x + amount, y}, dir}
  def follow_instruction({{x, y}, dir = "W"}, {"F", amount}), do: {{x - amount, y}, dir}

  def follow_instruction(acc, {"L", amount}), do: follow_instruction(acc, {"R", -amount})

  def follow_instruction({coords, dir}, {"R", amount}) do
    directions = ["N", "E", "S", "W"]
    change = div(amount, 90)

    new_direction =
      (Enum.find_index(directions, &(&1 == dir)) + change)
      |> then(&rem(&1, 4))
      |> then(&Enum.at(directions, &1))

    {coords, new_direction}
  end
end

parsed_input_stream
|> Enum.reduce({{0, 0}, "E"}, &Helper.follow_instruction(&2, &1))
|> then(fn {{x, y}, _} -> abs(x) + abs(y) end)

Part two

defmodule Helper do
  def follow_instruction({ship_coords, {wx, wy}, dir}, {"N", amount}),
    do: {ship_coords, {wx, wy + amount}, dir}

  def follow_instruction({ship_coords, {wx, wy}, dir}, {"S", amount}),
    do: {ship_coords, {wx, wy - amount}, dir}

  def follow_instruction({ship_coords, {wx, wy}, dir}, {"E", amount}),
    do: {ship_coords, {wx + amount, wy}, dir}

  def follow_instruction({ship_coords, {wx, wy}, dir}, {"W", amount}),
    do: {ship_coords, {wx - amount, wy}, dir}

  def follow_instruction({ship_coords, {wx, wy}, dir = "N"}, {"F", amount}),
    do: {ship_coords, {wx, wy + amount}, dir}

  def follow_instruction({ship_coords, {wx, wy}, dir = "S"}, {"F", amount}),
    do: {ship_coords, {wx, wy - amount}, dir}

  def follow_instruction({ship_coords, {wx, wy}, dir = "E"}, {"F", amount}),
    do: {ship_coords, {wx + amount, wy}, dir}

  def follow_instruction({ship_coords, {wx, wy}, dir = "W"}, {"F", amount}),
    do: {ship_coords, {wx - amount, wy}, dir}

  def follow_instruction(acc, {"L", amount}), do: follow_instruction(acc, {"R", -amount})

  def follow_instruction({ship_coords, dir}, {"R", amount}) do
    directions = ["N", "E", "S", "W"]
    change = div(amount, 90)

    new_direction =
      (Enum.find_index(directions, &(&1 == dir)) + change)
      |> then(&rem(&1, 4))
      |> then(&Enum.at(directions, &1))

    {coords, new_direction}
  end
end

parsed_input_stream
|> Enum.reduce({{0, 0}, {10, 1}, "E"}, &Helper.follow_instruction(&2, &1))
|> then(fn {{sx, sy}, _, _} -> abs(x) + abs(y) end)