Powered by AppSignal & Oban Pro

Day 5 - Advent of Code 2022

lib/advent_of_code/2022/day-05.livemd

Day 5 - Advent of Code 2022

Mix.install([:kino])
:ok

Links

Prompt

— Day 5: Supply Stacks —

The expedition can depart as soon as the final supplies have been unloaded from the ships. Supplies are stored in stacks of marked crates, but because the needed supplies are buried under many other crates, the crates need to be rearranged.

The ship has a giant cargo crane capable of moving crates between stacks. To ensure none of the crates get crushed or fall over, the crane operator will rearrange them in a series of carefully-planned steps. After the crates are rearranged, the desired crates will be at the top of each stack.

The Elves don’t want to interrupt the crane operator during this delicate procedure, but they forgot to ask her which crate will end up where, and they want to be ready to unload them as soon as possible so they can embark.

They do, however, have a drawing of the starting stacks of crates and the rearrangement procedure (your puzzle input). For example:

    [D]    
[N] [C]    
[Z] [M] [P]
 1   2   3 

move 1 from 2 to 1
move 3 from 1 to 3
move 2 from 2 to 1
move 1 from 1 to 2

In this example, there are three stacks of crates. Stack 1 contains two crates: crate Z is on the bottom, and crate N is on top. Stack 2 contains three crates; from bottom to top, they are crates M, C, and D. Finally, stack 3 contains a single crate, P.

Then, the rearrangement procedure is given. In each step of the procedure, a quantity of crates is moved from one stack to a different stack. In the first step of the above rearrangement procedure, one crate is moved from stack 2 to stack 1, resulting in this configuration:

[D]        
[N] [C]    
[Z] [M] [P]
 1   2   3 

In the second step, three crates are moved from stack 1 to stack 3. Crates are moved one at a time, so the first crate to be moved (D) ends up below the second and third crates:

        [Z]
        [N]
    [C] [D]
    [M] [P]
 1   2   3

Then, both crates are moved from stack 2 to stack 1. Again, because crates are moved one at a time, crate C ends up below crate M:

        [Z]
        [N]
[M]     [D]
[C]     [P]
 1   2   3

Finally, one crate is moved from stack 1 to stack 2:

        [Z]
        [N]
        [D]
[C] [M] [P]
 1   2   3

The Elves just need to know which crate will end up on top of each stack; in this example, the top crates are C in stack 1, M in stack 2, and Z in stack 3, so you should combine these together and give the Elves the message CMZ.

After the rearrangement procedure completes, what crate ends up on top of each stack?

— Part Two —

As you watch the crane operator expertly rearrange the crates, you notice the process isn’t following your prediction.

Some mud was covering the writing on the side of the crane, and you quickly wipe it away. The crane isn’t a CrateMover 9000 - it’s a CrateMover 9001.

The CrateMover 9001 is notable for many new and exciting features: air conditioning, leather seats, an extra cup holder, and the ability to pick up and move multiple crates at once.

Again considering the example above, the crates begin in the same configuration:

    [D]    
[N] [C]    
[Z] [M] [P]
 1   2   3 

Moving a single crate from stack 2 to stack 1 behaves the same as before:

[D]        
[N] [C]    
[Z] [M] [P]
 1   2   3 

However, the action of moving three crates from stack 1 to stack 3 means that those three moved crates stay in the same order, resulting in this new configuration:

        [D]
        [N]
    [C] [Z]
    [M] [P]
 1   2   3

Next, as both crates are moved from stack 2 to stack 1, they retain their order as well:

        [D]
        [N]
[C]     [Z]
[M]     [P]
 1   2   3

Finally, a single crate is still moved from stack 1 to stack 2, but now it’s crate C that gets moved:

        [D]
        [N]
        [Z]
[M] [C] [P]
 1   2   3

In this example, the CrateMover 9001 has put the crates in a totally different order: MCD.

Before the rearrangement process finishes, update your simulation so that the Elves know where they should stand to be ready to unload the final supplies. After the rearrangement procedure completes, what crate ends up on top of each stack?

To begin, get your puzzle input.

Input

input = Kino.Input.textarea("Please paste your input file:")
input = input |> Kino.Input.read()
"            [J] [Z] [G]            \n            [Z] [T] [S] [P] [R]    \n[R]         [Q] [V] [B] [G] [J]    \n[W] [W]     [N] [L] [V] [W] [C]    \n[F] [Q]     [T] [G] [C] [T] [T] [W]\n[H] [D] [W] [W] [H] [T] [R] [M] [B]\n[T] [G] [T] [R] [B] [P] [B] [G] [G]\n[S] [S] [B] [D] [F] [L] [Z] [N] [L]\n 1   2   3   4   5   6   7   8   9 \n\nmove 4 from 2 to 1\nmove 1 from 6 to 9\nmove 6 from 4 to 7\nmove 1 from 2 to 5\nmove 3 from 6 to 3\nmove 4 from 3 to 9\nmove 2 from 1 to 3\nmove 6 from 7 to 5\nmove 5 from 7 to 6\nmove 6 from 8 to 7\nmove 6 from 7 to 6\nmove 1 from 8 to 3\nmove 15 from 6 to 4\nmove 7 from 5 to 6\nmove 1 from 7 to 2\nmove 2 from 5 to 3\nmove 5 from 9 to 8\nmove 5 from 5 to 6\nmove 1 from 7 to 4\nmove 5 from 6 to 5\nmove 3 from 3 to 8\nmove 4 from 5 to 8\nmove 1 from 2 to 8\nmove 7 from 1 to 2\nmove 2 from 6 to 2\nmove 2 from 5 to 8\nmove 1 from 1 to 8\nmove 8 from 2 to 6\nmove 3 from 3 to 4\nmove 4 from 9 to 3\nmove 5 from 3 to 6\nmove 5 from 6 to 8\nmove 3 from 4 to 8\nmove 13 from 6 to 5\nmove 14 from 4 to 8\nmove 1 from 2 to 6\nmove 1 from 4 to 2\nmove 12 from 5 to 4\nmove 30 from 8 to 6\nmove 1 from 8 to 9\nmove 1 from 9 to 4\nmove 15 from 4 to 5\nmove 1 from 2 to 9\nmove 1 from 4 to 2\nmove 1 from 2 to 1\nmove 1 from 9 to 3\nmove 8 from 5 to 7\nmove 2 from 5 to 6\nmove 7 from 8 to 1\nmove 1 from 3 to 4\nmove 1 from 7 to 3\nmove 1 from 4 to 6\nmove 26 from 6 to 7\nmove 1 from 3 to 7\nmove 3 from 7 to 2\nmove 1 from 1 to 9\nmove 16 from 7 to 5\nmove 2 from 7 to 4\nmove 12 from 7 to 6\nmove 1 from 1 to 9\nmove 4 from 6 to 1\nmove 7 from 1 to 5\nmove 2 from 1 to 8\nmove 1 from 7 to 2\nmove 1 from 1 to 4\nmove 2 from 4 to 5\nmove 1 from 9 to 4\nmove 3 from 6 to 9\nmove 8 from 6 to 5\nmove 5 from 5 to 9\nmove 19 from 5 to 8\nmove 1 from 9 to 8\nmove 3 from 8 to 7\nmove 1 from 7 to 3\nmove 8 from 5 to 2\nmove 2 from 4 to 2\nmove 4 from 9 to 8\nmove 1 from 2 to 3\nmove 2 from 3 to 2\nmove 4 from 9 to 5\nmove 8 from 8 to 4\nmove 9 from 8 to 5\nmove 5 from 8 to 4\nmove 5 from 5 to 7\nmove 12 from 2 to 3\nmove 2 from 2 to 8\nmove 1 from 8 to 6\nmove 1 from 8 to 7\nmove 10 from 4 to 3\nmove 1 from 2 to 9\nmove 13 from 5 to 3\nmove 1 from 7 to 5\nmove 27 from 3 to 4\nmove 1 from 8 to 7\nmove 3 from 5 to 2\nmove 6 from 6 to 3\nmove 2 from 4 to 1\nmove 27 from 4 to 2\nmove 2 from 7 to 8\nmove 23 from 2 to 4\nmove 2 from 1 to 4\nmove 2 from 7 to 2\nmove 4 from 2 to 9\nmove 10 from 3 to 4\nmove 1 from 3 to 5\nmove 1 from 5 to 1\nmove 5 from 2 to 5\nmove 30 from 4 to 2\nmove 1 from 8 to 9\nmove 1 from 8 to 1\nmove 27 from 2 to 3\nmove 2 from 4 to 2\nmove 1 from 9 to 4\nmove 2 from 1 to 3\nmove 8 from 3 to 7\nmove 19 from 3 to 1\nmove 1 from 4 to 7\nmove 5 from 9 to 1\nmove 4 from 2 to 9\nmove 4 from 3 to 4\nmove 1 from 3 to 5\nmove 1 from 2 to 7\nmove 1 from 9 to 3\nmove 1 from 9 to 1\nmove 5 from 5 to 4\nmove 5 from 7 to 3\nmove 1 from 5 to 6\nmove 23 from 1 to 6\nmove 1 from 9 to 2\nmove 1 from 2 to 5\nmove 24 from 6 to 9\nmove 6 from 4 to 7\nmove 4 from 4 to 8\nmove 1 from 4 to 9\nmove 4 from 7 to 4\nmove 4 from 3 to 4\nmove 4 from 9 to 8\nmove 6 from 7 to 9\nmove 4 from 7 to 6\nmove 1 from 1 to 4\nmove 2 from 6 to 4\nmove 1 from 6 to 2\nmove 1 from 1 to 8\nmove 1 from 7 to 3\nmove 1 from 6 to 9\nmove 13 from 4 to 2\nmove 3 from 3 to 2\nmove 15 from 9 to 8\nmove 1 from 5 to 9\nmove 5 from 9 to 1\nmove 4 from 1 to 7\nmove 4 from 7 to 3\nmove 8 from 2 to 7\nmove 9 from 8 to 2\nmove 1 from 1 to 2\nmove 7 from 9 to 2\nmove 4 from 3 to 1\nmove 4 from 1 to 4\nmove 2 from 9 to 1\nmove 20 from 2 to 8\nmove 3 from 4 to 8\nmove 1 from 2 to 3\nmove 4 from 2 to 7\nmove 1 from 3 to 4\nmove 1 from 9 to 3\nmove 1 from 4 to 7\nmove 1 from 2 to 5\nmove 1 from 4 to 3\nmove 2 from 1 to 6\nmove 1 from 5 to 6\nmove 1 from 7 to 1\nmove 12 from 7 to 2\nmove 12 from 2 to 6\nmove 9 from 6 to 2\nmove 1 from 6 to 8\nmove 1 from 3 to 9\nmove 8 from 2 to 4\nmove 1 from 9 to 6\nmove 1 from 4 to 6\nmove 4 from 4 to 9\nmove 1 from 4 to 9\nmove 1 from 1 to 5\nmove 2 from 6 to 3\nmove 1 from 5 to 4\nmove 1 from 2 to 8\nmove 10 from 8 to 6\nmove 10 from 8 to 3\nmove 1 from 3 to 4\nmove 8 from 8 to 1\nmove 3 from 9 to 8\nmove 2 from 9 to 1\nmove 11 from 6 to 7\nmove 1 from 1 to 7\nmove 8 from 1 to 4\nmove 3 from 6 to 7\nmove 1 from 1 to 4\nmove 14 from 8 to" <> ...

Solution

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

  def part1(input) do
    {crates, moves} = parse(input)

    moves
    |> Enum.reduce(crates, &amp;move_one_at_a_time/2)
    |> Map.values()
    |> Enum.map(&amp;hd/1)
    |> Enum.join("")
  end

  def part2(input) do
    {crates, moves} = parse(input)

    moves
    |> Enum.reduce(crates, &amp;move_in_groups/2)
    |> Map.values()
    |> Enum.map(&amp;hd/1)
    |> Enum.join("")
  end

  def move_one_at_a_time({x, from, to}, crate_map) do
    Enum.reduce(
      1..x,
      crate_map,
      fn _, acc ->
        [crate | tail] = Map.get(acc, from)

        acc
        |> Map.put(from, tail)
        |> Map.update!(to, &amp;[crate | &amp;1])
      end
    )
  end

  def move_in_groups({x, from, to}, crate_map) do
    {moving, staying} = crate_map[from] |> Enum.split(x)

    crate_map
    |> Map.put(from, staying)
    |> Map.update!(to, &amp;(moving ++ &amp;1))
  end

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

      {parse_crates(crates), parse_moves(moves)}
    end

    def parse_crates(crates) do
      crates
      |> String.split("\n")
      |> Enum.reverse()
      |> Enum.reduce(%{}, &amp;parse_crate_line/2)
    end

    def parse_crate_line(line, crate_map) do
      Regex.scan(~r/(\[(?.)\]|\s{4})/, line, capture: :all_names)
      |> Enum.with_index(1)
      |> Enum.reduce(crate_map, fn
        {[""], _}, acc -> acc
        {[letter], index}, acc -> Map.update(acc, index, [letter], &amp;[letter | &amp;1])
      end)
    end

    def parse_moves(moves) do
      moves
      |> String.split("\n")
      |> Enum.map(&amp;parse_move_line/1)
    end

    def parse_move_line(line) do
      Regex.run(~r/move (\d+) from (\d+) to (\d+)/, line)
      |> tl()
      |> Enum.map(&amp;String.to_integer/1)
      |> List.to_tuple()
    end
  end
end
{:module, Day05, <<70, 79, 82, 49, 0, 0, 15, ...>>,
 {:module, Day05.Input, <<70, 79, 82, ...>>, {:parse_move_line, 1}}}
Day05.Input.parse(input)
{%{
   1 => ["R", "W", "F", "H", "T", "S"],
   2 => ["W", "Q", "D", "G", "S"],
   3 => ["W", "T", "B"],
   4 => ["J", "Z", "Q", "N", "T", "W", "R", "D"],
   5 => ["Z", "T", "V", "L", "G", "H", "B", "F"],
   6 => ["G", "S", "B", "V", "C", "T", "P", "L"],
   7 => ["P", "G", "W", "T", "R", "B", "Z"],
   8 => ["R", "J", "C", "T", "M", "G", "N"],
   9 => ["W", "B", "G", "L"]
 },
 [
   {4, 2, 1},
   {1, 6, 9},
   {6, 4, 7},
   {1, 2, 5},
   {3, 6, 3},
   {4, 3, 9},
   {2, 1, 3},
   {6, 7, 5},
   {5, 7, 6},
   {6, 8, 7},
   {6, 7, 6},
   {1, 8, 3},
   {15, 6, 4},
   {7, 5, 6},
   {1, 7, 2},
   {2, 5, 3},
   {5, 9, 8},
   {5, 5, 6},
   {1, 7, 4},
   {5, 6, 5},
   {3, 3, 8},
   {4, 5, 8},
   {1, 2, 8},
   {7, 1, 2},
   {2, 6, 2},
   {2, 5, 8},
   {1, 1, 8},
   {8, 2, 6},
   {3, 3, 4},
   {4, 9, 3},
   {5, 3, 6},
   {5, 6, 8},
   {3, 4, 8},
   {13, 6, 5},
   {14, 4, 8},
   {1, 2, 6},
   {1, 4, 2},
   {12, 5, 4},
   {30, 8, 6},
   {1, 8, 9},
   {1, 9, 4},
   {15, 4, 5},
   {1, 2, 9},
   {1, 4, 2},
   {1, 2, 1},
   {1, 9, ...},
   {8, ...},
   {...},
   ...
 ]}
Day05.part1(input)
"ZRLJGSCTR"

After the rearrangement procedure completes, what crate ends up on top of each stack?

Your puzzle answer was ZRLJGSCTR.

Day05.part2(input)
"PRTTGRFPB"

After the rearrangement procedure completes, what crate ends up on top of each stack?

Your puzzle answer was PRTTGRFPB.

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:
        "    [D]    \n[N] [C]    \n[Z] [M] [P]\n 1   2   3 \n\nmove 1 from 2 to 1\nmove 3 from 1 to 3\nmove 2 from 2 to 1\nmove 1 from 1 to 2",
      crates: %{1 => ["N", "Z"], 2 => ["D", "C", "M"], 3 => ["P"]}
    ]
  end

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

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

  describe "move_one_at_a_time/2" do
    test "reverses order of the crates when moved to another stack", %{crates: crates} do
      assert %{2 => ["M"], 3 => ["C", "D", "P"]} = Day05.move_one_at_a_time({2, 2, 3}, crates)
    end
  end

  describe "move_in_groups/2" do
    test "reverses order of the crates when moved to another stack", %{crates: crates} do
      assert %{2 => ["M"], 3 => ["D", "C", "P"]} = Day05.move_in_groups({2, 2, 3}, crates)
    end
  end
end

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

Randomized with seed 265646
%{excluded: 0, failures: 0, skipped: 0, total: 4}