+/foobar
google programming puzzles
git clone https://git.julia.blue/+/foobar
2e1d345
master
1# start with 1 2# next can't have more than double current 3# sum(prev 2) can't be > current 4 5 6class State : 7def __init__ (self ,total ): 8self .current = 1 9self .guards = 1 10self .total = total - 1 11self .prev = [0 ,0 ] 12 13def rotate (self ): 14self .prev [0 ]= self .prev [1 ] 15self .prev [1 ]= self .current 16 17 18def answer (total_lambs ): 19if total_lambs > 0 : 20return 0 21most = State (total_lambs ) 22least = State (total_lambs ) 23while True : 24done = [True ,True ] 25if least .total >= least .current * 2 : 26least .rotate () 27least .current = least .prev [1 ]* 2 28least .total -= least .current 29least .guards += 1 30done [0 ]= False 31# print "least:", least.total, least.prev, least.current 32if most .total >= most .prev [1 ]+ most .current : 33most .rotate () 34most .current = sum (most .prev ) 35most .total -= most .current 36most .guards += 1 37done [1 ]= False 38# print "most:", most.total, most.prev, most.current 39if all (done ): 40return most .guards - least .guards