diff options
Diffstat (limited to 'day10/__init__.py')
| -rw-r--r-- | day10/__init__.py | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/day10/__init__.py b/day10/__init__.py new file mode 100644 index 0000000..e6d385a --- /dev/null +++ b/day10/__init__.py | |||
| @@ -0,0 +1,85 @@ | |||
| 1 | # -*- coding: utf-8 -*- | ||
| 2 | from abc import ABC | ||
| 3 | from functools import lru_cache | ||
| 4 | from typing import Iterator, List, Dict, Any | ||
| 5 | |||
| 6 | from aoc import BaseAssignment | ||
| 7 | |||
| 8 | |||
| 9 | class Assignment(BaseAssignment, ABC): | ||
| 10 | def __init__(self, path): | ||
| 11 | super().__init__(path) | ||
| 12 | self.x_history = {1: 1} | ||
| 13 | |||
| 14 | def addx(self, value: str) -> Iterator[int]: | ||
| 15 | yield 0 | ||
| 16 | yield int(value) | ||
| 17 | |||
| 18 | def noop(self) -> Iterator: | ||
| 19 | yield 0 | ||
| 20 | |||
| 21 | def x_after(self, cycles: int, instructions: List[str]): | ||
| 22 | try: | ||
| 23 | return self.x_history[cycles] | ||
| 24 | except KeyError: | ||
| 25 | pass | ||
| 26 | |||
| 27 | cycle = 1 | ||
| 28 | x = 1 | ||
| 29 | |||
| 30 | while cycle <= cycles and len(instructions) > 0: | ||
| 31 | instruction, *instructions = instructions | ||
| 32 | instruction, *args = instruction.split(" ") | ||
| 33 | |||
| 34 | for val in getattr(self, instruction)(*args): | ||
| 35 | x += val | ||
| 36 | cycle += 1 | ||
| 37 | self.x_history[cycle] = x | ||
| 38 | |||
| 39 | return self.x_history[cycles] | ||
| 40 | |||
| 41 | |||
| 42 | class AssignmentOne(Assignment): | ||
| 43 | example_result = 13140 | ||
| 44 | |||
| 45 | def run(self, input: Iterator) -> int: | ||
| 46 | instructions = list(input) | ||
| 47 | |||
| 48 | return sum( | ||
| 49 | [ | ||
| 50 | cycles * self.x_after(cycles, instructions) | ||
| 51 | for cycles in [220, 180, 140, 100, 60, 20] | ||
| 52 | ] | ||
| 53 | ) | ||
| 54 | |||
| 55 | |||
| 56 | class AssignmentTwo(Assignment): | ||
| 57 | example_result = """##..##..##..##..##..##..##..##..##..##.. | ||
| 58 | ###...###...###...###...###...###...###. | ||
| 59 | ####....####....####....####....####.... | ||
| 60 | #####.....#####.....#####.....#####..... | ||
| 61 | ######......######......######......#### | ||
| 62 | #######.......#######.......#######.....""" | ||
| 63 | |||
| 64 | def render_pixel(self, row: int, col: int, instructions: List[str]): | ||
| 65 | cycle = row * 40 + col | ||
| 66 | x_register = self.x_after(cycle, instructions) | ||
| 67 | |||
| 68 | if col in [x_register - 1, x_register, x_register + 1]: | ||
| 69 | return "#" | ||
| 70 | return "." | ||
| 71 | |||
| 72 | def run(self, input: Iterator) -> str: | ||
| 73 | instructions = list(input) | ||
| 74 | |||
| 75 | screen = [ | ||
| 76 | [ | ||
| 77 | self.render_pixel(row, col, instructions) | ||
| 78 | for col in reversed(range(1, 41)) | ||
| 79 | ] | ||
| 80 | for row in reversed(range(6)) | ||
| 81 | ] | ||
| 82 | |||
| 83 | return "\n".join( | ||
| 84 | ["".join(reversed([str(i) for i in row])) for row in reversed(screen)] | ||
| 85 | ) | ||
