Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import functools
- @functools.lru_cache(maxsize=None)
- def solve(floors, eggs):
- if floors == 0:
- return 0, 0
- elif eggs == 1:
- return floors, 1
- else:
- acc = 999999
- drop = -1
- for i in range(floors):
- x, _ = solve(i, eggs - 1)
- y, _ = solve(floors - i - 1, eggs)
- a = max(x, y) + 1
- if a < acc:
- acc = a
- drop = i + 1
- return acc, drop
- if __name__ == "__main__":
- start = 1
- end = 100
- eggs = 2
- print(
- "I have %d eggs, %d floors, floor 1 is definitely safe, floor 100 is definitely deadly"
- % (eggs, end)
- )
- sanity_check = 999
- while end - start > 1:
- if eggs <= 0:
- raise RuntimeError("Sorry, I lied :(")
- moves, floor = solve(end - start - 1, eggs)
- if moves >= sanity_check:
- raise RuntimeError("Sorry, I lied :(")
- sanity_check = moves
- drop = floor + start
- print("I have %d eggs and will answer in %d moves.\nI drop from the floor %d" % (eggs, moves, drop))
- while True:
- r = input("result (1 - ok, 0 - breaks)> ")
- if r == "0":
- end = drop
- eggs = eggs - 1
- elif r == "1":
- start = drop
- else:
- print("You liar!!!!")
- continue
- break
- print("It survives %d-th floor, and breaks at %d-th floor" % (start, end))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement