-
Notifications
You must be signed in to change notification settings - Fork 0
/
04_boom-baby.py
61 lines (53 loc) · 2.93 KB
/
04_boom-baby.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Bomb, Baby!
===========
You're so close to destroying the LAMBCHOP doomsday device you can taste it!
But in order to do so, you need to deploy special self-replicating bombs designed
for you by the brightest scientists on Bunny Planet. There are two types: Mach
bombs (M) and Facula bombs (F). The bombs, once released into the LAMBCHOP's
inner workings, will automatically deploy to all the strategic points you've
identified and destroy them at the same time.
But there's a few catches. First, the bombs self-replicate via one of two
distinct processes:
Every Mach bomb retrieves a sync unit from a Facula bomb; for every Mach bomb, a
Facula bomb is created;
Every Facula bomb spontaneously creates a Mach bomb.
For example, if you had 3 Mach bombs and 2 Facula bombs, they could either
produce 3 Mach bombs and 5 Facula bombs, or 5 Mach bombs and 2 Facula bombs. The
replication process can be changed each cycle.
Second, you need to ensure that you have exactly the right number of Mach and
Facula bombs to destroy the LAMBCHOP device. Too few, and the device might
survive. Too many, and you might overload the mass capacitors and create a
singularity at the heart of the space station - not good!
And finally, you were only able to smuggle one of each type of bomb - one Mach,
one Facula - aboard the ship when you arrived, so that's all you have to
start with. (Thus it may be impossible to deploy the bombs to destroy the
LAMBCHOP, but that's not going to stop you from trying!)
You need to know how many replication cycles (generations) it will take to
generate the correct amount of bombs to destroy the LAMBCHOP. Write a function
solution(M, F) where M and F are the number of Mach and Facula bombs needed.
Return the fewest number of generations (as a string) that need to pass before
you'll have the exact number of bombs necessary to destroy the LAMBCHOP, or
the string "impossible" if this can't be done! M and F will be
string representations of positive integers no larger than 10^50. For example, if
M = "2" and F = "1", one generation would need to pass, so
the solution would be "1". However, if M = "2" and F =
"4", it would not be possible.
"""
def solution(m, f):
def get_generations(x, y):
# since x and y are functionally identical, keeping their values sorted
# eliminates duplicate branches
# assert x >= y
if x == y == 1: return 0
if y == 1: return x - 1
if y < 1: return None
generations, remainder = divmod(x, y)
if remainder == 0: return None
nested_gens = get_generations(y, remainder)
if nested_gens is None: return None
else: return generations + nested_gens
m, f = int(m), int(f)
generations = get_generations(max(m, f), min(m, f))
if generations is None: return 'impossible'
return str(generations)