Advertisement
hhoppe

Advent of code 2020 day 24 notes

Dec 26th, 2021
1,145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.95 KB | None | 0 0
  1. if 0:  # Notice the repeated 18-line blocks; find the block differences:
  2.   lines = puzzle.input.strip('\n').split('\n')
  3.   for m in range(18):
  4.     print(m)
  5.     set_ = set(line for i, line in enumerate(lines) if i % 18 == m)
  6.     print('\n'.join(set_))
  7.  
  8.  
  9. def test():  # See the 3 differing lines in each block.
  10.   lines = puzzle.input.strip('\n').split('\n')
  11.   params = {4: 'div z ', 5: 'add x ', 15: 'add y '}
  12.   a, b, c = [[int(lines[i].split()[-1]) for i in range(m, 253, 18)]
  13.              for m in params]
  14.   print(f'a = {a}')
  15.   print(list(zip(range(14), a, b, c)))
  16.  
  17. test()
  18.  
  19.  
  20. # Block of 18 lines repeated 14 times:
  21. # inp w     w = input
  22. # mul x 0
  23. # add x z
  24. # mod x 26  x = z % 26
  25. # div z 1   z /= a   where a = 1 or 26
  26. # add x 13  x += b   where b = -14, -13, -9, ..., 11, 12, 13, 15
  27. # eql x w
  28. # eql x 0   x = x != w
  29. # mul y 0
  30. # add y 25
  31. # mul y x
  32. # add y 1   y = x * 25 + 1
  33. # mul z y   z *= y
  34. # mul y 0
  35. # add y w   y = w
  36. # add y 14  y += c  where c = 3, 4, 5, 6, 7, 8, 10, 13, 13, 13, 14, 16
  37. # mul y x   y *= x
  38. # add z y   z += y
  39.  
  40. # Progressively simplify the code:
  41.  
  42. # w = input  (1..9)
  43. # x = w != ((z % 26) + b)
  44. # z = int(z / a) * (x * 25 + 1)  +  (w + c) * x
  45.  
  46. # where a = 1 or 26    (%18 == 4)
  47. # where b = -14, -13, -9, ..., 11, 12, 13, 15  (%18==5)
  48. # where c = 3, 4, 5, 6, 7, 8, 10, 13, 13, 13, 14, 16  (%18==15)
  49.  
  50.  
  51. # w = input  (1..9)
  52. # if w != ((z % 26) + b):
  53. #   z = int(z / a) * 26 + w + c
  54. # else:
  55. #   z = int(z / a)
  56.  
  57.  
  58. # w = input  (1..9)
  59. # if w != digits[0] + b
  60. #   if a == 26:
  61. #     digits[0] = w + c
  62. #   else:  # a == 1
  63. #     digits.push(w + c)
  64. # else:
  65. #   if a == 26:
  66. #     digits.pop()
  67.  
  68.  
  69. # w = input  (1..9)
  70. # match = w == digits[-1] + b
  71. # if a == 26:
  72. #   if match:
  73. #     digits.pop()
  74. #   else:
  75. #     digits[-1] = w + c
  76. # else:  # a == 1
  77. #   if not match:
  78. #     digits.append(w + c)
  79.  
  80. # valid = all(digit == 0 for digit in digits)
  81. # valid = not digits  # Actually, at least for this input.
  82.  
  83.  
  84. # match = w0 == (0) + 13  # False
  85. # append(w0 + 14)
  86. # [w0 + 14]
  87.  
  88. # match = w1 == (w0 + 14) + 12  # False
  89. # append(w1 + 8)
  90. # [w0 + 14, w1 + 8]
  91.  
  92. # match = w2 == (w1 + 8) + 11  # False
  93. # append(w2 + 5)
  94. # [w0 + 14, w1 + 8, w2 + 5]
  95.  
  96. # match = w3 == (w2 + 5) + 0  # True   if w3 == w2 + 5
  97. # pop()    # (if not match, replace w1 + 8 by w3 + 4)
  98. # [w0 + 14, w1 + 8]
  99.  
  100. # match = w4 == (w1 + 8) + 15  # False
  101. # append(w4 + 10)
  102. # [w0 + 14, w1 + 8, w4 + 10]
  103.  
  104. # match = w5 == (w4 + 10) - 13  # True if w5 == w4 - 3
  105. # pop()
  106. # [w0 + 14, w1 + 8]
  107.  
  108. # match = w6 == (w1 + 8) + 10  # False
  109. # append(w6 + 16)
  110. # [w0 + 14, w1 + 8, w6 + 16]
  111.  
  112. # match = w7 == (w6 + 16) - 9  # True if w7 == w6 + 7
  113. # pop()
  114. # [w0 + 14, w1 + 8]
  115.  
  116. # match = w8 == (w1 + 8) + 11  # False
  117. # append(w8 + 6)
  118. # [w0 + 14, w1 + 8, w8 + 6]
  119.  
  120. # match = w9 == (w8 + 6) + 13  # False
  121. # append(w9 + 13)
  122. # [w0 + 14, w1 + 8, w8 + 6, w9 + 13]
  123.  
  124. # match = w10 == (w9 + 13) - 14  # True if w10 == w9 - 1
  125. # pop()
  126. # [w0 + 14, w1 + 8, w8 + 6]
  127.  
  128. # match = w11 == (w8 + 6) - 3  # True if w11 == w8 + 3
  129. # pop()
  130. # [w0 + 14, w1 + 8]
  131.  
  132. # match = w12 == (w1 + 8) - 2  # True if w12 == w1 + 6
  133. # pop()
  134. # [w0 + 14]
  135.  
  136. # match = w13 == (w0 + 14) - 14  # True if w13 == w0
  137. # pop()
  138. # []
  139.  
  140. # pos: 01234567890123
  141. # max: 93499629698999
  142. # min: 11164118121471
  143.  
  144.  
  145. # Generalized solution:
  146. def process1(s, part2=False):
  147.   lines = s.strip('\n').split('\n')
  148.   a, b, c = [[int(lines[i].split()[-1]) for i in range(m, 253, 18)]
  149.              for m in (4, 5, 15)]
  150.   solution = [1 if part2 else 9] * 14
  151.   stack = []
  152.   for i in range(14):
  153.     if a[i] == 1:
  154.       stack.append((i, c[i]))
  155.     else:
  156.       j, old_c = stack.pop()
  157.       diff = old_c + b[i]
  158.       # The constraint is that solution[i] - solution[j] == diff.
  159.       if part2:
  160.         solution[i] = 1 + diff if diff >= 0 else 1
  161.         solution[j] = 1 if diff >= 0 else 1 - diff
  162.       else:
  163.         solution[i] = 9 if diff >= 0 else 9 + diff
  164.         solution[j] = 9 - diff if diff >= 0 else 9
  165.  
  166.   return ''.join(map(str, solution))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement