Skip to content

Commit d0846e3

Browse files
committed
2022: Day 10
1 parent 910e2df commit d0846e3

File tree

3 files changed

+216
-0
lines changed

3 files changed

+216
-0
lines changed

2022/10/input.txt

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
addx 1
2+
addx 5
3+
noop
4+
addx -1
5+
noop
6+
noop
7+
addx 6
8+
addx 15
9+
addx -9
10+
noop
11+
addx -1
12+
addx 4
13+
addx 2
14+
addx -22
15+
addx 27
16+
addx -1
17+
addx 4
18+
noop
19+
addx 1
20+
addx 2
21+
noop
22+
noop
23+
noop
24+
noop
25+
addx 1
26+
addx -33
27+
addx 2
28+
addx 5
29+
addx 2
30+
addx 3
31+
addx -2
32+
addx 7
33+
noop
34+
addx -2
35+
addx -8
36+
addx 15
37+
addx 5
38+
noop
39+
noop
40+
addx -2
41+
addx 2
42+
noop
43+
noop
44+
addx 7
45+
addx -14
46+
noop
47+
addx -2
48+
addx -17
49+
addx 5
50+
addx -4
51+
noop
52+
addx 23
53+
addx -18
54+
noop
55+
noop
56+
noop
57+
addx 28
58+
addx -18
59+
addx 4
60+
noop
61+
noop
62+
addx -5
63+
addx 1
64+
addx 10
65+
addx 2
66+
noop
67+
noop
68+
addx -30
69+
addx 33
70+
addx -32
71+
noop
72+
noop
73+
addx -2
74+
addx 6
75+
addx -2
76+
addx 4
77+
addx 3
78+
addx 19
79+
addx 10
80+
addx -5
81+
addx -16
82+
addx 3
83+
addx -2
84+
addx 17
85+
addx -19
86+
addx 11
87+
addx 2
88+
addx 9
89+
noop
90+
addx -4
91+
addx -6
92+
addx -7
93+
addx -24
94+
noop
95+
addx 7
96+
addx -2
97+
addx 5
98+
addx 2
99+
addx 3
100+
addx -2
101+
addx 2
102+
addx 5
103+
addx 2
104+
addx 7
105+
addx -2
106+
noop
107+
addx 3
108+
addx -2
109+
addx 2
110+
addx 7
111+
noop
112+
addx -2
113+
addx -34
114+
addx 1
115+
addx 1
116+
noop
117+
noop
118+
noop
119+
addx 5
120+
noop
121+
noop
122+
addx 5
123+
addx -1
124+
noop
125+
addx 6
126+
addx -1
127+
noop
128+
addx 4
129+
addx 3
130+
addx 4
131+
addx -1
132+
addx 5
133+
noop
134+
addx 5
135+
noop
136+
noop
137+
noop
138+
noop
139+
noop
140+
addx 1
141+
noop
142+
noop

2022/10/main.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from collections import defaultdict
2+
3+
4+
class Day10:
5+
6+
def __init__(self):
7+
self.puzzle_input = self.load_input()
8+
9+
def part_one(self) -> int:
10+
cycles, _ = self.do_stuff()
11+
signal_strengths = [cycles[i] * i for i in [20, 60, 100, 140, 180, 220]]
12+
return sum(signal_strengths)
13+
14+
def part_two(self) -> None:
15+
_, all_CRT_rows = self.do_stuff()
16+
return '\n' + '\n'.join(' '.join(map(str, sublist)) for sublist in all_CRT_rows)
17+
18+
def do_stuff(self) -> None:
19+
cycles = defaultdict(int)
20+
x_register = 1
21+
current_cycle = 0
22+
23+
current_CRT_row = []
24+
all_CRT_rows = []
25+
26+
for line in self.puzzle_input:
27+
if line == 'noop':
28+
current_cycle += 1
29+
cycles[current_cycle] = x_register
30+
31+
pixel = self.draw_pixel(cycle=current_cycle, x=x_register)
32+
current_CRT_row.append(pixel)
33+
34+
if len(current_CRT_row) % 40 == 0:
35+
all_CRT_rows.append(current_CRT_row)
36+
current_CRT_row = []
37+
38+
else:
39+
_, num = line.split()
40+
for _ in range(2):
41+
current_cycle += 1
42+
cycles[current_cycle] = x_register
43+
44+
pixel = self.draw_pixel(cycle=current_cycle, x=x_register)
45+
current_CRT_row.append(pixel)
46+
47+
if len(current_CRT_row) % 40 == 0:
48+
all_CRT_rows.append(current_CRT_row)
49+
current_CRT_row = []
50+
51+
x_register += int(num)
52+
53+
return cycles, all_CRT_rows
54+
55+
@staticmethod
56+
def draw_pixel(cycle, x) -> str:
57+
if cycle % 40 in [x, x + 1, x + 2]:
58+
return "#"
59+
return "."
60+
61+
@staticmethod
62+
def load_input() -> list:
63+
list_input = []
64+
with open("input.txt") as file:
65+
for line in file:
66+
list_input.append(line.rstrip())
67+
68+
return list_input
69+
70+
71+
day = Day10()
72+
print(f'Result part 1: {day.part_one()}')
73+
print(f'Result part 2: {day.part_two()}')

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ My Python Solutions to [Advent of Code](https://adventofcode.com/)
1313
|7 |No Space Left On Device||| [🔗](2022/07/main.py) |
1414
|8 |Treetop Tree House||| [🔗](2022/08/main.py) |
1515
|9 |Rope Bridge||| [🔗](2022/09/main.py) |
16+
|10 |Cathode-Ray Tube||| [🔗](2022/10/main.py) |
1617

1718
## [2021](https://adventofcode.com/2021)
1819
| Day | Name | Part 1 | Part 2 | Code |

0 commit comments

Comments
 (0)