Starting with a binary list (just 1
s and 0
s) we are going to roll a ball over it. The ball has a position and a direction. At every step the ball does the following:
- If the number at it's position is
1
, then flip the direction. It "bounces". - Flip the number, if it's
0
replace it with1
, if it's1
replace it with0
. - Move 1 step in the direction.
We start execution with the ball at the first position heading to the right. When the ball is out of the range of the list either to the left or the right we stop execution.
For an example here is each step being run with [0,0,0,1,0]
to start.
[0,0,0,1,0]
^>
[1,0,0,1,0]
^>
[1,1,0,1,0]
^>
[1,1,1,1,0]
^>
[1,1,1,0,0]
<^
[1,1,0,0,0]
^>
[1,1,0,1,0]
^>
[1,1,0,1,1]
^>
Task
Given a starting list as input output the list after the ball has rolled over it.
This is code-golf the goal is to minimize the size of the source code as measured in bytes.
Test cases
[0,0,0,0,0] -> [1,1,1,1,1]
[0,0,0,1,0] -> [1,1,0,1,1]
[0,0,1,1,0] -> [1,0,0,1,1]
[0,1,0,1,0] -> [0,1,0,1,1]
[1,1,1,1,1] -> [0,1,1,1,1]
[1,0,1,0,0] -> [0,0,1,0,0]
[0,1,0,1] -> [0,1,0,1]