From 5ad92f15858b9cd7628bc154aad8ec3fd8528e7a Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 5 Dec 2022 08:26:15 +0100 Subject: [PATCH] 2022: d05: ex2: add solution --- 2022/d05/ex2/ex2.py | 56 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100755 2022/d05/ex2/ex2.py diff --git a/2022/d05/ex2/ex2.py b/2022/d05/ex2/ex2.py new file mode 100755 index 0000000..d5db4ac --- /dev/null +++ b/2022/d05/ex2/ex2.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python + +import dataclasses +import sys + +Stack = list[str] + + +@dataclasses.dataclass +class Instruction: + n: int + start: int + end: int + + @classmethod + def from_input(cls, input: str) -> "Instruction": + words = input.split() + return Instruction(int(words[1]), int(words[3]) - 1, int(words[5]) - 1) + + +def parse(input: list[str]) -> tuple[list[Stack], list[Instruction]]: + def parse_stacks(input: list[str]) -> list[Stack]: + stacks: list[Stack] = [[] for _ in range(1, len(input[-1]), 4)] + for line in reversed(input[:-1]): + for stack, i in enumerate(range(1, len(line), 4)): + c = line[i] + if c == " ": + continue + stacks[stack].append(c) + return stacks + + def parse_instructions(input: list[str]) -> list[Instruction]: + return list(map(Instruction.from_input, input)) + + empty_line = input.index("") + + return parse_stacks(input[:empty_line]), parse_instructions(input[empty_line + 1 :]) + + +def solve(input: list[str]) -> str: + stacks, instructions = parse(input) + for instr in instructions: + start, end = stacks[instr.start], stacks[instr.end] + items = [start.pop() for _ in range(instr.n)] + items.reverse() + end.extend(items) + return "".join(stack[-1] for stack in stacks if stack) + + +def main() -> None: + input = sys.stdin.read().splitlines() + print(solve(input)) + + +if __name__ == "__main__": + main()