From 9d19a950738eb99dfe1b95686f940d617d983e08 Mon Sep 17 00:00:00 2001 From: Ruidy Date: Mon, 5 Dec 2022 14:45:41 +0100 Subject: [PATCH] day5 part 2 --- day5/README.md | 70 ++++++++++++++++++++++++++++++++++++++++++++++++-- day5/main.py | 48 ++++++++++++++++++++++++++-------- 2 files changed, 105 insertions(+), 13 deletions(-) diff --git a/day5/README.md b/day5/README.md index 4089c5e..f22bd5d 100644 --- a/day5/README.md +++ b/day5/README.md @@ -74,6 +74,72 @@ Finally, one crate is moved from stack 1 to stack 2: ``` 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`. +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?** \ No newline at end of file +**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?** \ No newline at end of file diff --git a/day5/main.py b/day5/main.py index e9f10ab..df7dd25 100644 --- a/day5/main.py +++ b/day5/main.py @@ -22,6 +22,38 @@ class Move: to: int +def top_crates_1(data: list) -> str: + stacks, instructions = process_data(data) + stacks = find_top_crates_1(stacks, instructions) + return output_top_crates(stacks) + + +def find_top_crates_1(stacks: list[list[str]], instructions: list[Move]) -> list[list[str]]: + for step in instructions: + for _ in range(step.quantity): + stacks[step.to].append(stacks[step.of].pop()) + + return stacks + + +def find_top_crates_2(stacks: list[list[str]], instructions: list[Move]) -> list[list[str]]: + for step in instructions: + tmp = [] + print(tmp) + for _ in range(step.quantity): + tmp.insert(0, stacks[step.of].pop()) + print(tmp) + stacks[step.to].extend(tmp) + + return stacks + + +def top_crates_2(data: list[str]) -> str: + stacks, instructions = process_data(data) + stacks = find_top_crates_2(stacks, instructions) + return output_top_crates(stacks) + + def process_data(data: list[str]) -> tuple[list[list[str]], list[Move]]: rows = [] steps = [] @@ -66,21 +98,15 @@ def _build_instructions(instructions: list[str]) -> list[Move]: return moves -def find_top_crates(stacks: list[list[str]], instructions: list[Move]) -> str: - for step in instructions: - for _ in range(step.quantity): - stacks[step.to].append(stacks[step.of].pop()) - +def output_top_crates(stacks) -> str: return "".join(stack[-1] for stack in stacks) -def top_crates(data: list) -> str: - stacks, instructions = process_data(data) - return find_top_crates(stacks, instructions) - - if __name__ == "__main__": dataset = read_data() - res = top_crates(dataset) + res = top_crates_1(dataset) assert res == "CMZ", f"{res} is not the right value" + + res = top_crates_2(dataset) + assert res == "MCD", f"{res} is not the right value"