Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save evertlammerts/dd7a7099dc42de119fc7ec3a279a49c1 to your computer and use it in GitHub Desktop.
Save evertlammerts/dd7a7099dc42de119fc7ec3a279a49c1 to your computer and use it in GitHub Desktop.
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