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

Day 10 - Advent of Code 2023

lib/advent_of_code/2023/day-10.livemd

Day 10 - Advent of Code 2023

Mix.install([:kino, :benchee])

Links

Input

input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
"F-F7----F7-7-J--J--7FFL7F-7FFF77JFL|7|-L7--F77FF-FF7..-J7.F---FF7-|-JF7-J-F|-7FF|-FLL--7F-F--.--|77F|F---7-F--F7.L7F77|F----FF-F7FF.F-.L.L.J\n7-L--|J|||FL-7|-|F7-LJ|F|-F7FJ|7.F-|-L7L-7LJ.-F--F.L--J---7|J|.|.LL||-|.F-|JL.FL.LLLJ7F7JL7JJ7-FL7L-F7L|J|FLJ.|JF--JFJJL7L.|LJ-|7F|-JJ77..F|\n|L|.7JF|J---J|J.|F.|L7LFFFJLJFJF7F77.F7.L7L-7F|7FLFJ.|F7J|LL.--|JJ|FJF-7J7L7|F-L---7J.|.F-|-F-7.LL7.|JF--7-7L7|7||J-|JJJFF||..|L7L7FJL7777FJ\nJ-FJJ-L|LF7|.|-F|J-|7J7LLL--7L-J||L7.FL|JFF-J-LL-JJ.F-L7F|-FF|||L7-LF7J|F|7|F7JFJ.L|J.L7JJF77-77-LL-JL|.J|LJJL7.F|..LJ.F--.LF-|.7F|FJ7|-F--J\n|FLJJ.L|F|LFF-.7J.FLJ-7.LFF7L7F-J|FJF--7|FJJF-7|-|L|-7F7-JF7F--7.--FF77-F.|FL|FJJ|7|-F7.LF-J-FJ|-.LL|.LF-7J|7LJF7L-L-F---|-F7F-|J7||.LF7LJJ.\n77|FF-FL-L-7J|L|-7-||FL7.F|L-J|F7|L7|F-J7.F-FFF7.77JFFJ||J||L-7|7LFFJL7F7.LJ.|7|.F-J7FL---|J-|JJJ7F|L7F|.|FFF7J--7J|.LJ|L|.FL-LLF-J|FLL--J.|\n|-F|J7L77-7J7|7L-|-J---JFFL7F-J|||FJ||F777JF.FJ|F7F-77-FF7||7J|L--7|F-J|L7|F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-----JFJL7||J..|||77F7LJ|-7FJJJ\nFF7-J|JJFJ.L|L|FFJFL7F--7L7LJ|L-J||L-JL--7|L7JL--7L7||LL--7L7|F-JL7|FJ|F-JL-JF-JFJ|L7L-7L-JL--7LJL-J||LJ|F-J||F7-F-JF-J||LFFLJ-77|LJFL7LF.|7\n|J|L7J|F7-|.F7|F-F-7LJF7L7|F7L--7|L--7F--J|FJF7F7L7|LJF---JFJ|L-7L|||FJL7F---JF7L7L7L-7L-7F7F7L-7F--JL7FJL-7|FJL-JF-J7FJL7F-77LLJ-L.77FJF7||\nL.|-F7-L|.LJ|7J-FL7|F-JL-JLJL7FFJL-7FJ|F-7|L7|LJ|J|L7LL---7L-JF7|FJ|||.FJL--7|||FJFJF7|F-J||||F7||F7LFJ|F7FJLJF---JF-7L-7|L7|7J||-L7LLL7|-||\nL|.FL--JJ7F-JL|LF-J|L-------7L7L-7FJL7LJFJ|FJL-7L7L7L7F-7|L-7FJLJL7|||FJF---JFJLJFJ7||||F7|||||||LJL7L7||||F--JF7F7|FJF7|L-JL7.L---77F7J|.JJ\nF77|7.|LFF|L|77.L-7|F7F7F--7|FJF-JL77|F-JFJ|F77L7L7|FJL7|F--J|F7F-J||||FJ.F7|L--7|7FJ||||||||||LJF--JFJ||||||F7|||LJ|FJLJF---J7LJ7L|7L|.LJ7.\nLJ-F7J|F-LJ-7|LF7FJ||||||F7LJ|7L-7FJFJ|F7L7|||F7L7LJL7FJ|L--7|||L7FJ|LJL--J|F--7||FJFJ||||||||L-7L7F7L7||||L7||||L-7LJF--J-F--77L||L|-J77||7\nL-JLJLFL|7F|FF-J|L7LJLJ|LJL-7|F7-||7L7|||FJLJ|||FL-7FJ|FJF7FJ|||FJ|FJF--7F-JL-7|||L7|FJLJLJ||L--JFJ||7|LJ||FJ|||L7FJF7L----JF7|77.7-LL|J.|.7\n-7-F|F7" <> ...

Solution

defmodule Day10 do
  defdelegate parse(input), to: __MODULE__.Input

  def part1(input) do
    grid = parse(input)
    loop = find_loop(grid)

    length(loop) |> div(2)
  end

  def part2(input) do
    grid = parse(input)
    loop = find_loop(grid)

    # https://en.wikipedia.org/wiki/Pick%27s_theorem
    shoelace_area(loop) - (length(loop) |> div(2)) + 1
  end

  # Run it in both directions, not being sure
  # which direction is "positively oriented".
  # The incorrect direction will have a negative answer.
  defp shoelace_area(loop) do
    max(
      shoelace_area_calc(loop),
      shoelace_area_calc(Enum.reverse(loop))
    )
  end

  # https://en.wikipedia.org/wiki/Shoelace_formula
  defp shoelace_area_calc(loop) do
    loop
    |> Enum.chunk_every(2, 1, [hd(loop)])
    |> Enum.map(fn [{x1, y1}, {x2, y2}] ->
      x1 * y2 - x2 * y1
    end)
    |> Enum.sum()
    |> div(2)
  end

  defp find_loop(grid) do
    start_xy = find_start_xy(grid)
    # Assume that the 'S' is acting as a 'F',
    # as it is in the examples and my real input
    start_a = next({start_xy, ?F, ?W}, grid)
    start_b = next({start_xy, ?F, ?N}, grid)

    [
      start_xy
      | grid
        |> find_loop([start_a], [start_b])
        |> Enum.map(&amp;elem(&amp;1, 0))
    ]
  end

  defp find_start_xy(grid) do
    Enum.find_value(grid, fn
      {xy, ?S} -> xy
      _ -> nil
    end)
  end

  # The 2 paths have met
  defp find_loop(_grid, [{xy, _, _} | _] = path_a, [{xy, _, _} | path_b]) do
    Enum.reverse(path_b) ++ path_a
  end

  defp find_loop(grid, path_a, path_b) do
    next_a = path_a |> hd() |> next(grid)
    next_b = path_b |> hd() |> next(grid)

    find_loop(
      grid,
      [next_a | path_a],
      [next_b | path_b]
    )
  end

  defp next(current, grid) do
    {next_xy, next_dir} = next_xy_and_dir(current)

    {next_xy, grid[next_xy], next_dir}
  end

  # | is a vertical pipe connecting north and south.
  defp next_xy_and_dir({{x, y}, ?|, ?N}), do: {{x, y - 1}, ?N}
  defp next_xy_and_dir({{x, y}, ?|, ?S}), do: {{x, y + 1}, ?S}
  # - is a horizontal pipe connecting east and west.
  defp next_xy_and_dir({{x, y}, ?-, ?E}), do: {{x + 1, y}, ?E}
  defp next_xy_and_dir({{x, y}, ?-, ?W}), do: {{x - 1, y}, ?W}
  # L is a 90-degree bend connecting north and east.
  defp next_xy_and_dir({{x, y}, ?L, ?S}), do: {{x + 1, y}, ?E}
  defp next_xy_and_dir({{x, y}, ?L, ?W}), do: {{x, y - 1}, ?N}
  # J is a 90-degree bend connecting north and west.
  defp next_xy_and_dir({{x, y}, ?J, ?S}), do: {{x - 1, y}, ?W}
  defp next_xy_and_dir({{x, y}, ?J, ?E}), do: {{x, y - 1}, ?N}
  # 7 is a 90-degree bend connecting south and west.
  defp next_xy_and_dir({{x, y}, ?7, ?N}), do: {{x - 1, y}, ?W}
  defp next_xy_and_dir({{x, y}, ?7, ?E}), do: {{x, y + 1}, ?S}
  # F is a 90-degree bend connecting south and east.
  defp next_xy_and_dir({{x, y}, ?F, ?N}), do: {{x + 1, y}, ?E}
  defp next_xy_and_dir({{x, y}, ?F, ?W}), do: {{x, y + 1}, ?S}
  # . is ground; there is no pipe in this tile.

  defmodule Input do
    def parse(input) when is_binary(input) do
      input
      |> String.splitter("\n", trim: true)
      |> Stream.with_index()
      |> Enum.reduce(%{}, &amp;parse_line/2)
    end

    def parse_line({line, y}, grid) do
      line
      |> String.to_charlist()
      |> Enum.with_index()
      |> Enum.reduce(grid, fn {char, x}, acc ->
        Map.put(acc, {x, y}, char)
      end)
    end
  end
end
{:module, Day10, <<70, 79, 82, 49, 0, 0, 21, ...>>,
 {:module, Day10.Input, <<70, 79, 82, ...>>, {:parse_line, 2}}}

Find the single giant loop starting at S. How many steps along the loop does it take to get from the starting position to the point farthest from the starting position?

Your puzzle answer was 6690.

Day10.part1(input)
6690

Figure out whether you have time to search for the nest by calculating the area within the loop. How many tiles are enclosed by the loop?

Your puzzle answer was 525.

Day10.part2(input)
525

Both parts of this puzzle are complete! They provide two gold stars: **

At this point, you should return to your Advent calendar and try another puzzle.

If you still want to see it, you can get your puzzle input.

Tests

ExUnit.start(auto_run: false)

defmodule Day10Test do
  use ExUnit.Case, async: false

  setup_all do
    [
      input1: ".....\n.S-7.\n.|.|.\n.L-J.\n.....",
      input2: "..F7.\n.FJ|.\nSJ.L7\n|F--J\nLJ...",
      input3:
        "...........\n.S-------7.\n.|F-----7|.\n.||.....||.\n.||.....||.\n.|L-7.F-J|.\n.|..|.|..|.\n.L--J.L--J.\n...........",
      input4:
        ".F----7F7F7F7F-7....\n.|F--7||||||||FJ....\n.||.FJ||||||||L7....\nFJL7L7LJLJ||LJ.L-7..\nL--J.L7...LJS7F-7L7.\n....F-J..F7FJ|L7L7L7\n....L7.F7||L7|.L7L7|\n.....|FJLJ|FJ|F7|.LJ\n....FJL-7.||.||||...\n....L---J.LJ.LJLJ..."
    ]
  end

  describe "part1/1" do
    test "returns expected value", %{input1: input1, input2: input2} do
      assert Day10.part1(input1) == 4
      assert Day10.part1(input2) == 8
    end
  end

  describe "part2/1" do
    test "returns expected value", context do
      assert Day10.part2(context.input1) == 1
      assert Day10.part2(context.input3) == 4
      assert Day10.part2(context.input4) == 8
    end
  end
end

ExUnit.run()
..
Finished in 0.00 seconds (0.00s async, 0.00s sync)
2 tests, 0 failures

Randomized with seed 958413
%{total: 2, failures: 0, excluded: 0, skipped: 0}

Benchmarking

# https://github.com/bencheeorg/benchee
Benchee.run(
  %{
    "Part 1" => fn -> Day10.part1(input) end,
    "Part 2" => fn -> Day10.part2(input) end
  },
  memory_time: 2,
  reduction_time: 2
)

nil
Warning: the benchmark Part 1 is using an evaluated function.
  Evaluated functions perform slower than compiled functions.
  You can move the Benchee caller to a function in a module and invoke `Mod.fun()` instead.
  Alternatively, you can move the benchmark into a benchmark.exs file and run mix run benchmark.exs

Warning: the benchmark Part 2 is using an evaluated function.
  Evaluated functions perform slower than compiled functions.
  You can move the Benchee caller to a function in a module and invoke `Mod.fun()` instead.
  Alternatively, you can move the benchmark into a benchmark.exs file and run mix run benchmark.exs

Operating System: macOS
CPU Information: Apple M1 Pro
Number of Available Cores: 10
Available memory: 32 GB
Elixir 1.15.6
Erlang 26.1

Benchmark suite executing with the following configuration:
warmup: 2 s
time: 5 s
memory time: 2 s
reduction time: 2 s
parallel: 1
inputs: none specified
Estimated total run time: 22 s

Benchmarking Part 1 ...
Benchmarking Part 2 ...

Name             ips        average  deviation         median         99th %
Part 1        181.02        5.52 ms     ±6.66%        5.49 ms        6.75 ms
Part 2        130.07        7.69 ms     ±5.53%        7.54 ms        8.83 ms

Comparison: 
Part 1        181.02
Part 2        130.07 - 1.39x slower +2.16 ms

Memory usage statistics:

Name      Memory usage
Part 1        12.05 MB
Part 2        17.75 MB - 1.47x memory usage +5.70 MB

**All measurements for memory usage were the same**

Reduction count statistics:

Name           average  deviation         median         99th %
Part 1        463.61 K     ±0.01%       463.60 K       463.71 K
Part 2        935.87 K     ±0.00%       935.87 K       935.99 K

Comparison: 
Part 1        463.60 K
Part 2        935.87 K - 2.02x reduction count +472.26 K
nil