From 5e72d702c3fd3b52549e4f6184926b37e1b87a4e Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Sun, 11 Dec 2022 20:36:08 +0100 Subject: Day 10 --- day10/__init__.py | 69 ++++++++++++++++++++++--------------------------------- 1 file changed, 27 insertions(+), 42 deletions(-) diff --git a/day10/__init__.py b/day10/__init__.py index e6d385a..740ff08 100644 --- a/day10/__init__.py +++ b/day10/__init__.py @@ -11,55 +11,41 @@ class Assignment(BaseAssignment, ABC): super().__init__(path) self.x_history = {1: 1} - def addx(self, value: str) -> Iterator[int]: - yield 0 - yield int(value) - - def noop(self) -> Iterator: - yield 0 - - def x_after(self, cycles: int, instructions: List[str]): - try: - return self.x_history[cycles] - except KeyError: - pass - - cycle = 1 + def parse_instructions(self, instructions: Iterator[str]) -> Iterator[int]: x = 1 - while cycle <= cycles and len(instructions) > 0: - instruction, *instructions = instructions + for instruction in instructions: instruction, *args = instruction.split(" ") - for val in getattr(self, instruction)(*args): - x += val - cycle += 1 - self.x_history[cycle] = x + yield x - return self.x_history[cycles] + if instruction != "noop": + yield x + x += int(*args) class AssignmentOne(Assignment): example_result = 13140 def run(self, input: Iterator) -> int: - instructions = list(input) - return sum( [ - cycles * self.x_after(cycles, instructions) - for cycles in [220, 180, 140, 100, 60, 20] + cycle * x + for cycle, x in enumerate(self.parse_instructions(input), start=1) + if cycle in {20, 60, 100, 140, 180, 220} ] ) class AssignmentTwo(Assignment): - example_result = """##..##..##..##..##..##..##..##..##..##.. -###...###...###...###...###...###...###. -####....####....####....####....####.... -#####.....#####.....#####.....#####..... -######......######......######......#### -#######.......#######.......#######.....""" + example_result = ( + "██ ██ ██ ██ ██ ██ ██ ██ ██ ██ \n" + "███ ███ ███ ███ ███ ███ ███ \n" + "████ ████ ████ ████ ████ \n" + "█████ █████ █████ █████ \n" + "██████ ██████ ██████ ████\n" + "███████ ███████ ███████ " + ) def render_pixel(self, row: int, col: int, instructions: List[str]): cycle = row * 40 + col @@ -70,16 +56,15 @@ class AssignmentTwo(Assignment): return "." def run(self, input: Iterator) -> str: - instructions = list(input) + rows = [] + row = [] - screen = [ - [ - self.render_pixel(row, col, instructions) - for col in reversed(range(1, 41)) - ] - for row in reversed(range(6)) - ] + for cycle, x in enumerate(self.parse_instructions(instructions=input), start=1): + col = (cycle - 1) % 40 + row.append("█" if col in {x - 1, x, x + 1} else " ") - return "\n".join( - ["".join(reversed([str(i) for i in row])) for row in reversed(screen)] - ) + if cycle % 40 == 0: + rows.append(row) + row = [] + + return "\n".join("".join(row) for row in rows) -- cgit v1.2.3