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.

31 lines
819B

  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 z1 < 26 ** exp:
  5. if evolve(z1, w, c1, c2, c3) == z2:
  6. yield z1
  7. def evolve(z, w, c1, c2, c3):
  8. return (z // c1) if (z % 26 + c2 == w) else (z * 26 + w + c3)
  9. consts = [
  10. (int(els[4]), int(els[5]), int(els[15]))
  11. for block in open(0).read().split('inp w')[1:]
  12. for els in [[ln.split()[-1] for ln in block.splitlines()]]
  13. ]
  14. state = [('', 0)]
  15. for exp, (c1, c2, c3) in list(enumerate(consts, 1))[::-1]:
  16. print(exp, len(state))
  17. state = [
  18. (w + s, z1)
  19. for s, z2 in state
  20. for w in '123456789'
  21. for z1 in valid_z1s(z2, int(w), c1, c2, c3)
  22. ]
  23. (head, _), *body, (tail, _) = sorted(state)
  24. print(tail)
  25. print(head)