From 6355d8f564d9a57de6a971e0d0128c5affa08d45 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Sun, 11 Dec 2022 15:30:00 +0100 Subject: Day10 [WIP] --- day10/__init__.py | 85 +++++++++++++++++++++++++++++++ day10/example.txt | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ day10/input.txt | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 370 insertions(+) create mode 100644 day10/__init__.py create mode 100644 day10/example.txt create mode 100644 day10/input.txt (limited to 'day10') 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 @@ +# -*- coding: utf-8 -*- +from abc import ABC +from functools import lru_cache +from typing import Iterator, List, Dict, Any + +from aoc import BaseAssignment + + +class Assignment(BaseAssignment, ABC): + def __init__(self, path): + 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 + x = 1 + + while cycle <= cycles and len(instructions) > 0: + instruction, *instructions = instructions + instruction, *args = instruction.split(" ") + + for val in getattr(self, instruction)(*args): + x += val + cycle += 1 + self.x_history[cycle] = x + + return self.x_history[cycles] + + +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] + ] + ) + + +class AssignmentTwo(Assignment): + example_result = """##..##..##..##..##..##..##..##..##..##.. +###...###...###...###...###...###...###. +####....####....####....####....####.... +#####.....#####.....#####.....#####..... +######......######......######......#### +#######.......#######.......#######.....""" + + def render_pixel(self, row: int, col: int, instructions: List[str]): + cycle = row * 40 + col + x_register = self.x_after(cycle, instructions) + + if col in [x_register - 1, x_register, x_register + 1]: + return "#" + return "." + + def run(self, input: Iterator) -> str: + instructions = list(input) + + screen = [ + [ + self.render_pixel(row, col, instructions) + for col in reversed(range(1, 41)) + ] + for row in reversed(range(6)) + ] + + return "\n".join( + ["".join(reversed([str(i) for i in row])) for row in reversed(screen)] + ) 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 @@ +addx 15 +addx -11 +addx 6 +addx -3 +addx 5 +addx -1 +addx -8 +addx 13 +addx 4 +noop +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx 5 +addx -1 +addx -35 +addx 1 +addx 24 +addx -19 +addx 1 +addx 16 +addx -11 +noop +noop +addx 21 +addx -15 +noop +noop +addx -3 +addx 9 +addx 1 +addx -3 +addx 8 +addx 1 +addx 5 +noop +noop +noop +noop +noop +addx -36 +noop +addx 1 +addx 7 +noop +noop +noop +addx 2 +addx 6 +noop +noop +noop +noop +noop +addx 1 +noop +noop +addx 7 +addx 1 +noop +addx -13 +addx 13 +addx 7 +noop +addx 1 +addx -33 +noop +noop +noop +addx 2 +noop +noop +noop +addx 8 +noop +addx -1 +addx 2 +addx 1 +noop +addx 17 +addx -9 +addx 1 +addx 1 +addx -3 +addx 11 +noop +noop +addx 1 +noop +addx 1 +noop +noop +addx -13 +addx -19 +addx 1 +addx 3 +addx 26 +addx -30 +addx 12 +addx -1 +addx 3 +addx 1 +noop +noop +noop +addx -9 +addx 18 +addx 1 +addx 2 +noop +noop +addx 9 +noop +noop +noop +addx -1 +addx 2 +addx -37 +addx 1 +addx 3 +noop +addx 15 +addx -21 +addx 22 +addx -6 +addx 1 +noop +addx 2 +addx 1 +noop +addx -10 +noop +noop +addx 20 +addx 1 +addx 2 +addx 2 +addx -6 +addx -11 +noop +noop +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 @@ +noop +noop +noop +addx 5 +noop +addx 1 +addx 2 +addx 5 +addx 2 +addx 1 +noop +addx 5 +noop +addx -1 +noop +addx 5 +noop +noop +addx 5 +addx 1 +noop +noop +addx 3 +addx 2 +noop +addx -38 +noop +addx 3 +addx 2 +addx -5 +addx 12 +addx 2 +addx 27 +addx -40 +addx 19 +addx 2 +addx 19 +addx -18 +addx 2 +addx 5 +addx 2 +addx -23 +addx 22 +addx 4 +addx -34 +addx -1 +addx 5 +noop +addx 2 +addx 1 +addx 20 +addx -17 +noop +addx 25 +addx -17 +addx -2 +noop +addx 3 +addx 19 +addx -12 +addx 3 +addx -2 +addx 3 +addx 1 +noop +addx 5 +noop +noop +addx -37 +addx 3 +addx 4 +noop +addx 24 +addx -6 +addx -15 +addx 2 +noop +addx 6 +addx -2 +addx 6 +addx -12 +addx -2 +addx 19 +noop +noop +noop +addx 3 +noop +addx 7 +addx -2 +addx -24 +addx -11 +addx 4 +addx 3 +addx -2 +noop +addx 7 +addx -2 +addx 2 +noop +addx 3 +addx 7 +noop +addx -2 +addx 5 +addx 2 +addx 5 +noop +noop +noop +addx 3 +addx -35 +addx 35 +addx -21 +addx -14 +noop +addx 5 +addx 2 +addx 33 +addx -7 +addx -23 +addx 5 +addx 2 +addx 1 +noop +noop +addx 5 +addx -1 +noop +addx 3 +addx -23 +addx 30 +addx 1 +noop +addx 4 +addx -17 +addx 11 +noop +noop -- cgit v1.2.3