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.

4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import re
  2. import sys
  3. def freeze(state):
  4. return tuple(frozenset(fl) for fl in state)
  5. def thaw(state):
  6. return [set(fl) for fl in state]
  7. def checks(state):
  8. for fl in state:
  9. dangerous = {d[:-1] for d in fl if d.endswith('+')}
  10. vulnerable = {d[:-1] for d in fl if d.endswith('-')} - dangerous
  11. if dangerous and vulnerable:
  12. yield False
  13. def advance(pair):
  14. f0, state = pair
  15. for devices in {frozenset({a, b}) for a in state[f0] for b in state[f0]}:
  16. for f1 in {f0 + 1, f0 - 1} - {-1, 4}:
  17. if len(devices) == 1 or f1 > f0:
  18. new = thaw(state)
  19. new[f0] -= devices
  20. new[f1] |= devices
  21. if all(checks(new)):
  22. yield f1, freeze(new)
  23. text = sys.stdin.read()
  24. state = [
  25. {a + (b or '+') for a, b in re.findall(r'(\w{2})\w+ium(-)?', ln)}
  26. for ln in text.splitlines()
  27. ]
  28. goal = (3, freeze([{}, {}, {}, {d for fl in state for d in fl}]))
  29. edge = {(0, freeze(state))}
  30. seen = set()
  31. ans1 = 0
  32. while goal not in seen:
  33. edge = {new for old in edge for new in advance(old)} - seen
  34. seen |= edge
  35. ans1 += 1
  36. print(ans1)
  37. print(ans1 + 12 * 2)