When will I have a binary car?
Javascript, 68 63 61 60 52 bytes
5 bytes off thanks @ETHproductions. 2 3 11!! bytes off thanks @NotthatCharles.
f=(i,a,m=0)=>/[^01]/.test(i+=a[m++%10])?f(i,a,m):m/2
Test here.
MATL, 29 26 25 bytes
`tvyyYs+V50<!A~]xx1Mf1)2/
Input format is
[27; 27; 27; 27; 27; 27; 27; 27; 27; 27]
101101
EDIT (June 10, 2016): The following link replaces v
by &v
(26 bytes) to adapt to changes in the language
Try it online!
` ] % do---while loop. Exit loop when top of stack is falsy
t % duplicate. Takes first input (array) first time
v % concat vertically (doubles length of array)
yy % copy top two. Takes second input (todasy's value) first time
Ys % cumulative sum of (repeated) miles each way
+ % add to today's value
V % convert each number to a string
50<!A % true for strings than only contain '01 ' (ASCII less than 50)
~ % negate. This is the loop condition
xx % delete stack contents
1M % push again array that tells which strings only contain '01 '
f1) % pick index of first true entry
2/ % divide by 2
Lua, 108 Bytes
First time using the repeat..until loop in a codegolf!
function f(o,t)i=0repeat i,o=i+1,(o+t[i%#t+1]).."."o=o:sub(1,o:find("%.")-1)until tonumber(o,2)print(i/2)end
Ungolfed
function f(o,t) -- o can either be a number or a string, t is a table
-- call it like f(2,{27,14,5...})
i=0 -- initialise the travel count
repeat -- proceed like a do..while in C
i,o=i+1,(o+t[i%#t+1]).."."-- increment i, and add t[i] to the odometer
o=o:sub(1,o:find("%.")-1) -- remove the decimal part of the odometer
until tonumber(o,2) -- tries to convert o from binary to decimal
-- return nil (and repeat the loop) if it can't
print(i/2) -- print i/2 which is the number of days for i travels
end
After the first loop, o
will have a decimal part because of tonumber
, I had to remove it... And to add it for the first case, that's why I concatenate it with a "."
.