From a5e7113ff4ad21d394ac6c59d5c2eb2579d4fcb2 Mon Sep 17 00:00:00 2001 From: Daniel Mevec Date: Mon, 5 Dec 2022 09:15:00 +0100 Subject: [PATCH] Day 5 - 2022 --- 2022/5/5.md | 127 ++++++++++++ 2022/5/5.py | 84 ++++++++ 2022/5/crane.txt | 511 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 722 insertions(+) create mode 100644 2022/5/5.md create mode 100644 2022/5/5.py create mode 100644 2022/5/crane.txt diff --git a/2022/5/5.md b/2022/5/5.md new file mode 100644 index 0000000..c2382c2 --- /dev/null +++ b/2022/5/5.md @@ -0,0 +1,127 @@ +# 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 2 + +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?* \ No newline at end of file diff --git a/2022/5/5.py b/2022/5/5.py new file mode 100644 index 0000000..17bb357 --- /dev/null +++ b/2022/5/5.py @@ -0,0 +1,84 @@ +""" +rearrange a list of crates one by one according to given instructions +""" + + +import re +from string import ascii_letters + +instruction_pattern = re.compile(r'(\d+)') + +class CrateMover9000: + def __init__(self, plan:str) -> None: + self.numstacks = len(instruction_pattern.findall(plan.pop())) + self.stacks = [[] for _ in range(self.numstacks)] + + for i in range(len(plan)): + layer = f"{plan.pop():<{self.numstacks*4}}" + crates = layer[1::4] + for j, crate in enumerate(crates): + if crate in ascii_letters: + self.stacks[j].append(crate) + + def move(self, instruction:str) -> None: + num, source, target = [int(s) for s in instruction_pattern.findall(instruction)] + for _ in range(num): + self.stacks[target-1].append(self.stacks[source-1].pop()) + + def shuffle(self, insttructions:str) -> None: + for line in instructions: + self.move(line) + + + def show(self) -> None: + for i, stack in enumerate(self.stacks): + print(i+1,*stack) + print("") + + def top_crates(self) -> str: + s = '' + for stack in self.stacks: + s += stack[-1] + return s + +text = """ + [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 +""" + +with open('2022/5/crane.txt') as filein: + text = filein.read() + +plan, instructions = [t.splitlines(keepends=False) for t in text.split('\n\n')] + +crane = CrateMover9000(plan) +crane.show() +crane.shuffle(instructions) +crane.show() +print("top message:", crane.top_crates()) + +""" +now rearrange a list of crates in batches according to given instructions +""" + +class CrateMover9001(CrateMover9000): + def __init__(self, plan: str) -> None: + super().__init__(plan) + + def move(self, instruction: str) -> None: + num, source, target = [int(s) for s in instruction_pattern.findall(instruction)] + self.stacks[target-1] += self.stacks[source-1][-num:] + self.stacks[source-1] = self.stacks[source-1][:-num] + +plan, instructions = [t.splitlines(keepends=False) for t in text.split('\n\n')] +crane = CrateMover9001(plan) +crane.shuffle(instructions) +crane.show() +print("top message:", crane.top_crates()) \ No newline at end of file diff --git a/2022/5/crane.txt b/2022/5/crane.txt new file mode 100644 index 0000000..2d46420 --- /dev/null +++ b/2022/5/crane.txt @@ -0,0 +1,511 @@ +[T] [V] [W] +[V] [C] [P] [D] [B] +[J] [P] [R] [N] [B] [Z] +[W] [Q] [D] [M] [T] [L] [T] +[N] [J] [H] [B] [P] [T] [P] [L] +[R] [D] [F] [P] [R] [P] [R] [S] [G] +[M] [W] [J] [R] [V] [B] [J] [C] [S] +[S] [B] [B] [F] [H] [C] [B] [N] [L] + 1 2 3 4 5 6 7 8 9 + +move 7 from 3 to 9 +move 6 from 2 to 1 +move 2 from 4 to 8 +move 10 from 8 to 4 +move 1 from 2 to 4 +move 15 from 4 to 1 +move 28 from 1 to 3 +move 1 from 2 to 5 +move 7 from 5 to 9 +move 5 from 9 to 5 +move 21 from 3 to 1 +move 1 from 6 to 4 +move 4 from 9 to 2 +move 7 from 9 to 2 +move 4 from 2 to 6 +move 1 from 9 to 1 +move 2 from 4 to 9 +move 2 from 7 to 4 +move 4 from 3 to 5 +move 2 from 7 to 9 +move 5 from 2 to 7 +move 2 from 9 to 1 +move 1 from 9 to 2 +move 3 from 3 to 7 +move 3 from 5 to 2 +move 2 from 6 to 1 +move 1 from 9 to 4 +move 25 from 1 to 9 +move 2 from 5 to 3 +move 1 from 1 to 7 +move 9 from 7 to 1 +move 1 from 2 to 8 +move 16 from 9 to 7 +move 5 from 9 to 5 +move 7 from 1 to 8 +move 1 from 9 to 7 +move 18 from 7 to 4 +move 1 from 3 to 4 +move 1 from 1 to 8 +move 2 from 8 to 1 +move 1 from 3 to 9 +move 17 from 4 to 5 +move 5 from 8 to 6 +move 2 from 8 to 1 +move 6 from 6 to 7 +move 2 from 9 to 4 +move 5 from 1 to 3 +move 3 from 3 to 7 +move 1 from 1 to 2 +move 2 from 3 to 8 +move 2 from 4 to 1 +move 2 from 8 to 7 +move 2 from 9 to 7 +move 1 from 2 to 4 +move 1 from 2 to 4 +move 2 from 1 to 7 +move 11 from 7 to 1 +move 4 from 4 to 1 +move 3 from 1 to 3 +move 7 from 1 to 8 +move 4 from 2 to 8 +move 2 from 1 to 4 +move 2 from 4 to 3 +move 2 from 1 to 3 +move 3 from 7 to 4 +move 4 from 8 to 6 +move 1 from 7 to 5 +move 2 from 4 to 1 +move 4 from 6 to 2 +move 3 from 1 to 8 +move 6 from 8 to 6 +move 1 from 2 to 1 +move 23 from 5 to 8 +move 21 from 8 to 3 +move 2 from 6 to 9 +move 1 from 8 to 9 +move 2 from 3 to 7 +move 26 from 3 to 8 +move 2 from 9 to 2 +move 8 from 6 to 3 +move 3 from 5 to 4 +move 1 from 7 to 4 +move 1 from 7 to 9 +move 1 from 5 to 4 +move 5 from 2 to 9 +move 7 from 9 to 8 +move 38 from 8 to 7 +move 1 from 1 to 7 +move 1 from 4 to 9 +move 3 from 4 to 1 +move 4 from 3 to 2 +move 1 from 1 to 7 +move 34 from 7 to 6 +move 3 from 4 to 7 +move 1 from 4 to 2 +move 2 from 1 to 3 +move 1 from 2 to 9 +move 5 from 3 to 6 +move 1 from 4 to 6 +move 4 from 2 to 6 +move 1 from 9 to 5 +move 4 from 7 to 3 +move 1 from 5 to 9 +move 1 from 9 to 1 +move 1 from 3 to 6 +move 1 from 9 to 3 +move 5 from 7 to 9 +move 1 from 1 to 9 +move 3 from 9 to 1 +move 1 from 3 to 4 +move 38 from 6 to 1 +move 2 from 9 to 4 +move 3 from 3 to 6 +move 1 from 9 to 5 +move 8 from 6 to 8 +move 1 from 3 to 6 +move 1 from 6 to 8 +move 2 from 4 to 3 +move 4 from 8 to 5 +move 1 from 6 to 2 +move 1 from 2 to 7 +move 1 from 6 to 3 +move 3 from 8 to 7 +move 4 from 7 to 6 +move 1 from 4 to 1 +move 5 from 1 to 4 +move 4 from 6 to 5 +move 3 from 5 to 8 +move 3 from 5 to 3 +move 1 from 8 to 2 +move 6 from 3 to 8 +move 1 from 5 to 7 +move 1 from 2 to 1 +move 1 from 7 to 6 +move 3 from 4 to 9 +move 6 from 8 to 6 +move 3 from 8 to 2 +move 3 from 6 to 2 +move 1 from 4 to 5 +move 1 from 6 to 9 +move 2 from 6 to 1 +move 1 from 4 to 1 +move 2 from 5 to 2 +move 1 from 8 to 5 +move 1 from 9 to 8 +move 22 from 1 to 5 +move 3 from 2 to 3 +move 1 from 8 to 4 +move 2 from 3 to 6 +move 1 from 6 to 4 +move 1 from 3 to 8 +move 1 from 2 to 8 +move 10 from 5 to 3 +move 1 from 6 to 8 +move 2 from 8 to 4 +move 1 from 6 to 3 +move 2 from 2 to 3 +move 1 from 8 to 5 +move 13 from 3 to 4 +move 2 from 1 to 7 +move 11 from 1 to 2 +move 3 from 4 to 5 +move 6 from 1 to 9 +move 8 from 2 to 6 +move 4 from 2 to 1 +move 2 from 6 to 5 +move 4 from 1 to 8 +move 2 from 8 to 6 +move 1 from 7 to 1 +move 1 from 7 to 2 +move 8 from 6 to 5 +move 1 from 8 to 4 +move 1 from 1 to 6 +move 10 from 5 to 1 +move 3 from 9 to 4 +move 6 from 1 to 3 +move 9 from 4 to 5 +move 1 from 2 to 1 +move 1 from 4 to 9 +move 1 from 6 to 8 +move 1 from 2 to 1 +move 1 from 4 to 8 +move 2 from 8 to 9 +move 1 from 8 to 6 +move 1 from 3 to 2 +move 1 from 1 to 4 +move 1 from 2 to 5 +move 1 from 1 to 8 +move 1 from 6 to 8 +move 8 from 5 to 3 +move 1 from 8 to 7 +move 1 from 7 to 8 +move 6 from 9 to 6 +move 2 from 9 to 2 +move 1 from 9 to 7 +move 1 from 8 to 5 +move 1 from 2 to 7 +move 1 from 2 to 9 +move 16 from 5 to 2 +move 4 from 5 to 1 +move 12 from 3 to 6 +move 1 from 5 to 4 +move 8 from 6 to 9 +move 1 from 8 to 5 +move 2 from 9 to 6 +move 2 from 2 to 5 +move 3 from 1 to 4 +move 3 from 6 to 7 +move 7 from 9 to 6 +move 4 from 7 to 1 +move 1 from 5 to 2 +move 1 from 3 to 2 +move 4 from 1 to 9 +move 4 from 9 to 5 +move 12 from 6 to 7 +move 4 from 2 to 6 +move 4 from 2 to 9 +move 7 from 6 to 3 +move 3 from 9 to 8 +move 5 from 2 to 4 +move 4 from 5 to 7 +move 1 from 9 to 4 +move 13 from 7 to 8 +move 3 from 7 to 8 +move 2 from 3 to 2 +move 5 from 1 to 5 +move 11 from 4 to 9 +move 7 from 9 to 1 +move 4 from 4 to 3 +move 1 from 6 to 8 +move 8 from 8 to 4 +move 1 from 1 to 7 +move 3 from 4 to 6 +move 4 from 2 to 8 +move 3 from 5 to 4 +move 1 from 4 to 1 +move 4 from 9 to 8 +move 3 from 5 to 2 +move 2 from 2 to 3 +move 1 from 6 to 7 +move 7 from 1 to 9 +move 3 from 7 to 4 +move 1 from 4 to 2 +move 1 from 6 to 8 +move 1 from 5 to 7 +move 1 from 6 to 7 +move 9 from 4 to 6 +move 7 from 9 to 5 +move 1 from 4 to 3 +move 1 from 5 to 6 +move 4 from 3 to 7 +move 3 from 3 to 1 +move 2 from 2 to 4 +move 3 from 1 to 6 +move 4 from 5 to 1 +move 2 from 5 to 3 +move 3 from 6 to 8 +move 6 from 7 to 3 +move 10 from 3 to 7 +move 10 from 6 to 4 +move 3 from 1 to 9 +move 4 from 7 to 2 +move 2 from 3 to 5 +move 1 from 3 to 5 +move 1 from 1 to 2 +move 3 from 9 to 1 +move 2 from 1 to 9 +move 4 from 2 to 5 +move 10 from 4 to 9 +move 2 from 8 to 7 +move 1 from 2 to 9 +move 1 from 9 to 4 +move 1 from 1 to 9 +move 3 from 7 to 8 +move 5 from 9 to 8 +move 6 from 5 to 4 +move 5 from 9 to 6 +move 5 from 8 to 5 +move 4 from 5 to 2 +move 3 from 7 to 8 +move 3 from 9 to 1 +move 2 from 5 to 8 +move 1 from 4 to 6 +move 3 from 6 to 3 +move 8 from 4 to 3 +move 2 from 6 to 7 +move 24 from 8 to 9 +move 1 from 6 to 9 +move 8 from 9 to 3 +move 1 from 1 to 9 +move 2 from 8 to 3 +move 3 from 9 to 4 +move 18 from 3 to 5 +move 1 from 2 to 6 +move 1 from 6 to 1 +move 13 from 9 to 8 +move 3 from 4 to 1 +move 1 from 4 to 2 +move 1 from 5 to 3 +move 1 from 9 to 2 +move 6 from 2 to 9 +move 3 from 3 to 1 +move 3 from 7 to 6 +move 2 from 1 to 5 +move 3 from 6 to 7 +move 17 from 8 to 1 +move 8 from 5 to 7 +move 11 from 7 to 5 +move 1 from 4 to 5 +move 24 from 1 to 3 +move 7 from 5 to 9 +move 11 from 5 to 9 +move 1 from 5 to 4 +move 1 from 5 to 2 +move 1 from 4 to 7 +move 16 from 3 to 1 +move 3 from 5 to 8 +move 1 from 9 to 1 +move 12 from 9 to 2 +move 5 from 3 to 1 +move 2 from 8 to 5 +move 2 from 3 to 4 +move 1 from 8 to 6 +move 2 from 5 to 3 +move 1 from 6 to 1 +move 2 from 4 to 1 +move 8 from 2 to 1 +move 24 from 1 to 6 +move 1 from 9 to 6 +move 4 from 2 to 6 +move 4 from 3 to 2 +move 4 from 9 to 4 +move 1 from 7 to 5 +move 6 from 9 to 1 +move 17 from 6 to 4 +move 1 from 1 to 9 +move 2 from 9 to 7 +move 9 from 6 to 7 +move 12 from 7 to 2 +move 11 from 1 to 2 +move 12 from 4 to 2 +move 1 from 1 to 4 +move 1 from 5 to 7 +move 2 from 6 to 3 +move 2 from 4 to 3 +move 1 from 1 to 6 +move 5 from 4 to 2 +move 1 from 7 to 5 +move 2 from 6 to 9 +move 6 from 2 to 4 +move 1 from 5 to 8 +move 3 from 4 to 1 +move 1 from 8 to 9 +move 5 from 4 to 1 +move 1 from 1 to 2 +move 1 from 3 to 8 +move 7 from 1 to 6 +move 1 from 8 to 6 +move 1 from 3 to 5 +move 1 from 1 to 8 +move 1 from 4 to 5 +move 2 from 5 to 9 +move 1 from 8 to 2 +move 2 from 6 to 8 +move 1 from 8 to 6 +move 1 from 6 to 8 +move 1 from 8 to 5 +move 4 from 9 to 5 +move 1 from 9 to 6 +move 1 from 6 to 8 +move 37 from 2 to 5 +move 1 from 2 to 8 +move 37 from 5 to 8 +move 21 from 8 to 4 +move 1 from 3 to 7 +move 12 from 4 to 1 +move 1 from 7 to 8 +move 4 from 6 to 3 +move 1 from 4 to 2 +move 2 from 2 to 6 +move 2 from 3 to 2 +move 2 from 2 to 7 +move 2 from 7 to 1 +move 3 from 5 to 3 +move 2 from 2 to 8 +move 15 from 8 to 7 +move 1 from 7 to 2 +move 2 from 5 to 8 +move 5 from 4 to 3 +move 3 from 6 to 2 +move 8 from 1 to 9 +move 8 from 9 to 4 +move 7 from 8 to 9 +move 2 from 8 to 5 +move 4 from 1 to 9 +move 10 from 3 to 2 +move 1 from 6 to 7 +move 6 from 7 to 8 +move 10 from 4 to 7 +move 1 from 3 to 7 +move 3 from 9 to 1 +move 5 from 8 to 1 +move 5 from 2 to 7 +move 1 from 4 to 3 +move 1 from 5 to 6 +move 10 from 1 to 7 +move 34 from 7 to 4 +move 1 from 6 to 9 +move 1 from 7 to 3 +move 8 from 4 to 2 +move 1 from 9 to 7 +move 1 from 7 to 9 +move 22 from 4 to 5 +move 1 from 3 to 8 +move 6 from 5 to 6 +move 1 from 8 to 4 +move 9 from 9 to 4 +move 1 from 3 to 2 +move 4 from 2 to 1 +move 11 from 2 to 6 +move 14 from 4 to 7 +move 1 from 2 to 1 +move 12 from 5 to 6 +move 7 from 7 to 9 +move 2 from 5 to 4 +move 1 from 8 to 5 +move 6 from 6 to 8 +move 3 from 7 to 8 +move 1 from 2 to 6 +move 2 from 4 to 3 +move 1 from 3 to 8 +move 1 from 2 to 5 +move 1 from 3 to 4 +move 5 from 8 to 9 +move 5 from 1 to 4 +move 3 from 8 to 9 +move 5 from 4 to 7 +move 18 from 6 to 3 +move 2 from 8 to 9 +move 3 from 6 to 3 +move 5 from 7 to 1 +move 1 from 6 to 7 +move 3 from 9 to 7 +move 6 from 7 to 8 +move 8 from 9 to 8 +move 1 from 7 to 6 +move 12 from 3 to 1 +move 2 from 9 to 7 +move 1 from 8 to 6 +move 9 from 8 to 9 +move 2 from 8 to 7 +move 1 from 7 to 8 +move 2 from 1 to 6 +move 7 from 1 to 9 +move 16 from 9 to 5 +move 4 from 3 to 9 +move 8 from 1 to 3 +move 5 from 5 to 1 +move 1 from 4 to 3 +move 4 from 7 to 6 +move 3 from 8 to 2 +move 8 from 3 to 2 +move 11 from 2 to 7 +move 3 from 6 to 1 +move 4 from 3 to 6 +move 12 from 5 to 7 +move 2 from 3 to 6 +move 7 from 1 to 7 +move 2 from 5 to 1 +move 1 from 9 to 2 +move 1 from 5 to 7 +move 31 from 7 to 4 +move 6 from 9 to 1 +move 6 from 4 to 7 +move 16 from 4 to 5 +move 1 from 9 to 4 +move 1 from 7 to 6 +move 4 from 4 to 1 +move 11 from 5 to 9 +move 2 from 5 to 6 +move 1 from 2 to 9 +move 4 from 5 to 1 +move 8 from 9 to 2 +move 12 from 6 to 5 +move 11 from 5 to 1 +move 18 from 1 to 2 +move 3 from 4 to 3 +move 2 from 6 to 7 +move 2 from 9 to 1 +move 1 from 5 to 9 +move 3 from 9 to 5 +move 13 from 2 to 8 +move 10 from 8 to 2 +move 3 from 8 to 6 +move 3 from 3 to 9 +move 7 from 1 to 5 +move 6 from 5 to 6 +move 3 from 1 to 2 +move 5 from 7 to 8 +move 13 from 2 to 8 +move 9 from 6 to 3