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