summaryrefslogtreecommitdiffstats
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
parentd60f6dc50ea3ea7066fc5e267076276939e2cbc2 (diff)
download2022-6355d8f564d9a57de6a971e0d0128c5affa08d45.tar.gz
2022-6355d8f564d9a57de6a971e0d0128c5affa08d45.tar.bz2
2022-6355d8f564d9a57de6a971e0d0128c5affa08d45.zip
Day10 [WIP]
-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
27noop
28addx 3
29addx 2
30addx -5
31addx 12
32addx 2
33addx 27
34addx -40
35addx 19
36addx 2
37addx 19
38addx -18
39addx 2
40addx 5
41addx 2
42addx -23
43addx 22
44addx 4
45addx -34
46addx -1
47addx 5
48noop
49addx 2
50addx 1
51addx 20
52addx -17
53noop
54addx 25
55addx -17
56addx -2
57noop
58addx 3
59addx 19
60addx -12
61addx 3
62addx -2
63addx 3
64addx 1
65noop
66addx 5
67noop
68noop
69addx -37
70addx 3
71addx 4
72noop
73addx 24
74addx -6
75addx -15
76addx 2
77noop
78addx 6
79addx -2
80addx 6
81addx -12
82addx -2
83addx 19
84noop
85noop
86noop
87addx 3
88noop
89addx 7
90addx -2
91addx -24
92addx -11
93addx 4
94addx 3
95addx -2
96noop
97addx 7
98addx -2
99addx 2
100noop
101addx 3
102addx 7
103noop
104addx -2
105addx 5
106addx 2
107addx 5
108noop
109noop
110noop
111addx 3
112addx -35
113addx 35
114addx -21
115addx -14
116noop
117addx 5
118addx 2
119addx 33
120addx -7
121addx -23
122addx 5
123addx 2
124addx 1
125noop
126noop
127addx 5
128addx -1
129noop
130addx 3
131addx -23
132addx 30
133addx 1
134noop
135addx 4
136addx -17
137addx 11
138noop
139noop