Skip to content

Commit 23b0e37

Browse files
committed
day10
1 parent 7d36dee commit 23b0e37

File tree

4 files changed

+689
-0
lines changed

4 files changed

+689
-0
lines changed

doc/v2022/day10.md

Lines changed: 333 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,333 @@
1+
# Day 10: Cathode-Ray Tube
2+
3+
## Part 1
4+
5+
You avoid the ropes, plunge into the river, and swim to shore.
6+
7+
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.
8+
9+
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.
10+
11+
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.
12+
13+
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:
14+
15+
* `addx V` takes two cycles to complete. After two cycles, the X register is increased by the value V. (V can be negative.)
16+
* `noop` takes one cycle to complete. It has no other effect.
17+
18+
The CPU uses these instructions in a program (your puzzle input) to, somehow, tell the screen what to draw.
19+
20+
Consider the following small program:
21+
22+
```
23+
noop
24+
addx 3
25+
addx -5
26+
```
27+
28+
Execution of this program proceeds as follows:
29+
30+
* 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.
31+
* At the start of the second cycle, the addx 3 instruction begins execution. During the second cycle, X is still 1.
32+
* During the third cycle, X is still 1. After the third cycle, the addx 3 instruction finishes execution, setting X to 4.
33+
* At the start of the fourth cycle, the addx -5 instruction begins execution. During the fourth cycle, X is still 4.
34+
* During the fifth cycle, X is still 4. After the fifth cycle, the addx -5 instruction finishes execution, setting X to -1.
35+
36+
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).
37+
38+
For example, consider this larger program:
39+
40+
```
41+
addx 15
42+
addx -11
43+
addx 6
44+
addx -3
45+
addx 5
46+
addx -1
47+
addx -8
48+
addx 13
49+
addx 4
50+
noop
51+
addx -1
52+
addx 5
53+
addx -1
54+
addx 5
55+
addx -1
56+
addx 5
57+
addx -1
58+
addx 5
59+
addx -1
60+
addx -35
61+
addx 1
62+
addx 24
63+
addx -19
64+
addx 1
65+
addx 16
66+
addx -11
67+
noop
68+
noop
69+
addx 21
70+
addx -15
71+
noop
72+
noop
73+
addx -3
74+
addx 9
75+
addx 1
76+
addx -3
77+
addx 8
78+
addx 1
79+
addx 5
80+
noop
81+
noop
82+
noop
83+
noop
84+
noop
85+
addx -36
86+
noop
87+
addx 1
88+
addx 7
89+
noop
90+
noop
91+
noop
92+
addx 2
93+
addx 6
94+
noop
95+
noop
96+
noop
97+
noop
98+
noop
99+
addx 1
100+
noop
101+
noop
102+
addx 7
103+
addx 1
104+
noop
105+
addx -13
106+
addx 13
107+
addx 7
108+
noop
109+
addx 1
110+
addx -33
111+
noop
112+
noop
113+
noop
114+
addx 2
115+
noop
116+
noop
117+
noop
118+
addx 8
119+
noop
120+
addx -1
121+
addx 2
122+
addx 1
123+
noop
124+
addx 17
125+
addx -9
126+
addx 1
127+
addx 1
128+
addx -3
129+
addx 11
130+
noop
131+
noop
132+
addx 1
133+
noop
134+
addx 1
135+
noop
136+
noop
137+
addx -13
138+
addx -19
139+
addx 1
140+
addx 3
141+
addx 26
142+
addx -30
143+
addx 12
144+
addx -1
145+
addx 3
146+
addx 1
147+
noop
148+
noop
149+
noop
150+
addx -9
151+
addx 18
152+
addx 1
153+
addx 2
154+
noop
155+
noop
156+
addx 9
157+
noop
158+
noop
159+
noop
160+
addx -1
161+
addx 2
162+
addx -37
163+
addx 1
164+
addx 3
165+
noop
166+
addx 15
167+
addx -21
168+
addx 22
169+
addx -6
170+
addx 1
171+
noop
172+
addx 2
173+
addx 1
174+
noop
175+
addx -10
176+
noop
177+
noop
178+
addx 20
179+
addx 1
180+
addx 2
181+
addx 2
182+
addx -6
183+
addx -11
184+
noop
185+
noop
186+
noop
187+
```
188+
189+
The interesting signal strengths can be determined as follows:
190+
191+
* 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.)
192+
* During the 60th cycle, register X has the value 19, so the signal strength is 60 * 19 = 1140.
193+
* During the 100th cycle, register X has the value 18, so the signal strength is 100 * 18 = 1800.
194+
* During the 140th cycle, register X has the value 21, so the signal strength is 140 * 21 = 2940.
195+
* During the 180th cycle, register X has the value 16, so the signal strength is 180 * 16 = 2880.
196+
* During the 220th cycle, register X has the value 18, so the signal strength is 220 * 18 = 3960.
197+
198+
The sum of these signal strengths is 13140.
199+
200+
Find the signal strength during the 20th, 60th, 100th, 140th, 180th, and 220th cycles. What is the sum of these six signal strengths?
201+
202+
## Part 2
203+
204+
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.)
205+
206+
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.
207+
208+
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:
209+
210+
```
211+
Cycle 1 -> ######################################## <- Cycle 40
212+
Cycle 41 -> ######################################## <- Cycle 80
213+
Cycle 81 -> ######################################## <- Cycle 120
214+
Cycle 121 -> ######################################## <- Cycle 160
215+
Cycle 161 -> ######################################## <- Cycle 200
216+
Cycle 201 -> ######################################## <- Cycle 240
217+
```
218+
219+
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 (.).
220+
221+
The first few pixels from the larger example above are drawn as follows:
222+
223+
```
224+
Sprite position: ###.....................................
225+
226+
Start cycle 1: begin executing addx 15
227+
During cycle 1: CRT draws pixel in position 0
228+
Current CRT row: #
229+
230+
During cycle 2: CRT draws pixel in position 1
231+
Current CRT row: ##
232+
End of cycle 2: finish executing addx 15 (Register X is now 16)
233+
Sprite position: ...............###......................
234+
235+
Start cycle 3: begin executing addx -11
236+
During cycle 3: CRT draws pixel in position 2
237+
Current CRT row: ##.
238+
239+
During cycle 4: CRT draws pixel in position 3
240+
Current CRT row: ##..
241+
End of cycle 4: finish executing addx -11 (Register X is now 5)
242+
Sprite position: ....###.................................
243+
244+
Start cycle 5: begin executing addx 6
245+
During cycle 5: CRT draws pixel in position 4
246+
Current CRT row: ##..#
247+
248+
During cycle 6: CRT draws pixel in position 5
249+
Current CRT row: ##..##
250+
End of cycle 6: finish executing addx 6 (Register X is now 11)
251+
Sprite position: ..........###...........................
252+
253+
Start cycle 7: begin executing addx -3
254+
During cycle 7: CRT draws pixel in position 6
255+
Current CRT row: ##..##.
256+
257+
During cycle 8: CRT draws pixel in position 7
258+
Current CRT row: ##..##..
259+
End of cycle 8: finish executing addx -3 (Register X is now 8)
260+
Sprite position: .......###..............................
261+
262+
Start cycle 9: begin executing addx 5
263+
During cycle 9: CRT draws pixel in position 8
264+
Current CRT row: ##..##..#
265+
266+
During cycle 10: CRT draws pixel in position 9
267+
Current CRT row: ##..##..##
268+
End of cycle 10: finish executing addx 5 (Register X is now 13)
269+
Sprite position: ............###.........................
270+
271+
Start cycle 11: begin executing addx -1
272+
During cycle 11: CRT draws pixel in position 10
273+
Current CRT row: ##..##..##.
274+
275+
During cycle 12: CRT draws pixel in position 11
276+
Current CRT row: ##..##..##..
277+
End of cycle 12: finish executing addx -1 (Register X is now 12)
278+
Sprite position: ...........###..........................
279+
280+
Start cycle 13: begin executing addx -8
281+
During cycle 13: CRT draws pixel in position 12
282+
Current CRT row: ##..##..##..#
283+
284+
During cycle 14: CRT draws pixel in position 13
285+
Current CRT row: ##..##..##..##
286+
End of cycle 14: finish executing addx -8 (Register X is now 4)
287+
Sprite position: ...###..................................
288+
289+
Start cycle 15: begin executing addx 13
290+
During cycle 15: CRT draws pixel in position 14
291+
Current CRT row: ##..##..##..##.
292+
293+
During cycle 16: CRT draws pixel in position 15
294+
Current CRT row: ##..##..##..##..
295+
End of cycle 16: finish executing addx 13 (Register X is now 17)
296+
Sprite position: ................###.....................
297+
298+
Start cycle 17: begin executing addx 4
299+
During cycle 17: CRT draws pixel in position 16
300+
Current CRT row: ##..##..##..##..#
301+
302+
During cycle 18: CRT draws pixel in position 17
303+
Current CRT row: ##..##..##..##..##
304+
End of cycle 18: finish executing addx 4 (Register X is now 21)
305+
Sprite position: ....................###.................
306+
307+
Start cycle 19: begin executing noop
308+
During cycle 19: CRT draws pixel in position 18
309+
Current CRT row: ##..##..##..##..##.
310+
End of cycle 19: finish executing noop
311+
312+
Start cycle 20: begin executing addx -1
313+
During cycle 20: CRT draws pixel in position 19
314+
Current CRT row: ##..##..##..##..##..
315+
316+
During cycle 21: CRT draws pixel in position 20
317+
Current CRT row: ##..##..##..##..##..#
318+
End of cycle 21: finish executing addx -1 (Register X is now 20)
319+
Sprite position: ...................###..................
320+
```
321+
322+
Allowing the program to run to completion causes the CRT to produce the following image:
323+
324+
```
325+
##..##..##..##..##..##..##..##..##..##..
326+
###...###...###...###...###...###...###.
327+
####....####....####....####....####....
328+
#####.....#####.....#####.....#####.....
329+
######......######......######......####
330+
#######.......#######.......#######.....
331+
```
332+
333+
Render the image given by your program. What eight capital letters appear on your CRT?

0 commit comments

Comments
 (0)