From 743c1034b4254bbe22041657465d303d622ea675 Mon Sep 17 00:00:00 2001 From: Ruediger Ludwig Date: Mon, 5 Dec 2022 17:38:36 +0100 Subject: [PATCH] day05 minor cleanup --- advent/days/day05/solution.py | 16 +++++++++------- advent/days/day05/test_solution.py | 18 +++++------------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/advent/days/day05/solution.py b/advent/days/day05/solution.py index 62a9ff3..6bb62dd 100644 --- a/advent/days/day05/solution.py +++ b/advent/days/day05/solution.py @@ -32,14 +32,17 @@ class Move: move_parser: ClassVar[P[tuple[int, int, int]]] = P.seq(amount_parser, from_parser, to_parser) @staticmethod - def parse(line: str) -> Move: - amount, frm, to = Move.move_parser.parse(line).get() + def parse(line: str) -> Move | None: + parsed = Move.move_parser.parse(line) + if parsed.is_fail(): + return None + amount, frm, to = parsed.get() return Move(amount, frm - 1, to - 1) def do_move(self, crates: list[str], as_9001: bool) -> list[str]: """ Moves the given crates by the provided move. Will fail if there are not enough crates - in the from stack + in the stack to take crates off """ if as_9001: crates[self.to] += crates[self.frm][-self.amount:] @@ -64,12 +67,11 @@ class Crane: return Crane.crate_row_parser.parse(line).get_or(None) @staticmethod - def parse_drawing(lines: Iterator[str]) -> list[str]: + def parse_stacks(lines: Iterator[str]) -> list[str]: stacks: list[str] = [] for line in lines: crate_row = Crane.parse_crate_row(line) if crate_row is None: - next(lines) # Empty line return stacks if len(stacks) < len(crate_row): @@ -83,8 +85,8 @@ class Crane: @staticmethod def parse(lines: Iterator[str], is_9001: bool) -> Crane: - drawing = Crane.parse_drawing(lines) - moves = [Move.parse(line) for line in lines] + drawing = Crane.parse_stacks(lines) + moves = [p for p in (Move.parse(line) for line in lines) if p is not None] return Crane(drawing, moves, is_9001) @staticmethod diff --git a/advent/days/day05/test_solution.py b/advent/days/day05/test_solution.py index 8bd9dd0..7e4db64 100644 --- a/advent/days/day05/test_solution.py +++ b/advent/days/day05/test_solution.py @@ -34,7 +34,7 @@ def test_parse_line2(): def test_drawing(): data = utils.read_data(day_num, 'test01.txt') expected = ["ZN", "MCD", "P"] - result = Crane.parse_drawing(data) + result = Crane.parse_stacks(data) assert result == expected @@ -54,25 +54,17 @@ def test_parse_all(): assert result == expected -def test_step(): - data = utils.read_data(day_num, 'test01.txt') - state = Crane.parse(data, True) - expected = ["ZND", "MC", "P"] - result = state.moves[0].do_move(state.stacks, False) - assert result == expected - - def test_all_moves(): data = utils.read_data(day_num, 'test01.txt') - state = Crane.parse(data, False) + crane = Crane.parse(data, False) expected = ["C", "M", "PDNZ"] - result = state.perform_all_moves() + result = crane.perform_all_moves() assert result == expected def test_all_moves9001(): data = utils.read_data(day_num, 'test01.txt') - state = Crane.parse(data, True) + crane = Crane.parse(data, True) expected = ["M", "C", "PZND"] - result = state.perform_all_moves() + result = crane.perform_all_moves() assert result == expected