diff options
Diffstat (limited to 'day10')
| -rw-r--r-- | day10/__init__.py | 85 | ||||
| -rw-r--r-- | day10/example.txt | 146 | ||||
| -rw-r--r-- | day10/input.txt | 139 |
3 files changed, 370 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 | ) | ||
diff --git a/day10/example.txt b/day10/example.txt new file mode 100644 index 0000000..37ee8ee --- /dev/null +++ b/day10/example.txt | |||
| @@ -0,0 +1,146 @@ | |||
| 1 | addx 15 | ||
| 2 | addx -11 | ||
| 3 | addx 6 | ||
| 4 | addx -3 | ||
| 5 | addx 5 | ||
| 6 | addx -1 | ||
| 7 | addx -8 | ||
| 8 | addx 13 | ||
| 9 | addx 4 | ||
| 10 | noop | ||
| 11 | addx -1 | ||
| 12 | addx 5 | ||
| 13 | addx -1 | ||
| 14 | addx 5 | ||
| 15 | addx -1 | ||
| 16 | addx 5 | ||
| 17 | addx -1 | ||
| 18 | addx 5 | ||
| 19 | addx -1 | ||
| 20 | addx -35 | ||
| 21 | addx 1 | ||
| 22 | addx 24 | ||
| 23 | addx -19 | ||
| 24 | addx 1 | ||
| 25 | addx 16 | ||
| 26 | addx -11 | ||
| 27 | noop | ||
| 28 | noop | ||
| 29 | addx 21 | ||
| 30 | addx -15 | ||
| 31 | noop | ||
| 32 | noop | ||
| 33 | addx -3 | ||
| 34 | addx 9 | ||
| 35 | addx 1 | ||
| 36 | addx -3 | ||
| 37 | addx 8 | ||
| 38 | addx 1 | ||
| 39 | addx 5 | ||
| 40 | noop | ||
| 41 | noop | ||
| 42 | noop | ||
| 43 | noop | ||
| 44 | noop | ||
| 45 | addx -36 | ||
| 46 | noop | ||
| 47 | addx 1 | ||
| 48 | addx 7 | ||
| 49 | noop | ||
| 50 | noop | ||
| 51 | noop | ||
| 52 | addx 2 | ||
| 53 | addx 6 | ||
| 54 | noop | ||
| 55 | noop | ||
| 56 | noop | ||
| 57 | noop | ||
| 58 | noop | ||
| 59 | addx 1 | ||
| 60 | noop | ||
| 61 | noop | ||
| 62 | addx 7 | ||
| 63 | addx 1 | ||
| 64 | noop | ||
| 65 | addx -13 | ||
| 66 | addx 13 | ||
| 67 | addx 7 | ||
| 68 | noop | ||
| 69 | addx 1 | ||
| 70 | addx -33 | ||
| 71 | noop | ||
| 72 | noop | ||
| 73 | noop | ||
| 74 | addx 2 | ||
| 75 | noop | ||
| 76 | noop | ||
| 77 | noop | ||
| 78 | addx 8 | ||
| 79 | noop | ||
| 80 | addx -1 | ||
| 81 | addx 2 | ||
| 82 | addx 1 | ||
| 83 | noop | ||
| 84 | addx 17 | ||
| 85 | addx -9 | ||
| 86 | addx 1 | ||
| 87 | addx 1 | ||
| 88 | addx -3 | ||
| 89 | addx 11 | ||
| 90 | noop | ||
| 91 | noop | ||
| 92 | addx 1 | ||
| 93 | noop | ||
| 94 | addx 1 | ||
| 95 | noop | ||
| 96 | noop | ||
| 97 | addx -13 | ||
| 98 | addx -19 | ||
| 99 | addx 1 | ||
| 100 | addx 3 | ||
| 101 | addx 26 | ||
| 102 | addx -30 | ||
| 103 | addx 12 | ||
| 104 | addx -1 | ||
| 105 | addx 3 | ||
| 106 | addx 1 | ||
| 107 | noop | ||
| 108 | noop | ||
| 109 | noop | ||
| 110 | addx -9 | ||
| 111 | addx 18 | ||
| 112 | addx 1 | ||
| 113 | addx 2 | ||
| 114 | noop | ||
| 115 | noop | ||
| 116 | addx 9 | ||
| 117 | noop | ||
| 118 | noop | ||
| 119 | noop | ||
| 120 | addx -1 | ||
| 121 | addx 2 | ||
| 122 | addx -37 | ||
| 123 | addx 1 | ||
| 124 | addx 3 | ||
| 125 | noop | ||
| 126 | addx 15 | ||
| 127 | addx -21 | ||
| 128 | addx 22 | ||
| 129 | addx -6 | ||
| 130 | addx 1 | ||
| 131 | noop | ||
| 132 | addx 2 | ||
| 133 | addx 1 | ||
| 134 | noop | ||
| 135 | addx -10 | ||
| 136 | noop | ||
| 137 | noop | ||
| 138 | addx 20 | ||
| 139 | addx 1 | ||
| 140 | addx 2 | ||
| 141 | addx 2 | ||
| 142 | addx -6 | ||
| 143 | addx -11 | ||
| 144 | noop | ||
| 145 | noop | ||
| 146 | noop | ||
diff --git a/day10/input.txt b/day10/input.txt new file mode 100644 index 0000000..008b1e7 --- /dev/null +++ b/day10/input.txt | |||
| @@ -0,0 +1,139 @@ | |||
| 1 | noop | ||
| 2 | noop | ||
| 3 | noop | ||
| 4 | addx 5 | ||
| 5 | noop | ||
| 6 | addx 1 | ||
| 7 | addx 2 | ||
| 8 | addx 5 | ||
| 9 | addx 2 | ||
| 10 | addx 1 | ||
| 11 | noop | ||
| 12 | addx 5 | ||
| 13 | noop | ||
| 14 | addx -1 | ||
| 15 | noop | ||
| 16 | addx 5 | ||
| 17 | noop | ||
| 18 | noop | ||
| 19 | addx 5 | ||
| 20 | addx 1 | ||
| 21 | noop | ||
| 22 | noop | ||
| 23 | addx 3 | ||
| 24 | addx 2 | ||
| 25 | noop | ||
| 26 | addx -38 | ||
