From 7d0d3e504e9abba9c40badf1670958f7498d6984 Mon Sep 17 00:00:00 2001 From: Ruediger Ludwig Date: Sat, 10 Dec 2022 08:16:14 +0100 Subject: [PATCH] Remove unneeded class (thanks to meowmeowwarrior) --- advent/days/day10/solution.py | 89 +++++++++++++++--------------- advent/days/day10/test_solution.py | 10 ++-- 2 files changed, 50 insertions(+), 49 deletions(-) diff --git a/advent/days/day10/solution.py b/advent/days/day10/solution.py index 70884a7..e29a1d2 100644 --- a/advent/days/day10/solution.py +++ b/advent/days/day10/solution.py @@ -6,57 +6,58 @@ day_num = 10 def part1(lines: Iterator[str]) -> int: - return sum(Signal.grab_values(lines)) + return sum(grab_values(lines)) def part2(lines: Iterator[str]) -> list[str]: - return Signal.draw(lines, 40, 6) + return draw(lines, 40, 6) -class Signal: - @staticmethod - def parse(line: str) -> None | int: - """ - Parses the a line into the two possible instructions. - May raise if the instructions was invalid - """ - match line.split(): - case ['noop']: - return None - case ['addx', value]: - return int(value) - case _: - raise Exception(f"Unknown line: {line}") +def parse(line: str) -> None | int: + """ + Parses the a line into the two possible instructions. + May raise if the instructions was invalid + """ + match line.split(): + case ['noop']: + return None + case ['addx', value]: + return int(value) + case _: + raise Exception(f"Unknown line: {line}") - @staticmethod - def cycles(lines: Iterator[str]) -> Iterator[int]: - """ - Cycles throw the instructions and yields a new value on each cycle - """ - register = 1 - for line in lines: - yield register - command = Signal.parse(line) - if command is not None: + +def cycles(lines: Iterator[str]) -> Iterator[int]: + """ + Cycles through the instructions and yields a new value for each cycle + """ + register = 1 + for line in lines: + yield register + match parse(line): + case None: + pass + case value: yield register - register += command + register += value + yield register - @staticmethod - def grab_values(lines: Iterator[str]) -> Iterator[int]: - for cycle, value in enumerate(Signal.cycles(lines), start=1): - if cycle in [20, 60, 100, 140, 180, 220]: - yield cycle * value - @staticmethod - def draw(lines: Iterator[str], width: int, height: int) -> list[str]: - picture = "" - for cycle, sprite in enumerate(Signal.cycles(lines)): - crt_pos = cycle % width - if sprite - 1 <= crt_pos and crt_pos <= sprite + 1: - picture += '#' - else: - picture += ' ' +def grab_values(lines: Iterator[str]) -> Iterator[int]: + for cycle, value in enumerate(cycles(lines), start=1): + if cycle in [20, 60, 100, 140, 180, 220]: + yield cycle * value - if crt_pos == width - 1: - picture += '\n' - return picture.split('\n')[:height] + +def draw(lines: Iterator[str], width: int, height: int) -> list[str]: + picture = "" + for cycle, sprite in enumerate(cycles(lines)): + crt_pos = cycle % width + if sprite - 1 <= crt_pos and crt_pos <= sprite + 1: + picture += '#' + else: + picture += ' ' + + if crt_pos == width - 1: + picture += '\n' + return picture.split('\n')[:height] diff --git a/advent/days/day10/test_solution.py b/advent/days/day10/test_solution.py index 82142a1..35da1fd 100644 --- a/advent/days/day10/test_solution.py +++ b/advent/days/day10/test_solution.py @@ -1,6 +1,6 @@ from advent.common import utils -from .solution import Signal, day_num, part1, part2 +from .solution import cycles, day_num, draw, grab_values, part1, part2 def test_part1(): @@ -19,20 +19,20 @@ def test_part2(): def test_small(): lines = utils.read_data(day_num, 'test02.txt') - expected = [1, 1, 1, 4, 4] - result = list(Signal.cycles(lines)) + expected = [1, 1, 1, 4, 4, -1] + result = list(cycles(lines)) assert result == expected def test_grab_values(): lines = utils.read_data(day_num, 'test01.txt') expected = [420, 1140, 1800, 2940, 2880, 3960] - result = list(Signal.grab_values(lines)) + result = list(grab_values(lines)) assert result == expected def test_draw(): lines = utils.read_data(day_num, 'test01.txt') expected = list(utils.read_data(day_num, 'expected.txt')) - result = Signal.draw(lines, 40, 6) + result = draw(lines, 40, 6) assert result == expected