You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 line
1019B

  1. def valid_z1s(z2, w, c1, c2, c3):
  2. valid = {(z2 - w - c3) // 26} | {(z2 * 26 + i) for i in range(26)}
  3. for z1 in valid:
  4. if evolve(z1, w, c1, c2, c3) == z2:
  5. yield z1
  6. def evolve(z, w, c1, c2, c3):
  7. return (z // c1) if (z % 26 + c2 == w) else (z * 26 + w + c3)
  8. consts = []
  9. for block in open(0).read().split('inp w\n')[1:]:
  10. els = [ln.split()[-1] for ln in block.splitlines()]
  11. consts.append((int(els[3]), int(els[4]), int(els[14])))
  12. valid_zs = {0: {0}}
  13. for c1, c2, c3 in consts[:7]:
  14. valid_zs[len(valid_zs)] = {
  15. evolve(z, w, c1, c2, c3)
  16. for z in valid_zs[len(valid_zs) - 1]
  17. for w in map(int, '123456789')
  18. }
  19. state = [('', 0)]
  20. idx = 14
  21. for c1, c2, c3 in consts[::-1]:
  22. idx -= 1
  23. state = [
  24. (w + s, z1)
  25. for s, z2 in state
  26. for w in '123456789'
  27. for z1 in valid_z1s(z2, int(w), c1, c2, c3)
  28. if idx not in valid_zs or z1 in valid_zs[idx]
  29. ]
  30. (head, _), *body, (tail, _) = sorted(state)
  31. print(tail)
  32. print(head)