fork download
  1. # START = 0, S1-5 = 1-5, EXIT = 6, BOOM = 7
  2. rules = { '0w': 1, '0r': 2, '1o': 3, '1w': 2, '2r': 0, '2b': 3, '3b': 3, '3o': 4, '3g': 5, '4g': 6, '5o': 6 }
  3.  
  4. def defuse(order):
  5. wires = list(order)
  6. i = 0
  7. state = 0
  8. try:
  9. while i < len(wires):
  10. state = rules[str(state) + wires[i]]
  11. i += 1
  12. except KeyError:
  13. return 7
  14. return state
  15.  
  16. def isboomornot(input, state):
  17. if state == 7:
  18. print(str.format("the sequence {0} caused an explosion ({1})", input, state))
  19. elif state == 6:
  20. print(str.format("the sequence {0} disarmed the bomb ({1})", input, state))
  21. else:
  22. print(str.format("the sequence {0} is not finished yet ({1})", input, state))
  23.  
  24. input1 = "wwrwobbgo"
  25. input2 = "wwgog"
  26. input3 = "wwrrrwwbgo"
  27. input4 = "wbbbbgo"
  28. input5 = "bgg"
  29. input6 = "rrwobg"
  30.  
  31. isboomornot(input1, defuse(input1))
  32. isboomornot(input2, defuse(input2))
  33. isboomornot(input3, defuse(input3))
  34. isboomornot(input4, defuse(input4))
  35. isboomornot(input5, defuse(input5))
  36. isboomornot(input6, defuse(input6))
Success #stdin #stdout 0s 9024KB
stdin
Standard input is empty
stdout
the sequence wwrwobbgo disarmed the bomb (6)
the sequence wwgog caused an explosion (7)
the sequence wwrrrwwbgo disarmed the bomb (6)
the sequence wbbbbgo caused an explosion (7)
the sequence bgg caused an explosion (7)
the sequence rrwobg is not finished yet (5)