description.txt 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. --- Day 24: Crossed Wires ---
  2. You and The Historians arrive at the edge of a large grove somewhere in the jungle. After the last incident, the Elves installed a small device that monitors the fruit. While The Historians search the grove, one of them asks if you can take a look at the monitoring device; apparently, it's been malfunctioning recently.
  3. The device seems to be trying to produce a number through some boolean logic gates. Each gate has two inputs and one output. The gates all operate on values that are either true (1) or false (0).
  4. AND gates output 1 if both inputs are 1; if either input is 0, these gates output 0.
  5. OR gates output 1 if one or both inputs is 1; if both inputs are 0, these gates output 0.
  6. XOR gates output 1 if the inputs are different; if the inputs are the same, these gates output 0.
  7. Gates wait until both inputs are received before producing output; wires can carry 0, 1 or no value at all. There are no loops; once a gate has determined its output, the output will not change until the whole system is reset. Each wire is connected to at most one gate output, but can be connected to many gate inputs.
  8. Rather than risk getting shocked while tinkering with the live system, you write down all of the gate connections and initial wire values (your puzzle input) so you can consider them in relative safety. For example:
  9. x00: 1
  10. x01: 1
  11. x02: 1
  12. y00: 0
  13. y01: 1
  14. y02: 0
  15. x00 AND y00 -> z00
  16. x01 XOR y01 -> z01
  17. x02 OR y02 -> z02
  18. Because gates wait for input, some wires need to start with a value (as inputs to the entire system). The first section specifies these values. For example, x00: 1 means that the wire named x00 starts with the value 1 (as if a gate is already outputting that value onto that wire).
  19. The second section lists all of the gates and the wires connected to them. For example, x00 AND y00 -> z00 describes an instance of an AND gate which has wires x00 and y00 connected to its inputs and which will write its output to wire z00.
  20. In this example, simulating these gates eventually causes 0 to appear on wire z00, 0 to appear on wire z01, and 1 to appear on wire z02.
  21. Ultimately, the system is trying to produce a number by combining the bits on all wires starting with z. z00 is the least significant bit, then z01, then z02, and so on.
  22. In this example, the three output bits form the binary number 100 which is equal to the decimal number 4.
  23. Here's a larger example:
  24. x00: 1
  25. x01: 0
  26. x02: 1
  27. x03: 1
  28. x04: 0
  29. y00: 1
  30. y01: 1
  31. y02: 1
  32. y03: 1
  33. y04: 1
  34. ntg XOR fgs -> mjb
  35. y02 OR x01 -> tnw
  36. kwq OR kpj -> z05
  37. x00 OR x03 -> fst
  38. tgd XOR rvg -> z01
  39. vdt OR tnw -> bfw
  40. bfw AND frj -> z10
  41. ffh OR nrd -> bqk
  42. y00 AND y03 -> djm
  43. y03 OR y00 -> psh
  44. bqk OR frj -> z08
  45. tnw OR fst -> frj
  46. gnj AND tgd -> z11
  47. bfw XOR mjb -> z00
  48. x03 OR x00 -> vdt
  49. gnj AND wpb -> z02
  50. x04 AND y00 -> kjc
  51. djm OR pbm -> qhw
  52. nrd AND vdt -> hwm
  53. kjc AND fst -> rvg
  54. y04 OR y02 -> fgs
  55. y01 AND x02 -> pbm
  56. ntg OR kjc -> kwq
  57. psh XOR fgs -> tgd
  58. qhw XOR tgd -> z09
  59. pbm OR djm -> kpj
  60. x03 XOR y03 -> ffh
  61. x00 XOR y04 -> ntg
  62. bfw OR bqk -> z06
  63. nrd XOR fgs -> wpb
  64. frj XOR qhw -> z04
  65. bqk OR frj -> z07
  66. y03 OR x01 -> nrd
  67. hwm AND bqk -> z03
  68. tgd XOR rvg -> z12
  69. tnw OR pbm -> gnj
  70. After waiting for values on all wires starting with z, the wires in this system have the following values:
  71. bfw: 1
  72. bqk: 1
  73. djm: 1
  74. ffh: 0
  75. fgs: 1
  76. frj: 1
  77. fst: 1
  78. gnj: 1
  79. hwm: 1
  80. kjc: 0
  81. kpj: 1
  82. kwq: 0
  83. mjb: 1
  84. nrd: 1
  85. ntg: 0
  86. pbm: 1
  87. psh: 1
  88. qhw: 1
  89. rvg: 0
  90. tgd: 0
  91. tnw: 1
  92. vdt: 1
  93. wpb: 0
  94. z00: 0
  95. z01: 0
  96. z02: 0
  97. z03: 1
  98. z04: 0
  99. z05: 1
  100. z06: 1
  101. z07: 1
  102. z08: 1
  103. z09: 1
  104. z10: 1
  105. z11: 0
  106. z12: 0
  107. Combining the bits from all wires starting with z produces the binary number 0011111101000. Converting this number to decimal produces 2024.
  108. Simulate the system of gates and wires. What decimal number does it output on the wires starting with z?
  109. Your puzzle answer was 65635066541798.
  110. The first half of this puzzle is complete! It provides one gold star: *
  111. --- Part Two ---
  112. After inspecting the monitoring device more closely, you determine that the system you're simulating is trying to add two binary numbers.
  113. Specifically, it is treating the bits on wires starting with x as one binary number, treating the bits on wires starting with y as a second binary number, and then attempting to add those two numbers together. The output of this operation is produced as a binary number on the wires starting with z. (In all three cases, wire 00 is the least significant bit, then 01, then 02, and so on.)
  114. The initial values for the wires in your puzzle input represent just one instance of a pair of numbers that sum to the wrong value. Ultimately, any two binary numbers provided as input should be handled correctly. That is, for any combination of bits on wires starting with x and wires starting with y, the sum of the two numbers those bits represent should be produced as a binary number on the wires starting with z.
  115. For example, if you have an addition system with four x wires, four y wires, and five z wires, you should be able to supply any four-bit number on the x wires, any four-bit number on the y numbers, and eventually find the sum of those two numbers as a five-bit number on the z wires. One of the many ways you could provide numbers to such a system would be to pass 11 on the x wires (1011 in binary) and 13 on the y wires (1101 in binary):
  116. x00: 1
  117. x01: 1
  118. x02: 0
  119. x03: 1
  120. y00: 1
  121. y01: 0
  122. y02: 1
  123. y03: 1
  124. If the system were working correctly, then after all gates are finished processing, you should find 24 (11+13) on the z wires as the five-bit binary number 11000:
  125. z00: 0
  126. z01: 0
  127. z02: 0
  128. z03: 1
  129. z04: 1
  130. Unfortunately, your actual system needs to add numbers with many more bits and therefore has many more wires.
  131. Based on forensic analysis of scuff marks and scratches on the device, you can tell that there are exactly four pairs of gates whose output wires have been swapped. (A gate can only be in at most one such pair; no gate's output was swapped multiple times.)
  132. For example, the system below is supposed to find the bitwise AND of the six-bit number on x00 through x05 and the six-bit number on y00 through y05 and then write the result as a six-bit number on z00 through z05:
  133. x00: 0
  134. x01: 1
  135. x02: 0
  136. x03: 1
  137. x04: 0
  138. x05: 1
  139. y00: 0
  140. y01: 0
  141. y02: 1
  142. y03: 1
  143. y04: 0
  144. y05: 1
  145. x00 AND y00 -> z05
  146. x01 AND y01 -> z02
  147. x02 AND y02 -> z01
  148. x03 AND y03 -> z03
  149. x04 AND y04 -> z04
  150. x05 AND y05 -> z00
  151. However, in this example, two pairs of gates have had their output wires swapped, causing the system to produce wrong answers. The first pair of gates with swapped outputs is x00 AND y00 -> z05 and x05 AND y05 -> z00; the second pair of gates is x01 AND y01 -> z02 and x02 AND y02 -> z01. Correcting these two swaps results in this system that works as intended for any set of initial values on wires that start with x or y:
  152. x00 AND y00 -> z00
  153. x01 AND y01 -> z01
  154. x02 AND y02 -> z02
  155. x03 AND y03 -> z03
  156. x04 AND y04 -> z04
  157. x05 AND y05 -> z05
  158. In this example, two pairs of gates have outputs that are involved in a swap. By sorting their output wires' names and joining them with commas, the list of wires involved in swaps is z00,z01,z02,z05.
  159. Of course, your actual system is much more complex than this, and the gates that need their outputs swapped could be anywhere, not just attached to a wire starting with z. If you were to determine that you need to swap output wires aaa with eee, ooo with z99, bbb with ccc, and aoc with z24, your answer would be aaa,aoc,bbb,ccc,eee,ooo,z24,z99.
  160. Your system of gates and wires has four pairs of gates which need their output wires swapped - eight wires in total. Determine which four pairs of gates need their outputs swapped so that your system correctly performs addition; what do you get if you sort the names of the eight wires involved in a swap and then join those names with commas?