solve part b

This commit is contained in:
aaron
2022-12-05 23:34:34 +01:00
parent 89a16489cb
commit b46117b788

View File

@@ -3,9 +3,10 @@ from aocd.models import Puzzle
from aocd import submit 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 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 # take a deep copy so embedded list do not get messed up
stacks = deepcopy(stacks) stacks = deepcopy(stacks)
@@ -17,6 +18,21 @@ def rearrange_stacks(stacks: dict, moves: list) -> str:
return "".join([stack[-1] for stack in stacks.values()]) 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: def parse_input(data: list) -> list:
""" """
turn input data into usable data types: turn input data into usable data types:
@@ -50,11 +66,11 @@ if __name__ == "__main__":
stacks, moves = parse_input(puzzle.input_data) stacks, moves = parse_input(puzzle.input_data)
# part a: # part a:
answer_a = rearrange_stacks(stacks, moves) answer_a = crate_mover_9000(stacks, moves)
print(f"{answer_a}") print(f"{answer_a}")
submit(answer_a, part="a", day=5, year=2022) submit(answer_a, part="a", day=5, year=2022)
# part b: # part b:
answer_b = 0 answer_b = crate_mover_9001(stacks, moves)
print(f"{answer_b}") print(f"{answer_b}")
# submit(answer_b, part="b", day=5, year=2022) submit(answer_b, part="b", day=5, year=2022)