description.txt 9.8 KB

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