From b46117b78848322989fbd9685d6a3cc260a10d3b Mon Sep 17 00:00:00 2001 From: aaron Date: Mon, 5 Dec 2022 23:34:34 +0100 Subject: [PATCH] solve part b --- 5/supplystacks.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/5/supplystacks.py b/5/supplystacks.py index 3218843..eb43c89 100644 --- a/5/supplystacks.py +++ b/5/supplystacks.py @@ -3,9 +3,10 @@ from aocd.models import Puzzle from aocd import submit -def rearrange_stacks(stacks: dict, moves: list) -> str: +def crate_mover_9000(stacks: dict, moves: list) -> str: """ execute moves on the stacks and return the top of each stack + the crate mover 9000 only moves single crates. """ # take a deep copy so embedded list do not get messed up stacks = deepcopy(stacks) @@ -17,6 +18,21 @@ def rearrange_stacks(stacks: dict, moves: list) -> str: return "".join([stack[-1] for stack in stacks.values()]) +def crate_mover_9001(stacks: dict, moves: list) -> str: + """ + execute moves on the stacks and return the top of each stack + the crate mover 9001 moves stacks of crates. + """ + # take a deep copy so embedded list do not get messed up + stacks = deepcopy(stacks) + # for each move, move amount of crates from index to dest + for amount, index, dest in moves: + stacks[dest].extend(stacks[index][-amount:]) + stacks[index] = stacks[index][:-amount] + # take the last element fom each stack and append to output + return "".join([stack[-1] for stack in stacks.values()]) + + def parse_input(data: list) -> list: """ turn input data into usable data types: @@ -50,11 +66,11 @@ if __name__ == "__main__": stacks, moves = parse_input(puzzle.input_data) # part a: - answer_a = rearrange_stacks(stacks, moves) + answer_a = crate_mover_9000(stacks, moves) print(f"{answer_a}") submit(answer_a, part="a", day=5, year=2022) # part b: - answer_b = 0 + answer_b = crate_mover_9001(stacks, moves) print(f"{answer_b}") - # submit(answer_b, part="b", day=5, year=2022) + submit(answer_b, part="b", day=5, year=2022)