From 030af6ed8fde8565fd7156d18dcb3ca2d8f1ec79 Mon Sep 17 00:00:00 2001 From: zuckerberg <5-zuckerberg@users.noreply.git.neet.dev> Date: Mon, 5 Dec 2022 13:07:10 -0700 Subject: [PATCH] Day 5 --- 5/input | 513 +++++++++++++++++++++++++++++++++++++++++++++++++ 5/puzzle.txt | 109 +++++++++++ 5/solution.nix | 46 +++++ runAll.sh | 2 +- util.nix | 10 +- 5 files changed, 678 insertions(+), 2 deletions(-) create mode 100644 5/input create mode 100644 5/puzzle.txt create mode 100644 5/solution.nix diff --git a/5/input b/5/input new file mode 100644 index 0000000..08ba37e --- /dev/null +++ b/5/input @@ -0,0 +1,513 @@ +[D] [N] [F] +[H] [F] [L] [J] [H] +[R] [H] [F] [V] [G] [H] +[Z] [Q] [Z] [W] [L] [J] [B] +[S] [W] [H] [B] [H] [D] [C] [M] +[P] [R] [S] [G] [J] [J] [W] [Z] [V] +[W] [B] [V] [F] [G] [T] [T] [T] [P] +[Q] [V] [C] [H] [P] [Q] [Z] [D] [W] + 1 2 3 4 5 6 7 8 9 + +move 1 from 3 to 9 +move 2 from 2 to 1 +move 3 from 5 to 4 +move 1 from 1 to 8 +move 1 from 3 to 9 +move 1 from 5 to 7 +move 1 from 5 to 3 +move 4 from 4 to 2 +move 2 from 3 to 4 +move 1 from 3 to 2 +move 6 from 1 to 5 +move 1 from 4 to 3 +move 1 from 3 to 9 +move 4 from 2 to 4 +move 4 from 8 to 7 +move 3 from 2 to 6 +move 1 from 2 to 7 +move 5 from 5 to 6 +move 1 from 5 to 8 +move 5 from 8 to 7 +move 7 from 4 to 6 +move 15 from 6 to 4 +move 1 from 8 to 7 +move 1 from 1 to 5 +move 1 from 2 to 4 +move 2 from 4 to 8 +move 1 from 5 to 2 +move 5 from 6 to 4 +move 2 from 2 to 1 +move 1 from 9 to 4 +move 1 from 6 to 9 +move 3 from 9 to 3 +move 3 from 4 to 3 +move 1 from 6 to 1 +move 5 from 3 to 4 +move 2 from 8 to 5 +move 1 from 3 to 6 +move 1 from 6 to 2 +move 1 from 2 to 8 +move 6 from 4 to 2 +move 1 from 2 to 7 +move 1 from 5 to 3 +move 4 from 9 to 3 +move 1 from 9 to 1 +move 3 from 1 to 6 +move 1 from 9 to 7 +move 14 from 7 to 6 +move 1 from 8 to 3 +move 4 from 2 to 6 +move 3 from 3 to 8 +move 9 from 4 to 9 +move 1 from 1 to 5 +move 2 from 5 to 8 +move 3 from 8 to 2 +move 4 from 2 to 6 +move 1 from 3 to 9 +move 10 from 6 to 1 +move 5 from 9 to 8 +move 1 from 9 to 3 +move 6 from 1 to 8 +move 3 from 7 to 4 +move 2 from 4 to 5 +move 2 from 9 to 8 +move 15 from 8 to 3 +move 3 from 7 to 9 +move 8 from 4 to 3 +move 2 from 5 to 9 +move 6 from 6 to 5 +move 6 from 5 to 8 +move 1 from 7 to 8 +move 6 from 9 to 2 +move 5 from 2 to 4 +move 6 from 3 to 5 +move 5 from 5 to 8 +move 1 from 5 to 7 +move 1 from 9 to 7 +move 2 from 6 to 4 +move 12 from 8 to 2 +move 7 from 2 to 4 +move 3 from 7 to 5 +move 3 from 5 to 7 +move 3 from 7 to 9 +move 2 from 9 to 7 +move 1 from 9 to 3 +move 2 from 7 to 4 +move 3 from 1 to 9 +move 4 from 6 to 5 +move 6 from 2 to 8 +move 14 from 4 to 9 +move 7 from 9 to 6 +move 9 from 9 to 2 +move 1 from 5 to 8 +move 5 from 6 to 3 +move 3 from 1 to 9 +move 3 from 8 to 9 +move 1 from 8 to 3 +move 5 from 2 to 5 +move 1 from 4 to 9 +move 2 from 6 to 1 +move 2 from 3 to 6 +move 3 from 8 to 3 +move 2 from 6 to 3 +move 1 from 4 to 9 +move 4 from 3 to 6 +move 7 from 6 to 9 +move 10 from 9 to 2 +move 10 from 3 to 2 +move 7 from 2 to 8 +move 2 from 1 to 7 +move 13 from 3 to 7 +move 7 from 5 to 1 +move 1 from 9 to 6 +move 4 from 8 to 4 +move 2 from 3 to 2 +move 4 from 4 to 6 +move 1 from 3 to 4 +move 5 from 6 to 5 +move 3 from 5 to 7 +move 12 from 2 to 5 +move 7 from 5 to 6 +move 2 from 8 to 3 +move 7 from 6 to 2 +move 3 from 9 to 6 +move 1 from 6 to 7 +move 1 from 4 to 9 +move 2 from 7 to 6 +move 13 from 7 to 4 +move 3 from 7 to 5 +move 1 from 9 to 6 +move 12 from 4 to 3 +move 1 from 8 to 1 +move 2 from 6 to 4 +move 1 from 7 to 9 +move 2 from 9 to 8 +move 12 from 3 to 5 +move 1 from 8 to 2 +move 15 from 5 to 6 +move 2 from 4 to 6 +move 1 from 9 to 6 +move 5 from 5 to 4 +move 4 from 4 to 2 +move 2 from 1 to 5 +move 4 from 1 to 5 +move 1 from 8 to 6 +move 7 from 5 to 2 +move 22 from 2 to 3 +move 9 from 6 to 3 +move 1 from 1 to 8 +move 1 from 8 to 7 +move 23 from 3 to 6 +move 2 from 2 to 4 +move 1 from 7 to 8 +move 1 from 8 to 2 +move 19 from 6 to 9 +move 2 from 2 to 4 +move 4 from 4 to 6 +move 13 from 6 to 8 +move 12 from 9 to 1 +move 2 from 5 to 9 +move 2 from 4 to 8 +move 1 from 2 to 7 +move 1 from 7 to 1 +move 4 from 6 to 2 +move 10 from 1 to 9 +move 1 from 6 to 7 +move 11 from 8 to 2 +move 6 from 3 to 6 +move 1 from 7 to 2 +move 1 from 1 to 8 +move 2 from 6 to 7 +move 7 from 6 to 3 +move 9 from 3 to 1 +move 7 from 9 to 6 +move 1 from 8 to 7 +move 4 from 2 to 6 +move 1 from 8 to 3 +move 6 from 6 to 5 +move 9 from 9 to 3 +move 5 from 6 to 1 +move 1 from 7 to 8 +move 2 from 8 to 4 +move 1 from 4 to 2 +move 1 from 4 to 5 +move 2 from 5 to 6 +move 1 from 6 to 9 +move 9 from 1 to 4 +move 4 from 4 to 6 +move 2 from 4 to 7 +move 7 from 2 to 8 +move 5 from 6 to 7 +move 6 from 3 to 8 +move 8 from 1 to 9 +move 3 from 5 to 2 +move 2 from 3 to 9 +move 3 from 9 to 4 +move 7 from 2 to 3 +move 1 from 7 to 2 +move 10 from 3 to 2 +move 6 from 9 to 4 +move 1 from 3 to 1 +move 1 from 1 to 8 +move 4 from 8 to 5 +move 10 from 8 to 4 +move 2 from 8 to 9 +move 7 from 4 to 9 +move 6 from 2 to 6 +move 3 from 6 to 5 +move 4 from 4 to 9 +move 8 from 7 to 5 +move 1 from 9 to 2 +move 7 from 2 to 1 +move 4 from 9 to 8 +move 2 from 6 to 3 +move 2 from 3 to 2 +move 13 from 5 to 7 +move 5 from 4 to 9 +move 5 from 1 to 7 +move 3 from 5 to 8 +move 17 from 7 to 2 +move 15 from 2 to 6 +move 15 from 9 to 5 +move 1 from 9 to 5 +move 4 from 8 to 6 +move 1 from 4 to 6 +move 5 from 4 to 7 +move 5 from 2 to 7 +move 18 from 6 to 2 +move 2 from 7 to 6 +move 10 from 2 to 8 +move 2 from 2 to 3 +move 11 from 8 to 7 +move 7 from 7 to 5 +move 9 from 7 to 5 +move 3 from 7 to 5 +move 2 from 1 to 7 +move 4 from 2 to 1 +move 30 from 5 to 1 +move 1 from 3 to 1 +move 35 from 1 to 9 +move 2 from 2 to 5 +move 2 from 8 to 3 +move 20 from 9 to 2 +move 3 from 7 to 9 +move 1 from 3 to 6 +move 5 from 5 to 3 +move 18 from 2 to 5 +move 4 from 5 to 8 +move 7 from 9 to 7 +move 1 from 6 to 2 +move 3 from 8 to 5 +move 6 from 3 to 5 +move 3 from 7 to 4 +move 2 from 2 to 3 +move 1 from 4 to 5 +move 2 from 4 to 5 +move 4 from 7 to 2 +move 26 from 5 to 6 +move 2 from 2 to 7 +move 1 from 2 to 9 +move 1 from 7 to 8 +move 1 from 5 to 3 +move 2 from 8 to 3 +move 11 from 9 to 3 +move 6 from 3 to 4 +move 27 from 6 to 4 +move 33 from 4 to 3 +move 4 from 6 to 8 +move 1 from 2 to 8 +move 1 from 7 to 3 +move 4 from 8 to 9 +move 1 from 8 to 6 +move 34 from 3 to 8 +move 1 from 8 to 5 +move 1 from 2 to 9 +move 8 from 3 to 9 +move 3 from 5 to 4 +move 1 from 6 to 5 +move 27 from 8 to 9 +move 1 from 3 to 4 +move 1 from 5 to 7 +move 3 from 8 to 1 +move 11 from 9 to 1 +move 1 from 7 to 5 +move 11 from 9 to 3 +move 1 from 5 to 1 +move 1 from 8 to 7 +move 2 from 9 to 2 +move 1 from 2 to 1 +move 1 from 2 to 7 +move 2 from 8 to 2 +move 6 from 3 to 8 +move 1 from 4 to 2 +move 7 from 1 to 2 +move 1 from 7 to 1 +move 19 from 9 to 1 +move 3 from 2 to 9 +move 10 from 1 to 4 +move 2 from 9 to 1 +move 1 from 7 to 9 +move 7 from 1 to 6 +move 10 from 4 to 3 +move 14 from 1 to 7 +move 2 from 9 to 1 +move 3 from 4 to 6 +move 9 from 7 to 6 +move 1 from 3 to 5 +move 4 from 8 to 5 +move 10 from 6 to 8 +move 3 from 5 to 6 +move 10 from 3 to 4 +move 4 from 3 to 7 +move 1 from 5 to 9 +move 2 from 7 to 9 +move 1 from 1 to 9 +move 6 from 2 to 4 +move 1 from 5 to 3 +move 11 from 4 to 9 +move 3 from 4 to 9 +move 1 from 2 to 7 +move 2 from 3 to 5 +move 1 from 3 to 2 +move 7 from 7 to 2 +move 2 from 5 to 8 +move 8 from 2 to 1 +move 2 from 6 to 8 +move 9 from 6 to 8 +move 3 from 8 to 2 +move 3 from 2 to 6 +move 9 from 9 to 5 +move 3 from 5 to 8 +move 5 from 9 to 4 +move 3 from 6 to 4 +move 1 from 6 to 3 +move 3 from 1 to 6 +move 3 from 6 to 9 +move 17 from 8 to 5 +move 12 from 5 to 4 +move 21 from 4 to 3 +move 1 from 4 to 9 +move 7 from 5 to 4 +move 22 from 3 to 7 +move 3 from 1 to 8 +move 3 from 9 to 1 +move 4 from 4 to 6 +move 1 from 6 to 2 +move 3 from 4 to 1 +move 1 from 6 to 7 +move 4 from 9 to 3 +move 2 from 5 to 7 +move 1 from 9 to 6 +move 2 from 6 to 9 +move 8 from 7 to 9 +move 1 from 6 to 2 +move 1 from 9 to 3 +move 4 from 3 to 4 +move 14 from 7 to 4 +move 1 from 3 to 2 +move 3 from 7 to 8 +move 12 from 8 to 9 +move 8 from 4 to 1 +move 1 from 7 to 4 +move 2 from 5 to 1 +move 3 from 2 to 9 +move 17 from 9 to 3 +move 6 from 9 to 1 +move 1 from 9 to 2 +move 13 from 3 to 9 +move 4 from 3 to 1 +move 3 from 9 to 1 +move 22 from 1 to 9 +move 1 from 8 to 1 +move 6 from 9 to 5 +move 4 from 1 to 9 +move 3 from 1 to 9 +move 4 from 4 to 8 +move 4 from 4 to 2 +move 1 from 4 to 3 +move 3 from 8 to 9 +move 1 from 3 to 4 +move 1 from 1 to 3 +move 1 from 8 to 2 +move 1 from 5 to 8 +move 4 from 2 to 1 +move 1 from 8 to 7 +move 10 from 9 to 6 +move 1 from 7 to 9 +move 1 from 2 to 3 +move 1 from 6 to 1 +move 3 from 5 to 7 +move 1 from 8 to 7 +move 1 from 6 to 1 +move 1 from 2 to 4 +move 1 from 5 to 2 +move 19 from 9 to 2 +move 1 from 4 to 7 +move 1 from 3 to 7 +move 3 from 7 to 9 +move 4 from 1 to 2 +move 10 from 9 to 4 +move 1 from 5 to 8 +move 3 from 6 to 4 +move 1 from 3 to 4 +move 10 from 2 to 8 +move 12 from 2 to 5 +move 3 from 5 to 9 +move 5 from 6 to 5 +move 5 from 1 to 4 +move 22 from 4 to 3 +move 3 from 8 to 7 +move 1 from 7 to 2 +move 3 from 2 to 9 +move 19 from 3 to 5 +move 2 from 7 to 8 +move 7 from 5 to 6 +move 5 from 9 to 6 +move 1 from 9 to 3 +move 16 from 5 to 1 +move 2 from 3 to 1 +move 3 from 7 to 3 +move 7 from 8 to 4 +move 2 from 8 to 1 +move 5 from 5 to 9 +move 1 from 5 to 2 +move 1 from 2 to 3 +move 1 from 8 to 5 +move 4 from 5 to 7 +move 2 from 3 to 8 +move 2 from 1 to 5 +move 4 from 7 to 6 +move 6 from 4 to 7 +move 4 from 9 to 8 +move 14 from 6 to 7 +move 8 from 1 to 7 +move 7 from 1 to 3 +move 3 from 5 to 9 +move 28 from 7 to 5 +move 1 from 1 to 8 +move 4 from 8 to 3 +move 9 from 3 to 1 +move 1 from 9 to 5 +move 6 from 3 to 2 +move 10 from 1 to 6 +move 1 from 1 to 9 +move 5 from 9 to 7 +move 14 from 5 to 3 +move 1 from 4 to 1 +move 1 from 7 to 2 +move 1 from 7 to 1 +move 1 from 1 to 7 +move 3 from 8 to 5 +move 4 from 6 to 3 +move 3 from 7 to 2 +move 15 from 3 to 6 +move 16 from 5 to 7 +move 4 from 2 to 8 +move 1 from 3 to 1 +move 5 from 7 to 3 +move 12 from 6 to 4 +move 4 from 8 to 5 +move 1 from 4 to 2 +move 2 from 5 to 3 +move 8 from 6 to 3 +move 7 from 4 to 5 +move 9 from 7 to 6 +move 1 from 7 to 9 +move 1 from 1 to 9 +move 1 from 1 to 9 +move 5 from 2 to 8 +move 5 from 8 to 2 +move 11 from 5 to 9 +move 1 from 4 to 2 +move 4 from 9 to 6 +move 12 from 3 to 7 +move 3 from 4 to 9 +move 14 from 6 to 2 +move 2 from 2 to 4 +move 2 from 3 to 5 +move 10 from 7 to 2 +move 1 from 4 to 8 +move 1 from 2 to 7 +move 28 from 2 to 9 +move 4 from 7 to 5 +move 1 from 2 to 4 +move 6 from 5 to 1 +move 2 from 4 to 3 +move 1 from 8 to 1 +move 40 from 9 to 1 +move 10 from 1 to 6 +move 5 from 3 to 5 +move 1 from 9 to 8 +move 3 from 6 to 7 +move 11 from 1 to 2 +move 9 from 2 to 3 +move 3 from 5 to 1 +move 4 from 7 to 1 +move 2 from 2 to 4 +move 2 from 5 to 8 +move 19 from 1 to 7 +move 8 from 3 to 2 +move 14 from 1 to 8 +move 14 from 7 to 1 +move 4 from 6 to 5 +move 1 from 1 to 9 \ No newline at end of file diff --git a/5/puzzle.txt b/5/puzzle.txt new file mode 100644 index 0000000..ca72988 --- /dev/null +++ b/5/puzzle.txt @@ -0,0 +1,109 @@ +--- 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? + +Your puzzle answer was ZWHVFWQWW. +--- 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? + +Your puzzle answer was HZFZCCWWV. \ No newline at end of file diff --git a/5/solution.nix b/5/solution.nix new file mode 100644 index 0000000..257fd16 --- /dev/null +++ b/5/solution.nix @@ -0,0 +1,46 @@ +with import ../util.nix; + +let + # read input + input = splitString "\n\n" (readFile ./input); + + stacks = + let + # each layer of stacks as a raw input string + layers = dropLast (splitString "\n" (head input)); + # each layer of stacks as a list + parsedLayers = map (s: map (l: elemAt l 1) (chunksOf 4 (stringToCharacters s))) layers; + in + # flip from layers to stacks and remove empty slots + map (filter (s: s != " ")) (transpose parsedLayers); + + # list of moves "move 1 from 7 to 9" => [1 7 9] + moves = map + (line: map toInt (subtractLists ["move" "from" "to"] (splitString " " line))) + (splitString "\n" (last input)); + + # funcs for how many boxes can be moved + moveTopOnly = cnt: 1; # crane can only move one bpx at a time + moveAll = cnt: cnt; # crane can move any number of boxes at a time + + # performs a single move + performMove = moveCntFunc: stacks: mv: + if head mv == 0 then stacks # base case; no more boxes to move + else let + cnt = elemAt mv 0; + cnt' = moveCntFunc cnt; + src = elemAt mv 1; + dest = elemAt mv 2; + boxes = take cnt' (elemAt stacks (src -1)); + stacks' = imap1 (i: s: + if i == src then drop cnt' s + else if i == dest then boxes ++ s + else s) stacks; + in performMove moveCntFunc stacks' [(cnt - cnt') src dest]; + + # performs all moves in sequence + performMoves = moveCntFunc: foldl (performMove moveCntFunc) stacks moves; +in { + part1 = concatStrings (map head (performMoves moveTopOnly)); + part2 = concatStrings (map head (performMoves moveAll)); +} \ No newline at end of file diff --git a/runAll.sh b/runAll.sh index 04d2357..6bbecff 100644 --- a/runAll.sh +++ b/runAll.sh @@ -1,6 +1,6 @@ #! /bin/sh -for i in {1..4} +for i in {1..5} do echo "--- Day $i ---" nix eval -f $i/solution.nix diff --git a/util.nix b/util.nix index 5f4683d..0e35c09 100644 --- a/util.nix +++ b/util.nix @@ -16,7 +16,7 @@ let cutInHalf = l: [(take (length l / 2) l) (drop (length l / 2) l)]; # Splits a list into a list of lists with length cnt chunksOf = cnt: l: - if length l >= cnt then + if length l > 0 then [(take cnt l)] ++ chunksOf cnt (drop cnt l) else []; # same as intersectLists but takes an array of lists to intersect instead of just two @@ -26,6 +26,14 @@ let else intersectLists (head l) (intersectManyLists (tail l)); # converts a boolean to a int (c style) boolToInt = b: if b then 1 else 0; + # drops the last element of a list + dropLast = l: take (length l - 1) l; + # transposes a matrix + transpose = ll: + let + outerSize = length ll; + innerSize = length (elemAt ll 0); + in genList (i: genList (j: elemAt (elemAt ll j) i) outerSize) innerSize; }; in lib // lib.strings // builtins // utils \ No newline at end of file