blob: 1fad6201afb5b63656721b01746f91d72bc6df94 (
plain) (
tree)
|
|
#!/usr/bin/env python
with open('day24.txt') as data:
program = [tuple(line.strip().split()) for line in data]
params = []
for i in range(0, 18 * 14, 18):
a = int(program[i+4][-1])
b = int(program[i+5][-1])
c = int(program[i+15][-1])
params.append((a, b, c))
def f(params, z, w):
a, b, c = params
if z % 26 + b != w:
z = z//a*26 + w + c
else:
z = z//a
return z
zs = {0: [0, 0]}
for i, p in enumerate(params):
new_zs = {}
for z, inp in zs.items():
for w in range(1, 10):
new_z = f(p, z, w)
if p[0] == 1 or (p[0] == 26 and new_z < z):
if new_z not in new_zs:
new_zs[new_z] = [inp[0]*10+w, inp[1]*10+w]
else:
new_zs[new_z][0] = min(new_zs[new_z][0], inp[0]*10+w)
new_zs[new_z][1] = max(new_zs[new_z][1], inp[1]*10+w)
print(f"Digit: {i+1} tracked values of z: {len(new_zs)}")
zs = new_zs
print(f'best valid values: {zs[0]}')
|