Skip to content

Commit 21a188d

Browse files
committed
Add day 10
1 parent 6061462 commit 21a188d

File tree

5 files changed

+715
-0
lines changed

5 files changed

+715
-0
lines changed

day-10/README.md

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

0 commit comments

Comments
 (0)