Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if 0: # Notice the repeated 18-line blocks; find the block differences:
- lines = puzzle.input.strip('\n').split('\n')
- for m in range(18):
- print(m)
- set_ = set(line for i, line in enumerate(lines) if i % 18 == m)
- print('\n'.join(set_))
- def test(): # See the 3 differing lines in each block.
- lines = puzzle.input.strip('\n').split('\n')
- params = {4: 'div z ', 5: 'add x ', 15: 'add y '}
- a, b, c = [[int(lines[i].split()[-1]) for i in range(m, 253, 18)]
- for m in params]
- print(f'a = {a}')
- print(list(zip(range(14), a, b, c)))
- test()
- # Block of 18 lines repeated 14 times:
- # inp w w = input
- # mul x 0
- # add x z
- # mod x 26 x = z % 26
- # div z 1 z /= a where a = 1 or 26
- # add x 13 x += b where b = -14, -13, -9, ..., 11, 12, 13, 15
- # eql x w
- # eql x 0 x = x != w
- # mul y 0
- # add y 25
- # mul y x
- # add y 1 y = x * 25 + 1
- # mul z y z *= y
- # mul y 0
- # add y w y = w
- # add y 14 y += c where c = 3, 4, 5, 6, 7, 8, 10, 13, 13, 13, 14, 16
- # mul y x y *= x
- # add z y z += y
- # Progressively simplify the code:
- # w = input (1..9)
- # x = w != ((z % 26) + b)
- # z = int(z / a) * (x * 25 + 1) + (w + c) * x
- # where a = 1 or 26 (%18 == 4)
- # where b = -14, -13, -9, ..., 11, 12, 13, 15 (%18==5)
- # where c = 3, 4, 5, 6, 7, 8, 10, 13, 13, 13, 14, 16 (%18==15)
- # w = input (1..9)
- # if w != ((z % 26) + b):
- # z = int(z / a) * 26 + w + c
- # else:
- # z = int(z / a)
- # w = input (1..9)
- # if w != digits[0] + b
- # if a == 26:
- # digits[0] = w + c
- # else: # a == 1
- # digits.push(w + c)
- # else:
- # if a == 26:
- # digits.pop()
- # w = input (1..9)
- # match = w == digits[-1] + b
- # if a == 26:
- # if match:
- # digits.pop()
- # else:
- # digits[-1] = w + c
- # else: # a == 1
- # if not match:
- # digits.append(w + c)
- # valid = all(digit == 0 for digit in digits)
- # valid = not digits # Actually, at least for this input.
- # match = w0 == (0) + 13 # False
- # append(w0 + 14)
- # [w0 + 14]
- # match = w1 == (w0 + 14) + 12 # False
- # append(w1 + 8)
- # [w0 + 14, w1 + 8]
- # match = w2 == (w1 + 8) + 11 # False
- # append(w2 + 5)
- # [w0 + 14, w1 + 8, w2 + 5]
- # match = w3 == (w2 + 5) + 0 # True if w3 == w2 + 5
- # pop() # (if not match, replace w1 + 8 by w3 + 4)
- # [w0 + 14, w1 + 8]
- # match = w4 == (w1 + 8) + 15 # False
- # append(w4 + 10)
- # [w0 + 14, w1 + 8, w4 + 10]
- # match = w5 == (w4 + 10) - 13 # True if w5 == w4 - 3
- # pop()
- # [w0 + 14, w1 + 8]
- # match = w6 == (w1 + 8) + 10 # False
- # append(w6 + 16)
- # [w0 + 14, w1 + 8, w6 + 16]
- # match = w7 == (w6 + 16) - 9 # True if w7 == w6 + 7
- # pop()
- # [w0 + 14, w1 + 8]
- # match = w8 == (w1 + 8) + 11 # False
- # append(w8 + 6)
- # [w0 + 14, w1 + 8, w8 + 6]
- # match = w9 == (w8 + 6) + 13 # False
- # append(w9 + 13)
- # [w0 + 14, w1 + 8, w8 + 6, w9 + 13]
- # match = w10 == (w9 + 13) - 14 # True if w10 == w9 - 1
- # pop()
- # [w0 + 14, w1 + 8, w8 + 6]
- # match = w11 == (w8 + 6) - 3 # True if w11 == w8 + 3
- # pop()
- # [w0 + 14, w1 + 8]
- # match = w12 == (w1 + 8) - 2 # True if w12 == w1 + 6
- # pop()
- # [w0 + 14]
- # match = w13 == (w0 + 14) - 14 # True if w13 == w0
- # pop()
- # []
- # pos: 01234567890123
- # max: 93499629698999
- # min: 11164118121471
- # Generalized solution:
- def process1(s, part2=False):
- lines = s.strip('\n').split('\n')
- a, b, c = [[int(lines[i].split()[-1]) for i in range(m, 253, 18)]
- for m in (4, 5, 15)]
- solution = [1 if part2 else 9] * 14
- stack = []
- for i in range(14):
- if a[i] == 1:
- stack.append((i, c[i]))
- else:
- j, old_c = stack.pop()
- diff = old_c + b[i]
- # The constraint is that solution[i] - solution[j] == diff.
- if part2:
- solution[i] = 1 + diff if diff >= 0 else 1
- solution[j] = 1 if diff >= 0 else 1 - diff
- else:
- solution[i] = 9 if diff >= 0 else 9 + diff
- solution[j] = 9 - diff if diff >= 0 else 9
- return ''.join(map(str, solution))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement