+/foobar

google programming puzzles

git clone https://git.julia.blue/+/foobar

Joseph RyanI made it2e1d345

master
763 B39 linesraw
1def fib():
2    prev = 0
3    current = 1
4    while True:
5        yield current
6        temp = prev + current
7        prev = current
8        current = temp
9
10
11def powersof2():
12    current = 1
13    while True:
14        yield current
15        current *= 2
16
17
18def countGuards(tot, func):
19    guards = 0
20    for i in func():
21        tot -= i
22        if tot < 0:
23            if func == powersof2:
24                tot += i
25                if tot >= i / 2 + i / 4:
26                    guards += 1
27            break
28        guards += 1
29    return guards
30
31
32def answer(total_lambs):
33    return countGuards(total_lambs, fib) - countGuards(total_lambs, powersof2)
34
35
36import sys
37print answer(int(sys.argv[1]))
38
39# print "".join(["._-^@|X"[answer(i)] for i in range(int(sys.argv[1]))])