Advertisement
tinyevil

Untitled

Jun 30th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. import functools
  2.  
  3.  
  4. @functools.lru_cache(maxsize=None)
  5. def solve(floors, eggs):
  6.     if floors == 0:
  7.         return 0, 0
  8.     elif eggs == 1:
  9.         return floors, 1
  10.     else:
  11.         acc = 999999
  12.         drop = -1
  13.         for i in range(floors):
  14.             x, _ = solve(i, eggs - 1)
  15.             y, _ = solve(floors - i - 1, eggs)
  16.             a = max(x, y) + 1
  17.             if a < acc:
  18.                 acc = a
  19.                 drop = i + 1
  20.         return acc, drop
  21.  
  22.  
  23. if __name__ == "__main__":
  24.     start = 1
  25.     end = 100
  26.     eggs = 2
  27.  
  28.     print(
  29.         "I have %d eggs, %d floors, floor 1 is definitely safe, floor 100 is definitely deadly"
  30.         % (eggs, end)
  31.     )
  32.  
  33.     sanity_check = 999
  34.  
  35.     while end - start > 1:
  36.         if eggs <= 0:
  37.             raise RuntimeError("Sorry, I lied :(")
  38.         moves, floor = solve(end - start - 1, eggs)
  39.         if moves >= sanity_check:
  40.             raise RuntimeError("Sorry, I lied :(")
  41.         sanity_check = moves
  42.         drop = floor + start
  43.         print("I have %d eggs and will answer in %d moves.\nI drop from the floor %d" % (eggs, moves, drop))
  44.         while True:
  45.             r = input("result (1 - ok, 0 - breaks)> ")
  46.             if r == "0":
  47.                 end = drop
  48.                 eggs = eggs - 1
  49.             elif r == "1":
  50.                 start = drop
  51.             else:
  52.                 print("You liar!!!!")
  53.                 continue
  54.             break
  55.     print("It survives %d-th floor, and breaks at %d-th floor" % (start, end))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement