Starting with a binary list (just
0s) 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
0replace it with
1, if it's
1replace it with
- 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] ^>
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.
[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]