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

Day 12 - Advent of Code 2023

lib/advent_of_code/2023/day-12.livemd

Day 12 - Advent of Code 2023

Mix.install([
  {:kino, "~> 0.12.0"},
  {:benchee, "~> 1.2"},
  {:memoize, "~> 1.4"}
])

Links

Input

input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
".???#??????#. 6,1\n???##???##?#??#?#..# 1,14,1\n#??????#????#?###?# 3,1,10,1\n.???????#????. 1,2,1,2\n?##?.??.???.. 3,2,2\n#.?###?#??. 1,5\n?#??#??????????.??.? 7,2,3,1\n???###?#.##??? 8,3\n.##???.?????? 2,1,2\n???.?????????? 1,5,2\n???#????#? 4,1,1\n.????.#.???. 2,1,2\n.????.?.??##?? 1,1,1,5\n??????##?#? 6,1\n?#????#?#?.?#???? 1,2,2,3\n#..??????????????. 1,1,2,7\n????????#?. 1,2,4\n##??.??.??#?#? 3,4\n????#???????##?.??.? 2,1,1,5,1,1\n.?#?????#???..#???? 4,1,1,1,1,1\n?#?????#.#???? 6,1,1,2\n????#?.????? 1,1,2,1\n.??#?.#??. 2,3\n?.?#????.???#####??? 1,5,9\n??.#?#?#??#? 1,8\n?.???.??.?? 1,1,1\n????.?.#.? 1,1,1\n??#???????#?.#??# 3,3,1,2,1\n??#???.??????.? 4,4,1\n#??#??..??????. 4,1,1,1,1\n?????.#??. 1,1,1\n.???#.??#???.? 1,1,4,1\n??##???#..???? 3,2,1\n????..???###?# 1,1,7\n.??.?.##?#..?.?? 1,4,1,2\n?????#?#???##??#???. 3,14\n?#??????#?? 4,2\n?.?#??#??? 2,5\n??#?#???????## 1,1,1,7\n??#?.###??#? 1,6\n#???????#?.#?#? 5,1,4\n#??.???#???.?## 3,1,5,2\n???#????????#??? 4,6\n.#????#??????.?? 3,4,1,1\n?.???.?#?????#? 2,5\n?.???..#?.?. 1,2,1,1\n???.#????#????#? 1,1,8\n??????????#.?.?.??? 2,7,1,1,1,1\n#??##??#???? 1,7,1\n??#?.??#??????? 3,5,3\n??###???????#.#? 10,1,1\n??#.??.??????#?.?? 2,2,3,2,1\n??#??????.??.????? 8,3\n?..#?#???#?? 1,4,1,1\n.?.?#?????#??#? 1,1,1,1,4\n????#?.#??? 4,2,1\n?.???###?.? 6,1\n??.???????..???. 4,2,2\n????.#????.#. 3,4,1\n????.????.?#?#??? 2,1,1,5\n#???????.?#?#.. 3,1,1,1,1\n.??.??????.#? 5,1\n.?????#.???.##?? 5,1,2\n####????#?????#??.# 5,10,1\n?????.???#.???.?#. 3,1,2,1,1,1\n#?#????#????#? 8,4\n?#?.????????.??# 2,6,1,1\n???????.???.???? 2,1,1,3\n#???#?#???????? 5,1,1,4\n?.#??????..???#???? 7,6\n.??????#??# 1,1,4\n????????????????? 5,1,1\n??????#????????????? 15,1\n?#?????#?# 3,3,1\n?????#.###???# 1,1,4,2\n?#?????.??? 6,1\n?.???.??#??#?#?? 1,9\n?#?.??????#?? 1,2,3\n??#?????????#???.?# 3,3,2,1,1\n?##???.?.?#????. 5,5\n?.????#?###????? 1,7,1\n????.????##???? 4,3,1\n??#.?????###??. 1,1,2,1,5\n.???...??. 1,1\n#?.?#####???#?.#?? 1,11,1\n.???????#?.??? 7,1\n.????.????# 3,2,2\n#????#???.??#? 3,4,1,1\n?#??..?#??. 3,3\n.??.????##??##???? 1,10\n#.#???#?###??.#??#.? 1,1,8,4,1\n???????#????????#?#? 9,6\n#.?.??????.?????#? 1,1,1,1,3,1\n..#?...?#???###.. 1,7\n.?.??#????#? 1,2,4\n..#.#.????????#?#? 1,1,1,1,1,2\n?##?#.?#.? 4,1\n?.#?#?#??#??.??? 1,1,4,1,1\n.?.?#?#???#??.??# 1,9,1,1\n.#?????????##?? 3,7\n.??????.?. 1,1,1\n.?#????###??#???##. 12,1,2\n.????.???.??#?#???#? 1,1,9\n?.???#???#.??? 8,2\n??.?#..???. 1,1\n????##???#??###??.?? 16,1\n??.??..#????? 2,4,1\n?.???.??????? 3,6\n??.???.?#?#?????? 1,1,1,4,4\n?#????.????.?#?#.? 3,1,1,2,3,1\n..?????#?.#?#?#.??. 1,1,1,1,3,1\n??#?#?#????????.???? 11,1,3\n?????.?????? 1,1\n..??#??##???.??#??.. 6,3\n?????.###?.?#???.?#? 3,3,3,3\n???.??#####????#?. 1,7,2\n??????#???.???.???? 8,1,1,1,1\n??????.??.?????.? 5,1,1,1\n..???????? 1,1\n?.#??..????#?#??? 1,1,1,3,4\n.?????????#. 1,1,1,1\n???#??##?#?.. 7,1\n?.?####???.??##? 5,3\n#???#?????..???? 1,3,4,4\n#?.???.??.?#??? 2,2,2,3\n?#?#????????.?#????? 9,1,6\n##??.#.?..?##?#??? 2,1,7\n??#?#.????.??..?.?# 5,1,1,1,1,2\n?.?#??###?????????? 1,7,8\n#.?#.????? 1,2,3\n?.???#?#?.?#.?. 3,2\n??????##?#??#??.?? 1,10,1,1\n?.?#?#?#??####???? 1,11,1,1\n#.????????##.#???? 1,8,1,1,1\n???#.???.??????#? 4,1,5\n?#?????###?.? 5,3,1\n..??????.?. 3,1\n#????#.?????.#?... 1,1,1,2,1,2\n??#.?????#??? 1,1,1,5\n???..????? 1,1,1\n??#?????#??... 4,3\n?.#???#??# 1,1,5\n???#???????##?#?? 1,2,2,1,5\n??????#????#??#. 1,4,2,1,1\n???#??.????? 3,2\n.##?#?#???#?# 8,3\n?.#?...??##?????#?.. 1,1,10\n????#.?.#? 3,1,1\n??#?#?.?#.#??#?? 2,2,1,6\n.#???..?#####? 1,1,6\n#???????#???#?#.. 1,7,2,1\n.?#???.???##??#???? 3,1,1,7,1\n???#.?????#? 1,1,1,1\n???.??######?????. 1,11\n#..???.???? 1,2,1\n??????#??.????#? 2,5,1,3\n#?.?.?.?????.? 2,1,2,1\n??#.???.???.? 2,2,1\n?#?????#?#?????????? 3,13\n?????????#??.?.? 1,1,3\n##?##???..??.??# 6,1,3\n??#?.???.?. 1,2\n??.#??#???##??. 1,1,1,5\n.??#?????#?..# 7,1,1\n???.??.???????? 2,1,1,1,1\n#?.?#?##?????? 1,9\n????#???#??.??????? 11,1,2\n??#?????#??#.###?# 10,5\n?#..????????? 2,3,2\n.#?.????????.??.? 1,4,2\n?###?.##?##.#?.???.? 5,2,2,1,2,1\n??#???????#??#????# 1,1,1,1,8,1\n??.??????#? 1,1,2\n???.??#.??.????.??? 1,1,2,1,4,1\n???.#.???????# 1,1,8\n??.??#.??#..? 2,3,3,1\n?.?#??.##?##? 3,5\n#.???????.?? 1,2,3,2\n###?.?#??#? 4,5\n?#?#.?#?" <> ...

Solution

defmodule Day12 do
  use Memoize

  defdelegate parse(input), to: __MODULE__.Input

  def part1(input) do
    input
    |> parse()
    |> Stream.map(&amp;count_matches/1)
    |> Enum.sum()
  end

  def part2(input) do
    input
    |> parse()
    |> Stream.map(&amp;expand/1)
    |> Stream.map(&amp;count_matches/1)
    |> Enum.sum()
  end

  defp expand({line, sizes}) do
    {
      List.duplicate(line, 5) |> Enum.join("?") |> String.to_charlist(),
      List.duplicate(sizes, 5) |> List.flatten()
    }
  end

  defmemo count_matches({line, []}) do
    if Enum.any?(line, &amp;(&amp;1 == ?#)), do: 0, else: 1
  end

  defmemo count_matches({line, sizes}) do
    cond do
      length(line) < Enum.sum(sizes) + length(sizes) - 1 ->
        0

      hd(line) == ?. ->
        count_matches({tl(line), sizes})

      hd(line) == ?# ->
        size = hd(sizes)
        match = Enum.take(line, size) |> Enum.all?(&amp;(&amp;1 in ~c"?#"))
        blank = Enum.at(line, size) != ?#

        if match and blank do
          count_matches({Enum.slice(line, (size + 1)..-1), tl(sizes)})
        else
          0
        end

      hd(line) == ?? ->
        count_matches({[?. | tl(line)], sizes}) + count_matches({[?# | tl(line)], sizes})
    end
  end

  defmodule Input do
    def parse(input) when is_binary(input) do
      input
      |> String.splitter("\n", trim: true)
      |> parse()
    end

    def parse(input) do
      Enum.map(input, &amp;parse_line/1)
    end

    def parse_line(line) do
      [line, sizes] = String.split(line, " ", trim: true)
      sizes = sizes |> String.split(",", trim: true) |> Enum.map(&amp;String.to_integer/1)

      {String.to_charlist(line), sizes}
    end
  end
end
{:module, Day12, <<70, 79, 82, 49, 0, 0, 19, ...>>,
 {:module, Day12.Input, <<70, 79, 82, ...>>, {:parse_line, 1}}}

For each row, count all of the different arrangements of operational and broken springs that meet the given criteria. What is the sum of those counts?

Your puzzle answer was 7599.

Day12.part1(input)
7599

Unfold your condition records; what is the new sum of possible arrangement counts?

Your puzzle answer was 15454556629917.

Day12.part2(input)
15454556629917

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 Day12Test do
  use ExUnit.Case, async: false

  setup_all do
    [
      input:
        "???.### 1,1,3\n.??..??...?##. 1,1,3\n?#?#?#?#?#?#?#? 1,3,1,6\n????.#...#... 4,1,1\n????.######..#####. 1,6,5\n?###???????? 3,2,1"
    ]
  end

  describe "part1/1" do
    test "returns expected value", %{input: input} do
      assert Day12.part1(input) == 21
    end
  end

  describe "part2/1" do
    test "returns expected value", %{input: input} do
      assert Day12.part2(input) == 525_152
    end
  end
end

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

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

Benchmarking

# https://github.com/bencheeorg/benchee
Benchee.run(
  %{
    "Part 1" => fn ->
      Memoize.invalidate(Day12)
      Day12.part1(input)
    end,
    "Part 2" => fn ->
      Memoize.invalidate(Day12)
      Day12.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         14.71       0.0680 s     ±3.09%       0.0672 s       0.0730 s
Part 2          0.36         2.80 s     ±0.23%         2.80 s         2.81 s

Comparison: 
Part 1         14.71
Part 2          0.36 - 41.25x slower +2.74 s

Memory usage statistics:

Name      Memory usage
Part 1       0.0420 GB
Part 2         1.85 GB - 44.06x memory usage +1.81 GB

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

Reduction count statistics:

Name   Reduction count
Part 1          8.57 M
Part 2        386.23 M - 45.08x reduction count +377.66 M

**All measurements for reduction count were the same**
nil