summaryrefslogtreecommitdiffstats
path: root/day10/__init__.py
diff options
context:
space:
mode:
authorGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-11 15:30:00 +0100
committerGravatar Tom van der Lee <t0m.vd.l33@gmail.com>2022-12-11 15:30:00 +0100
commit6355d8f564d9a57de6a971e0d0128c5affa08d45 (patch)
tree661fa148fff591179236938906b99777c4f3824d /day10/__init__.py
parentd60f6dc50ea3ea7066fc5e267076276939e2cbc2 (diff)
download2022-6355d8f564d9a57de6a971e0d0128c5affa08d45.tar.gz
2022-6355d8f564d9a57de6a971e0d0128c5affa08d45.tar.bz2
2022-6355d8f564d9a57de6a971e0d0128c5affa08d45.zip
Day10 [WIP]
Diffstat (limited to 'day10/__init__.py')
-rw-r--r--day10/__init__.py85
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 -*-
2from abc import ABC
3from functools import lru_cache
4from typing import Iterator, List, Dict, Any
5
6from aoc import BaseAssignment
7
8
9class 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
42class 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
56class 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 )