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

Day 5 - Advent of Code 2024

lib/advent_of_code/2024/day-05.livemd

Day 5 - Advent of Code 2024

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

Links

Prompt

— Day 5: Print Queue —

Satisfied with their search on Ceres, the squadron of scholars suggests subsequently scanning the stationery stacks of sub-basement 17.

The North Pole printing department is busier than ever this close to Christmas, and while The Historians continue their search of this historically significant facility, an Elf operating a very familiar printer beckons you over.

The Elf must recognize you, because they waste no time explaining that the new sleigh launch safety manual updates won’t print correctly. Failure to update the safety manuals would be dire indeed, so you offer your services.

Safety protocols clearly indicate that new pages for the safety manuals must be printed in a very specific order. The notation X|Y means that if both page number X and page number Y are to be produced as part of an update, page number X must be printed at some point before page number Y.

The Elf has for you both the page ordering rules and the pages to produce in each update (your puzzle input), but can’t figure out whether each update has the pages in the right order.

For example:

47|53
97|13
97|61
97|47
75|29
61|13
75|53
29|13
97|29
53|29
61|53
97|53
61|29
47|13
75|47
97|75
47|61
75|61
47|29
75|13
53|13

75,47,61,53,29
97,61,53,29,13
75,29,13
75,97,47,61,53
61,13,29
97,13,75,29,47

The first section specifies the page ordering rules, one per line. The first rule, 47|53, means that if an update includes both page number 47 and page number 53, then page number 47 must be printed at some point before page number 53. (47 doesn’t necessarily need to be immediately before 53; other pages are allowed to be between them.)

The second section specifies the page numbers of each update. Because most safety manuals are different, the pages needed in the updates are different too. The first update, 75,47,61,53,29, means that the update consists of page numbers 75, 47, 61, 53, and 29.

To get the printers going as soon as possible, start by identifying which updates are already in the right order.

In the above example, the first update (75,47,61,53,29) is in the right order:

  • 75 is correctly first because there are rules that put each other page after it: 75|47, 75|61, 75|53, and 75|29.
  • 47 is correctly second because 75 must be before it (75|47) and every other page must be after it according to 47|61, 47|53, and 47|29.
  • 61 is correctly in the middle because 75 and 47 are before it (75|61 and 47|61) and 53 and 29 are after it (61|53 and 61|29).
  • 53 is correctly fourth because it is before page number 29 (53|29).
  • 29 is the only page left and so is correctly last. Because the first update does not include some page numbers, the ordering rules involving those missing page numbers are ignored.

The second and third updates are also in the correct order according to the rules. Like the first update, they also do not include every page number, and so only some of the ordering rules apply - within each update, the ordering rules that involve missing page numbers are not used.

The fourth update, 75,97,47,61,53, is not in the correct order: it would print 75 before 97, which violates the rule 97|75.

The fifth update, 61,13,29, is also not in the correct order, since it breaks the rule 29|13.

The last update, 97,13,75,29,47, is not in the correct order due to breaking several rules.

For some reason, the Elves also need to know the middle page number of each update being printed. Because you are currently only printing the correctly-ordered updates, you will need to find the middle page number of each correctly-ordered update. In the above example, the correctly-ordered updates are:

75,47,*61*,53,29
97,61,*53*,29,13
75,*29*,13

These have middle page numbers of 61, 53, and 29 respectively. Adding these page numbers together gives 143.

Of course, you’ll need to be careful: the actual list of page ordering rules is bigger and more complicated than the above example.

Determine which updates are already in the correct order. What do you get if you add up the middle page number from those correctly-ordered updates?

To begin, get your puzzle input.

— Part Two —

While the Elves get to work printing the correctly-ordered updates, you have a little time to fix the rest of them.

For each of the incorrectly-ordered updates, use the page ordering rules to put the page numbers in the right order. For the above example, here are the three incorrectly-ordered updates and their correct orderings:

  • 75,97,47,61,53 becomes 97,75,*47*,61,53.
  • 61,13,29 becomes 61,*29*,13.
  • 97,13,75,29,47 becomes 97,75,*47*,29,13. After taking only the incorrectly-ordered updates and ordering them correctly, their middle page numbers are 47, 29, and 47. Adding these together produces 123.

Find the updates which are not in the correct order. What do you get if you add up the middle page numbers after correctly ordering just those updates?

Although it hasn’t changed, you can still get your puzzle input.

Input

input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
"39|57\n31|97\n31|75\n29|45\n29|73\n29|84\n66|91\n66|39\n66|57\n66|54\n97|16\n97|38\n97|59\n97|11\n97|62\n87|38\n87|59\n87|18\n87|93\n87|13\n87|33\n25|55\n25|22\n25|31\n25|64\n25|33\n25|85\n25|93\n68|11\n68|57\n68|36\n68|78\n68|66\n68|73\n68|39\n68|27\n32|29\n32|73\n32|45\n32|14\n32|48\n32|39\n32|54\n32|74\n32|17\n17|76\n17|45\n17|25\n17|14\n17|91\n17|11\n17|27\n17|38\n17|16\n17|22\n34|66\n34|91\n34|64\n34|29\n34|74\n34|65\n34|68\n34|26\n34|36\n34|13\n34|93\n48|76\n48|34\n48|55\n48|22\n48|42\n48|89\n48|14\n48|85\n48|87\n48|23\n48|38\n48|79\n73|62\n73|13\n73|42\n73|25\n73|55\n73|26\n73|65\n73|59\n73|18\n73|21\n73|64\n73|34\n73|79\n52|21\n52|18\n52|13\n52|22\n52|32\n52|55\n52|85\n52|42\n52|93\n52|39\n52|26\n52|66\n52|34\n52|68\n74|27\n74|57\n74|48\n74|73\n74|91\n74|16\n74|87\n74|38\n74|54\n74|75\n74|29\n74|62\n74|69\n74|84\n74|23\n38|37\n38|21\n38|13\n38|42\n38|64\n38|55\n38|66\n38|23\n38|85\n38|32\n38|79\n38|34\n38|89\n38|33\n38|52\n38|22\n69|65\n69|62\n69|38\n69|55\n69|34\n69|37\n69|33\n69|79\n69|21\n69|18\n69|87\n69|23\n69|85\n69|22\n69|16\n69|93\n69|89\n11|18\n11|85\n11|89\n11|59\n11|52\n11|22\n11|26\n11|87\n11|25\n11|76\n11|37\n11|23\n11|42\n11|21\n11|62\n11|38\n11|13\n11|69\n33|97\n33|27\n33|68\n33|29\n33|44\n33|31\n33|93\n33|66\n33|32\n33|84\n33|11\n33|45\n33|17\n33|54\n33|78\n33|75\n33|39\n33|36\n33|91\n91|79\n91|55\n91|22\n91|87\n91|23\n91|36\n91|25\n91|16\n91|76\n91|45\n91|59\n91|11\n91|75\n91|29\n91|62\n91|27\n91|69\n91|54\n91|48\n91|52\n75|34\n75|21\n75|42\n75|25\n75|76\n75|79\n75|23\n75|89\n75|18\n75|22\n75|26\n75|62\n75|69\n75|73\n75|11\n75|85\n75|37\n75|14\n75|52\n75|59\n75|38\n59|55\n59|13\n59|79\n59|32\n59|65\n59|93\n59|42\n59|52\n59|18\n59|44\n59|37\n59|85\n59|22\n59|25\n59|68\n59|33\n59|38\n59|31\n59|21\n59|23\n59|89\n59|26\n26|29\n26|39\n26|44\n26|31\n26|91\n26|17\n26|32\n26|93\n26|74\n26|66\n26|48\n26|57\n26|36\n26|97\n26|13\n26|45\n26|64\n26|65\n26|27\n26|33\n26|84\n26|78\n26|68\n79|42\n79|26\n79|57\n79|31\n79|89\n79|93\n79|18\n79|17\n79|33\n79|68\n79|74\n79|39\n79|85\n79|37\n79|78\n79|21\n79|44\n79|66\n79|65\n79|34\n79|64\n79|32\n79|97\n79|13\n42|37\n42|18\n42|13\n42|97\n42|29\n42|39\n42|78\n42|64\n42|65\n42|66\n42|89\n42|26\n42|44\n42|57\n42|85\n42|34\n42|32\n42|93\n42|31\n42|74\n42|91\n42|17\n42|33\n42|68\n18|17\n18|97\n18|44\n18|36\n18|29\n18|33\n18|91\n18|27\n18|64\n18|74\n18|57\n18|65\n18|13\n18|68\n18|84\n18|26\n18|78\n18|39\n18|66\n18|54\n18|93\n18|31\n18|32\n18|37\n22|26\n22|93\n22|13\n22|78\n22|66\n22|37\n22|31\n22|97\n22|34\n22|32\n22|85\n22|21\n22|39\n22|42\n22|65\n22|64\n22|44\n22|33\n22|89\n22|74\n22|79\n22|68\n22|55\n22|18\n62|44\n62|25\n62|85\n62|22\n62|64\n62|33\n62|13\n62|87\n62|26\n62|89\n62|16\n62|23\n62|79\n62|38\n62|59\n62|93\n62|55\n62|37\n62|18\n62|21\n62|42\n62|34\n62|65\n62|52\n21|34\n21|66\n21|93\n21|17\n21|44\n21|37\n21|68\n21|26\n21|74\n21|57\n21|18\n21|91\n21|65\n21|13\n21|31\n21|89\n21|32\n21|85\n21|39\n21|97\n21|78\n21|42\n21|64\n21|33\n93|84\n93|11\n93|57\n93|45\n93|36\n93|29\n93|76\n93|39\n93|31\n93|14\n93|91\n93|32\n93|66\n93|75\n93|48\n93|97\n93|78\n93|73\n93|74\n93|54\n93|17\n93|27\n93|44\n93|68\n23|32\n23|26\n23|68\n23|79\n23|39\n23|64\n23|25\n23|65\n23|22\n23|85\n23|31\n23|93\n23|55\n23|21\n23|13\n23|89\n23|37\n23|66\n23|52\n23|18\n23|33\n23|44\n23|42\n23|34\n84|52\n84|73\n84|11\n84|55\n84|14\n84|87\n84|76\n84|89\n84|16\n84|62\n84|48\n84|42\n84|54\n84|21\n84|69\n84|38\n84|22\n84|45\n84|79\n84|34\n84|23\n84|75\n84|59\n84|25\n16|22\n16|18\n16|85\n16|65\n16|37\n16|44\n16|13\n16|21\n16|38\n16|59\n16|25\n16|23\n16|87\n16|55\n16|68\n16|52\n16|89\n16|33\n16|34\n16|64\n16|42\n16|79\n16|26\n16|93\n85|44\n85|65\n85|39\n85|37\n85|84\n85|57\n85|64\n85|26\n85|32\n85|91\n85|74\n85|33\n85|18\n85|27\n85|78\n85|93\n85|17\n85|97\n85|66\n85|31\n85|36\n85|29\n85|13\n85|68\n55|26\n55|89\n55|64\n55|37\n55|78\n55|44\n55|93\n55|68\n55|39\n55|97\n55|79\n55|17\n55|21\n55|31\n55|85\n55|66\n55|13\n55|65\n55|42\n55|18\n55|34\n55|33\n55|32\n55|74\n65|75\n65|66\n65|11\n65|78\n65|39\n65|68\n65|44\n65|97\n65|36\n65|31\n65|45\n65|91\n65|14\n65|48\n65|33\n65|57\n65|84\n65|17\n65|93\n65|54\n65|27\n65|29\n65|74\n65|32\n76|37\n76|25\n76|42\n76|69\n76|23\n76|18\n76|16\n76|89\n76|62\n76|52\n76|34\n76|55\n76|21\n76|13\n76|79\n76|38\n76|65\n76|85\n76|26\n76|59\n76|87\n76|73\n76|64\n76|22\n27|21\n27|55\n27|38\n27|79\n27|59\n27|73\n27|75\n27|11\n27|16\n27|62\n27|42\n27|23\n27|76\n27|22\n27|45\n27|34\n27|52\n27|84\n27|87\n27|25\n27|48\n27|69\n27|54\n27|14\n14|52\n14|73\n14|22\n14|69\n14|25\n14|34\n14|16\n14|23\n14|21\n14|37\n14|38\n14|64\n14|87\n14|85\n14|59\n14|26\n14|13\n14|18\n14|76\n14|42\n14|62\n14|79\n14|55\n14|89\n36|48\n36|75\n36|23\n36|21\n36|73\n36|84\n36|54\n36|87\n36|25\n36|62\n36|42\n36|16\n36|79\n36|76\n36|27\n36|38\n36|11\n36|22\n36|69\n36|52\n36|45\n36|14\n36|5" <> ...

Solution

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

  def part1(input) do
    {rules, updates} = parse(input)

    updates
    |> Enum.filter(&amp;correct_order?(&amp;1, rules))
    |> Enum.map(&amp;get_middle/1)
    |> Enum.sum()
  end

  def part2(input) do
    {rules, updates} = parse(input)

    updates
    |> Enum.reject(&amp;correct_order?(&amp;1, rules))
    |> Enum.map(&amp;reorder(&amp;1, rules))
    |> Enum.map(&amp;get_middle/1)
    |> Enum.sum()
  end

  defp correct_order?(update, rules) do
    Enum.all?(rules, &amp;rule_met?(&amp;1, update))
  end

  defp rule_met?(rule, update) do
    {idx_a, idx_b} = find_indexes(rule, update)

    cond do
      is_nil(idx_a) -> true
      is_nil(idx_b) -> true
      idx_a < idx_b -> true
      true -> false
    end
  end

  defp find_indexes({a, b}, update) do
    {
      Enum.find_index(update, &amp;(&amp;1 == a)),
      Enum.find_index(update, &amp;(&amp;1 == b))
    }
  end

  defp get_middle(list) do
    middle_index = (length(list) / 2) |> floor()

    Enum.at(list, middle_index)
  end

  defp reorder(update, rules), do: reorder(update, rules, rules)

  defp reorder(update, [], _), do: update

  defp reorder(update, [rule | tail], all_rules) do
    if rule_met?(rule, update) do
      reorder(update, tail, all_rules)
    else
      # Move b to be immediately after a
      {_a, b} = rule
      {idx_a, idx_b} = find_indexes(rule, update)

      update
      |> List.insert_at(idx_a + 1, b)
      |> List.delete_at(idx_b)
      |> reorder(all_rules)
    end
  end

  defmodule Input do
    def parse(input) do
      [rules, updates] = String.split(input, "\n\n")

      {
        parse_lines(rules, &amp;parse_rule/1),
        parse_lines(updates, &amp;parse_update/1)
      }
    end

    def parse_lines(input, fun) do
      input
      |> String.split("\n", trim: true)
      |> Enum.map(&amp;fun.(&amp;1))
    end

    def parse_rule(rule) do
      rule
      |> String.split("|")
      |> Enum.map(&amp;String.to_integer/1)
      |> List.to_tuple()
    end

    def parse_update(update) do
      update
      |> String.split(",")
      |> Enum.map(&amp;String.to_integer/1)
    end
  end
end
{:module, Day05, <<70, 79, 82, 49, 0, 0, 18, ...>>,
 {:module, Day05.Input, <<70, 79, 82, ...>>, {:parse_update, 1}}}

Determine which updates are already in the correct order. What do you get if you add up the middle page number from those correctly-ordered updates?

Your puzzle answer was 5964.

Day05.part1(input)
5964

Find the updates which are not in the correct order. What do you get if you add up the middle page numbers after correctly ordering just those updates?

Your puzzle answer was 4719.

Day05.part2(input)
4719

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

  setup_all do
    [
      input:
        "47|53\n97|13\n97|61\n97|47\n75|29\n61|13\n75|53\n29|13\n97|29\n53|29\n61|53\n97|53\n61|29\n47|13\n75|47\n97|75\n47|61\n75|61\n47|29\n75|13\n53|13\n\n75,47,61,53,29\n97,61,53,29,13\n75,29,13\n75,97,47,61,53\n61,13,29\n97,13,75,29,47"
    ]
  end

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

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

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

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

Benchmarking

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

    nil
  end
end
{:module, Benchmarking, <<70, 79, 82, 49, 0, 0, 7, ...>>, {:run, 1}}
Benchmarking.run(input)
Operating System: macOS
CPU Information: Apple M1
Number of Available Cores: 8
Available memory: 8 GB
Elixir 1.15.7
Erlang 26.1.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         33.31       30.02 ms     ±4.95%       29.88 ms       36.70 ms
Part 2          2.85      350.67 ms     ±1.52%      349.18 ms      369.89 ms

Comparison: 
Part 1         33.31
Part 2          2.85 - 11.68x slower +320.66 ms

Memory usage statistics:

Name      Memory usage
Part 1        16.27 MB
Part 2       173.24 MB - 10.65x memory usage +156.97 MB

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

Reduction count statistics:

Name   Reduction count
Part 1         11.79 M
Part 2        140.68 M - 11.93x reduction count +128.90 M

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