mirror of
https://github.com/rjNemo/advent_of_code_2022
synced 2026-06-06 02:26:41 +00:00
day5 part 2
This commit is contained in:
parent
36f1365782
commit
9d19a95073
2 changed files with 105 additions and 13 deletions
|
|
@ -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?**
|
||||
**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?**
|
||||
48
day5/main.py
48
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"
|
||||
|
|
|
|||
Loading…
Reference in a new issue