day05 minor cleanup

This commit is contained in:
Ruediger Ludwig 2022-12-05 17:38:36 +01:00
parent 4814a87518
commit 743c1034b4
2 changed files with 14 additions and 20 deletions

View file

@ -32,14 +32,17 @@ class Move:
move_parser: ClassVar[P[tuple[int, int, int]]] = P.seq(amount_parser, from_parser, to_parser) move_parser: ClassVar[P[tuple[int, int, int]]] = P.seq(amount_parser, from_parser, to_parser)
@staticmethod @staticmethod
def parse(line: str) -> Move: def parse(line: str) -> Move | None:
amount, frm, to = Move.move_parser.parse(line).get() parsed = Move.move_parser.parse(line)
if parsed.is_fail():
return None
amount, frm, to = parsed.get()
return Move(amount, frm - 1, to - 1) return Move(amount, frm - 1, to - 1)
def do_move(self, crates: list[str], as_9001: bool) -> list[str]: 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 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: if as_9001:
crates[self.to] += crates[self.frm][-self.amount:] crates[self.to] += crates[self.frm][-self.amount:]
@ -64,12 +67,11 @@ class Crane:
return Crane.crate_row_parser.parse(line).get_or(None) return Crane.crate_row_parser.parse(line).get_or(None)
@staticmethod @staticmethod
def parse_drawing(lines: Iterator[str]) -> list[str]: def parse_stacks(lines: Iterator[str]) -> list[str]:
stacks: list[str] = [] stacks: list[str] = []
for line in lines: for line in lines:
crate_row = Crane.parse_crate_row(line) crate_row = Crane.parse_crate_row(line)
if crate_row is None: if crate_row is None:
next(lines) # Empty line
return stacks return stacks
if len(stacks) < len(crate_row): if len(stacks) < len(crate_row):
@ -83,8 +85,8 @@ class Crane:
@staticmethod @staticmethod
def parse(lines: Iterator[str], is_9001: bool) -> Crane: def parse(lines: Iterator[str], is_9001: bool) -> Crane:
drawing = Crane.parse_drawing(lines) drawing = Crane.parse_stacks(lines)
moves = [Move.parse(line) for line in lines] moves = [p for p in (Move.parse(line) for line in lines) if p is not None]
return Crane(drawing, moves, is_9001) return Crane(drawing, moves, is_9001)
@staticmethod @staticmethod

View file

@ -34,7 +34,7 @@ def test_parse_line2():
def test_drawing(): def test_drawing():
data = utils.read_data(day_num, 'test01.txt') data = utils.read_data(day_num, 'test01.txt')
expected = ["ZN", "MCD", "P"] expected = ["ZN", "MCD", "P"]
result = Crane.parse_drawing(data) result = Crane.parse_stacks(data)
assert result == expected assert result == expected
@ -54,25 +54,17 @@ def test_parse_all():
assert result == expected 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(): def test_all_moves():
data = utils.read_data(day_num, 'test01.txt') data = utils.read_data(day_num, 'test01.txt')
state = Crane.parse(data, False) crane = Crane.parse(data, False)
expected = ["C", "M", "PDNZ"] expected = ["C", "M", "PDNZ"]
result = state.perform_all_moves() result = crane.perform_all_moves()
assert result == expected assert result == expected
def test_all_moves9001(): def test_all_moves9001():
data = utils.read_data(day_num, 'test01.txt') data = utils.read_data(day_num, 'test01.txt')
state = Crane.parse(data, True) crane = Crane.parse(data, True)
expected = ["M", "C", "PZND"] expected = ["M", "C", "PZND"]
result = state.perform_all_moves() result = crane.perform_all_moves()
assert result == expected assert result == expected