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

Day 4 - Advent of Code 2023

lib/advent_of_code/2023/day-04.livemd

Day 4 - Advent of Code 2023

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

Links

Input

input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
# input = AdventOfCode.Input.get!(4, 2023)
"Card   1: 24 76 32 40 51 61 89  6 30 60 | 30 69 24 86  6  8 92 61 51 88 63 67 32 62 15 49 22 77 40 27 89 60 76 58 79\nCard   2: 97  3 51 52 79  8 89 76 10 59 | 59 48 52 76 97 16 92 81 62 25 89 51 54  3 79 18 94 78  8 32 99 66 10 70 38\nCard   3:  8 67 56 82 96  2 21 47 41 38 |  6 83 17 36  8 21 82 27 68 67  7 38 56 42 66  3 47 87 41 71 88 96  2 98 72\nCard   4: 41 83 77 61 91 13 84 63 81 79 | 29 28 85 84 19 83 37 55  7 97  8 11 12 50 72 42 48 92 30  2 27 18 38 89 59\nCard   5: 31 96 75 87 56  8 79 80 49 89 | 32 75 80 56 77 48 59 89  6 67 87 33 14 44 50 49 28 82 79 40  9 31 99  8 96\nCard   6: 32 94 17 27 59 63  7 87 68 53 |  2 37 31 69 17  7 75 53 87 26 51 96 19 63 59 68 34 56 35 30 93 79 89 61 71\nCard   7: 19 40 50 67  3  2 79 33 14 98 | 51 30 70 72  2 20 35 50 94 37 40 74 14 91 33 98 67 92  3 59 79 19 97 75 31\nCard   8: 67 12 63 96 61 48 95 40 73 46 | 67 12 40 63 15 46 33 16 43 92 78 74 95 73  4 53 14  9 90 94 72 13 96 54 61\nCard   9: 47 23 84 63 95 98 26 90 99 64 | 98 95  4 47  1 23 26 11 74 84 36 82 63 60 53 44  6 59 64 99 17 42 90 85 19\nCard  10: 70 82 46 43 38 21  2 98 33 64 | 93  3 64 21 96 80 32 70 79 58 34 42  2 19 39 61 46 31 88 43 87 54 67 69  9\nCard  11: 44 79 34 75 85 94  2 81 55 66 | 55 85  6 58 72 51 37 44 29 79 39 66 49 48 75 15 83 81  2 24 94 99 34 59 33\nCard  12: 74 34  8 69 16 83 80 97 22 29 | 96 97  5 87 50 84 94 19 93 52 40 46 15  7 41 37 98 12 16 56 65 95 27 90 54\nCard  13:  2 56  8 43 13 72 33 11 36 29 | 57 40 14 35 18 65 78 87 58 84 51  2 43 61 37 66 30 10 39 60 64 54 48  9 15\nCard  14: 43 84 95 53 44 69  9 94  5 17 | 65 74 88 75 81 10 62 83 33 51 76 26 89 94 80 17 56 42 21  6 87  1 46 95  9\nCard  15: 57  7 28  1  5 11 81 43 69 34 | 52 49  8 91 34  2 29 74 94 81 77 58 11 83 96 43 40 55 89 27 10 30  7 57 28\nCard  16:  6 14 70 53 54 52 26 72 59 42 | 37 52 60 53 86 49 91 33  4 77 64 51 23 85 89 10 42  8 29 66 61 74 99 26 87\nCard  17: 10 86 55 81 40 23 25 67 93 59 | 68 62 15 32 54 85 99  5 10  2 56 38 64 11 55 84 50 67 34 12 52 59 76  1 36\nCard  18: 32 90 99 60 31 54 58 78 88 61 | 63 93 58 47 80 60 46 88 21 48 83 31 35 64 23 24 55 91  1 73 45 20 29 77 66\nCard  19: 80  1 28 63 37 42 24 81 99 94 | 15 10 33 88 28  2 16 62 81 72 42 67 49 38 65 95 63 61 24 52 11  6 26 39 70\nCard  20: 44 26 16  4 55 63 33 36 82 86 | 70  3 46 58 41 95 96 73 15 13 99 69 91 75 87 57 92 65 51 38 53 80 14 52 48\nCard  21: 48 36 78  7 17  2 61 54 49 11 | 56 41 84 37 45 62 40 18 35 33 43 57 80  9 32 13 25 46 61 58 55 48 30 11 74\nCard  22: 83 30  3  9 31 13 82 81 15 74 | 64 47 66 39 14 17 10 79 84 62 27 81 73 96 43 42 36 40 70 80 90 41 50 55 16\nCard  23: 84  6 52 97 89 48 62 20 15 53 | 41 66 12 56 86 61 45 38  1 51 31 92 14 96 16 37 67 98 64 57 77 10 73 39 80\nCard  24: 90  8 46 94 95 43 31 74 80 66 | 49 91 47  1 37 48 72 45 29 26 76 58 44 97 20 70 55 77 13 86 19 87 59  4 34\nCard  25: 80 52 16 86 65 84 31 36 49 59 | 95 72 65 80 52 97 59  9 81  2 96 36 86 31 16  8 48  6 50 66 84 49 78 18 56\nCard  26: 32 56 87 82 62 97 42 22 61 83 | 85 29 58 59 48 20 69 50 98 46 60 25 40 52 73 28 66 63  1  6 74 12 88 37 64\nCard  27: 46 65 51 32 83 91 67 52 43 71 | 98 83 43 46 36 71 38 14 30 26  5 47  3 85 61 51  4 91 35 28 67 48 60 52 74\nCard  28: 26 60 29 89 24 28 95 92 78 97 | 39 95 41 34 16 25 62 60 53 97 24 92 77 78 29 68 35 86 71 58 89 12 28  9 11\nCard  29: 66  6 93 49 51  2 48 96 62 89 |  2 17 62 39  6 51 93 18 95 54 72  1 97 66 84 96 60 89 90 69 13 48 49 36 16\nCard  30: 53 95 92 75 76 56 48 87 79 15 |  1  4 97  3  6 79 11 76 88  7 56 75 48 95 60 94 59 36 46 86 70 69 91  2 73\nCard  31: 80 63 37 67 35 98 97 72 87 52 | 67 77 58 66 54 19 87 75 76 51 29 80 53 26 24 49 88 91 52 72 86 81  4 39 37\nCard  32: 67 23 77 16  4 31 24 13 76 86 | 16  6 69 31 51  4 28 83  1 89 79 24 46 38 99 77 43 50 19 87 90 73 85 56 70\nCard  33: 38 41 64 25 96 99 68 58 92 40 | 84 59  5 94 49  6  2 40 73 96 38 23 43 91 47 88 24 17  9 25 35 52 13 92 64\nCard  34: 24 52 69 59 55  8 43 15 45 92 | 56 47 84 62  4 81 88 92 54 96 52 89 42 46 49 33 69 10 51 12  5 82 85 65 20\nCard  35: 19 82 84 29 58 44 65 87 26 32 | 95 63 13 56 77  3 57 17 32 20 81  2 82 76 48 39 71 93 84  8 15 88 89 74 11\nC" <> ...

Solution

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

  def part1(input) do
    input
    |> parse()
    |> Enum.map(fn {_game, win, input} ->
      case length(winners(win, input)) do
        0 -> 0
        1 -> 1
        x -> 2 ** (x - 1)
      end
    end)
    |> Enum.sum()
  end

  def part2(input) do
    games =
      input
      |> parse()
      |> Enum.map(fn {game, win, input} ->
        {game, winners(win, input) |> length()}
      end)

    card_count = for {x, _} <- games, into: %{}, do: {x, 1}

    Enum.reduce(games, card_count, fn
      {_, 0}, acc ->
        acc

      {game, wins}, acc ->
        Enum.reduce(1..acc[game], acc, fn _, acc ->
          Enum.reduce(1..wins, acc, fn n, acc ->
            Map.update!(acc, game + n, &amp;(&amp;1 + 1))
          end)
        end)
    end)
    |> Map.values()
    |> Enum.sum()
  end

  defp winners(win, input) do
    input
    |> Enum.filter(fn x -> x in win end)
    |> Enum.uniq()
  end

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

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

    def parse_line(line) do
      [game, win, input] =
        ~r/Card\s+(\d+): ([\d\s]+) \| ([\d\s]+)/
        |> Regex.run(line, capture: :all_but_first)

      {
        String.to_integer(game),
        String.split(win, " ", trim: true) |> Enum.map(&amp;String.to_integer/1),
        String.split(input, " ", trim: true) |> Enum.map(&amp;String.to_integer/1)
      }
    end
  end
end
{:module, Day04, <<70, 79, 82, 49, 0, 0, 16, ...>>,
 {:module, Day04.Input, <<70, 79, 82, ...>>, {:parse_line, 1}}}

Take a seat in the large pile of colorful cards. How many points are they worth in total?

Your puzzle answer was 22193.

Day04.part1(input)
22193

Process all of the original and copied scratchcards until no more scratchcards are won. Including the original set of scratchcards, how many total scratchcards do you end up with?

Your puzzle answer was 5625994.

Day04.part2(input)
5625994

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

  setup_all do
    [
      input:
        "Card 1: 41 48 83 86 17 | 83 86  6 31 17  9 48 53\nCard 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19\nCard 3:  1 21 53 59 44 | 69 82 63 72 16 21 14  1\nCard 4: 41 92 73 84 69 | 59 84 76 51 58  5 54 83\nCard 5: 87 83 26 28 32 | 88 30 70 12 93 22 82 36\nCard 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11"
    ]
  end

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

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

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

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

Benchmarking

# https://github.com/bencheeorg/benchee
Benchee.run(
  %{
    "Part 1" => fn -> Day04.part1(input) end,
    "Part 2" => fn -> Day04.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        1.72 K        0.58 ms     ±2.53%        0.58 ms        0.63 ms
Part 2     0.00248 K      402.56 ms     ±0.67%      402.44 ms      407.40 ms

Comparison: 
Part 1        1.72 K
Part 2     0.00248 K - 693.06x slower +401.98 ms

Memory usage statistics:

Name           average  deviation         median         99th %
Part 1      0.00054 GB     ±0.00%     0.00054 GB     0.00054 GB
Part 2         1.58 GB     ±0.00%        1.58 GB        1.58 GB

Comparison: 
Part 1      0.00054 GB
Part 2         1.58 GB - 2935.47x memory usage +1.58 GB

Reduction count statistics:

Name           average  deviation         median         99th %
Part 1        0.0875 M     ±0.00%       0.0875 M       0.0875 M
Part 2         45.36 M     ±0.00%        45.36 M        45.36 M

Comparison: 
Part 1        0.0875 M
Part 2         45.36 M - 518.10x reduction count +45.27 M
nil