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

Advent of Code 2015 Day 3 Part 2

2015_day3_part2.livemd

Advent of Code 2015 Day 3 Part 2

Mix.install([
  {:kino_aoc, "~> 0.1"}
])

Get Inputs

{:ok, puzzle_input} =
  KinoAOC.download_puzzle("2015", "3", System.fetch_env!("LB_SESSION"))

My answer

puzzle_input
|> String.codepoints()
|> Enum.reduce({{0, {0, 0}, {0, 0}}, [{0, 0}]}, fn direction, {{s, {sx, sy}, {rx, ry}}, houses} ->
  {x, y} = if s == 0, do: {sx, sy}, else: {rx, ry}

  new_point =
    case direction do
      ">" -> {x + 1, y}
      "<" -> {x - 1, y}
      "^" -> {x, y + 1}
      _ -> {x, y - 1}
    end

  new_sr =
    if s == 0 do
      {1, new_point, {rx, ry}}
    else
      {0, {sx, sy}, new_point}
    end

  {new_sr, [new_point | houses]}
end)
|> elem(1)
|> Enum.uniq()
|> length()