Created
July 27, 2017 09:24
-
-
Save evertlammerts/dd7a7099dc42de119fc7ec3a279a49c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reproduce(m, f, target_m, target_f): | |
stack = [] | |
generation = 0 | |
while True: | |
generation += 1 | |
m1, f1 = m+f, f | |
m, f = m, f+m | |
success = m == target_m and f == target_f | |
success = success or (m1 == target_m and f1 == target_f) | |
if success: | |
return generation | |
impossible = m > target_m or f > target_f | |
impossible = impossible or (m1 > target_m or f1 > target_f) | |
if impossible: | |
if len(stack): | |
m, f, generation = stack.pop() | |
continue | |
return False | |
stack.append((m1, f1, generation)) | |
def answer(target_m, target_f): | |
target_m, target_f = int(target_m), int(target_f) | |
m = f = 1 | |
answer = reproduce(m, f, target_m, target_f) | |
return (answer and '%d' % answer) or 'impossible' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment