Skip to content

Commit 50741dc

Browse files
author
Ivan Arabadzhiyski
committed
day10
1 parent 05f7294 commit 50741dc

File tree

6 files changed

+650
-1
lines changed

6 files changed

+650
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ Repo for my [Advent of Code 2022](https://adventofcode.com/) solutions.
99
- [Day 6: Tuning Trouble](./solutions/day6)
1010
- [Day 7: No Space Left On Device](./solutions/day7)
1111
- [Day 8: Treetop Tree House](./solutions/day8)
12-
- [Day 9: Rope Bridge](./solutions/9)
12+
- [Day 9: Rope Bridge](./solutions/day9)
13+
- [Day 10: Cathode-Ray Tube](./solutions/day10)

solutions/day10/README.md

Lines changed: 319 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,319 @@
1+
## --- Day 10: Cathode-Ray Tube ---
2+
3+
You avoid the ropes, plunge into the river, and swim to shore.
4+
The Elves yell something about meeting back up with them upriver, but the river is too loud to tell exactly what they're saying. They finish crossing the bridge and disappear from view.
5+
Situations like this must be why the Elves prioritized getting the communication system on your handheld device working. You pull it out of your pack, but the amount of water slowly draining from a big crack in its screen tells you it probably won't be of much immediate use.
6+
Unless, that is, you can design a replacement for the device's video system! It seems to be some kind of cathode-ray tube screen and simple CPU that are both driven by a precise clock circuit. The clock circuit ticks at a constant rate; each tick is called a cycle.
7+
Start by figuring out the signal being sent by the CPU. The CPU has a single register, X, which starts with the value 1. It supports only two instructions:
8+
9+
addx V takes two cycles to complete. After two cycles, the X register is increased by the value V. (V can be negative.)
10+
noop takes one cycle to complete. It has no other effect.
11+
12+
The CPU uses these instructions in a program (your puzzle input) to, somehow, tell the screen what to draw.
13+
Consider the following small program:
14+
noop
15+
addx 3
16+
addx -5
17+
18+
Execution of this program proceeds as follows:
19+
20+
At the start of the first cycle, the noop instruction begins execution. During the first cycle, X is 1. After the first cycle, the noop instruction finishes execution, doing nothing.
21+
At the start of the second cycle, the addx 3 instruction begins execution. During the second cycle, X is still 1.
22+
During the third cycle, X is still 1. After the third cycle, the addx 3 instruction finishes execution, setting X to 4.
23+
At the start of the fourth cycle, the addx -5 instruction begins execution. During the fourth cycle, X is still 4.
24+
During the fifth cycle, X is still 4. After the fifth cycle, the addx -5 instruction finishes execution, setting X to -1.
25+
26+
Maybe you can learn something by looking at the value of the X register throughout execution. For now, consider the signal strength (the cycle number multiplied by the value of the X register) during the 20th cycle and every 40 cycles after that (that is, during the 20th, 60th, 100th, 140th, 180th, and 220th cycles).
27+
For example, consider this larger program:
28+
```
29+
addx 15
30+
addx -11
31+
addx 6
32+
addx -3
33+
addx 5
34+
addx -1
35+
addx -8
36+
addx 13
37+
addx 4
38+
noop
39+
addx -1
40+
addx 5
41+
addx -1
42+
addx 5
43+
addx -1
44+
addx 5
45+
addx -1
46+
addx 5
47+
addx -1
48+
addx -35
49+
addx 1
50+
addx 24
51+
addx -19
52+
addx 1
53+
addx 16
54+
addx -11
55+
noop
56+
noop
57+
addx 21
58+
addx -15
59+
noop
60+
noop
61+
addx -3
62+
addx 9
63+
addx 1
64+
addx -3
65+
addx 8
66+
addx 1
67+
addx 5
68+
noop
69+
noop
70+
noop
71+
noop
72+
noop
73+
addx -36
74+
noop
75+
addx 1
76+
addx 7
77+
noop
78+
noop
79+
noop
80+
addx 2
81+
addx 6
82+
noop
83+
noop
84+
noop
85+
noop
86+
noop
87+
addx 1
88+
noop
89+
noop
90+
addx 7
91+
addx 1
92+
noop
93+
addx -13
94+
addx 13
95+
addx 7
96+
noop
97+
addx 1
98+
addx -33
99+
noop
100+
noop
101+
noop
102+
addx 2
103+
noop
104+
noop
105+
noop
106+
addx 8
107+
noop
108+
addx -1
109+
addx 2
110+
addx 1
111+
noop
112+
addx 17
113+
addx -9
114+
addx 1
115+
addx 1
116+
addx -3
117+
addx 11
118+
noop
119+
noop
120+
addx 1
121+
noop
122+
addx 1
123+
noop
124+
noop
125+
addx -13
126+
addx -19
127+
addx 1
128+
addx 3
129+
addx 26
130+
addx -30
131+
addx 12
132+
addx -1
133+
addx 3
134+
addx 1
135+
noop
136+
noop
137+
noop
138+
addx -9
139+
addx 18
140+
addx 1
141+
addx 2
142+
noop
143+
noop
144+
addx 9
145+
noop
146+
noop
147+
noop
148+
addx -1
149+
addx 2
150+
addx -37
151+
addx 1
152+
addx 3
153+
noop
154+
addx 15
155+
addx -21
156+
addx 22
157+
addx -6
158+
addx 1
159+
noop
160+
addx 2
161+
addx 1
162+
noop
163+
addx -10
164+
noop
165+
noop
166+
addx 20
167+
addx 1
168+
addx 2
169+
addx 2
170+
addx -6
171+
addx -11
172+
noop
173+
noop
174+
noop
175+
```
176+
177+
The interesting signal strengths can be determined as follows:
178+
179+
During the 20th cycle, register X has the value 21, so the signal strength is 20 * 21 = 420. (The 20th cycle occurs in the middle of the second addx -1, so the value of register X is the starting value, 1, plus all of the other addx values up to that point: 1 + 15 - 11 + 6 - 3 + 5 - 1 - 8 + 13 + 4 = 21.)
180+
During the 60th cycle, register X has the value 19, so the signal strength is 60 * 19 = 1140.
181+
During the 100th cycle, register X has the value 18, so the signal strength is 100 * 18 = 1800.
182+
During the 140th cycle, register X has the value 21, so the signal strength is 140 * 21 = 2940.
183+
During the 180th cycle, register X has the value 16, so the signal strength is 180 * 16 = 2880.
184+
During the 220th cycle, register X has the value 18, so the signal strength is 220 * 18 = 3960.
185+
186+
The sum of these signal strengths is 13140.
187+
Find the signal strength during the 20th, 60th, 100th, 140th, 180th, and 220th cycles. `What is the sum of these six signal strengths?`
188+
189+
Your puzzle answer was `13060`.
190+
191+
## --- Part Two ---
192+
193+
It seems like the X register controls the horizontal position of a sprite. Specifically, the sprite is 3 pixels wide, and the X register sets the horizontal position of the middle of that sprite. (In this system, there is no such thing as "vertical position": if the sprite's horizontal position puts its pixels where the CRT is currently drawing, then those pixels will be drawn.)
194+
195+
You count the pixels on the CRT: 40 wide and 6 high. This CRT screen draws the top row of pixels left-to-right, then the row below that, and so on. The left-most pixel in each row is in position 0, and the right-most pixel in each row is in position 39.
196+
197+
Like the CPU, the CRT is tied closely to the clock circuit: the CRT draws a single pixel during each cycle. Representing each pixel of the screen as a #, here are the cycles during which the first and last pixel in each row are drawn:
198+
```
199+
Cycle 1 -> ######################################## <- Cycle 40
200+
Cycle 41 -> ######################################## <- Cycle 80
201+
Cycle 81 -> ######################################## <- Cycle 120
202+
Cycle 121 -> ######################################## <- Cycle 160
203+
Cycle 161 -> ######################################## <- Cycle 200
204+
Cycle 201 -> ######################################## <- Cycle 240
205+
```
206+
So, by carefully timing the CPU instructions and the CRT drawing operations, you should be able to determine whether the sprite is visible the instant each pixel is drawn. If the sprite is positioned such that one of its three pixels is the pixel currently being drawn, the screen produces a lit pixel (#); otherwise, the screen leaves the pixel dark (.).
207+
208+
The first few pixels from the larger example above are drawn as follows:
209+
210+
```
211+
Sprite position: ###.....................................
212+
213+
Start cycle 1: begin executing addx 15
214+
During cycle 1: CRT draws pixel in position 0
215+
Current CRT row: #
216+
217+
During cycle 2: CRT draws pixel in position 1
218+
Current CRT row: ##
219+
End of cycle 2: finish executing addx 15 (Register X is now 16)
220+
Sprite position: ...............###......................
221+
222+
Start cycle 3: begin executing addx -11
223+
During cycle 3: CRT draws pixel in position 2
224+
Current CRT row: ##.
225+
226+
During cycle 4: CRT draws pixel in position 3
227+
Current CRT row: ##..
228+
End of cycle 4: finish executing addx -11 (Register X is now 5)
229+
Sprite position: ....###.................................
230+
231+
Start cycle 5: begin executing addx 6
232+
During cycle 5: CRT draws pixel in position 4
233+
Current CRT row: ##..#
234+
235+
During cycle 6: CRT draws pixel in position 5
236+
Current CRT row: ##..##
237+
End of cycle 6: finish executing addx 6 (Register X is now 11)
238+
Sprite position: ..........###...........................
239+
240+
Start cycle 7: begin executing addx -3
241+
During cycle 7: CRT draws pixel in position 6
242+
Current CRT row: ##..##.
243+
244+
During cycle 8: CRT draws pixel in position 7
245+
Current CRT row: ##..##..
246+
End of cycle 8: finish executing addx -3 (Register X is now 8)
247+
Sprite position: .......###..............................
248+
249+
Start cycle 9: begin executing addx 5
250+
During cycle 9: CRT draws pixel in position 8
251+
Current CRT row: ##..##..#
252+
253+
During cycle 10: CRT draws pixel in position 9
254+
Current CRT row: ##..##..##
255+
End of cycle 10: finish executing addx 5 (Register X is now 13)
256+
Sprite position: ............###.........................
257+
258+
Start cycle 11: begin executing addx -1
259+
During cycle 11: CRT draws pixel in position 10
260+
Current CRT row: ##..##..##.
261+
262+
During cycle 12: CRT draws pixel in position 11
263+
Current CRT row: ##..##..##..
264+
End of cycle 12: finish executing addx -1 (Register X is now 12)
265+
Sprite position: ...........###..........................
266+
267+
Start cycle 13: begin executing addx -8
268+
During cycle 13: CRT draws pixel in position 12
269+
Current CRT row: ##..##..##..#
270+
271+
During cycle 14: CRT draws pixel in position 13
272+
Current CRT row: ##..##..##..##
273+
End of cycle 14: finish executing addx -8 (Register X is now 4)
274+
Sprite position: ...###..................................
275+
276+
Start cycle 15: begin executing addx 13
277+
During cycle 15: CRT draws pixel in position 14
278+
Current CRT row: ##..##..##..##.
279+
280+
During cycle 16: CRT draws pixel in position 15
281+
Current CRT row: ##..##..##..##..
282+
End of cycle 16: finish executing addx 13 (Register X is now 17)
283+
Sprite position: ................###.....................
284+
285+
Start cycle 17: begin executing addx 4
286+
During cycle 17: CRT draws pixel in position 16
287+
Current CRT row: ##..##..##..##..#
288+
289+
During cycle 18: CRT draws pixel in position 17
290+
Current CRT row: ##..##..##..##..##
291+
End of cycle 18: finish executing addx 4 (Register X is now 21)
292+
Sprite position: ....................###.................
293+
294+
Start cycle 19: begin executing noop
295+
During cycle 19: CRT draws pixel in position 18
296+
Current CRT row: ##..##..##..##..##.
297+
End of cycle 19: finish executing noop
298+
299+
Start cycle 20: begin executing addx -1
300+
During cycle 20: CRT draws pixel in position 19
301+
Current CRT row: ##..##..##..##..##..
302+
303+
During cycle 21: CRT draws pixel in position 20
304+
Current CRT row: ##..##..##..##..##..#
305+
End of cycle 21: finish executing addx -1 (Register X is now 20)
306+
Sprite position: ...................###..................
307+
```
308+
Allowing the program to run to completion causes the CRT to produce the following image:
309+
```
310+
##..##..##..##..##..##..##..##..##..##..
311+
###...###...###...###...###...###...###.
312+
####....####....####....####....####....
313+
#####.....#####.....#####.....#####.....
314+
######......######......######......####
315+
#######.......#######.......#######.....
316+
```
317+
Render the image given by your program. What eight capital letters appear on your CRT?
318+
319+
Your puzzle answer was `FJUBULRZ`.

0 commit comments

Comments
 (0)