[Question]
  [
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 with `1`, if it's `1` replace it with `0`.
* 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](/questions/tagged/code-golf "show questions tagged '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]
```
  
[Answer]
# [tinylisp](https://github.com/dloscutoff/Esolangs/tree/master/tinylisp), ~~130~~ ~~118~~ 98 bytes
```
(load library
(d B(q((L R)(i R(i(h R)(reverse(B(c 0(t R))L))(B(c 1 L)(t R)))(reverse L
(q((L)(B()L
```
[Try it online!](https://tio.run/##XYwxDoMwEAR7XrHlXoe/QEF1FT8gwRInWZAYFInXOxgQiqNtbvZGu9q0BVteKTHM/YBgj9jHreKAhm9S0QkNHY1jPqP/@Lh4Nnyi5rpXoiIHOqicza1B81B7DGVJNLHFjjWuVL/sCnYFu@LvcOXm0/jz0xc "tinylisp – Try It Online")
-12 bytes thanks to Razetime. See the edit history for a `library`-less version.
-20(!) bytes thanks to DLosc. Maybe I'm not very good at golfing in tinylisp...
`B` does the rolling.
```
(def roll_ball
  (lambda (Left Right Dir)				; takes left, right direction
    (if (equal? Dir 1)					; if direction is 1,
      (reverse (roll_ball Right Left 0))		; reverse the result of roll_ball with left and right lists flipped			
      (if (nil? Right)					; if we're at the end of the list,
        (reverse Left)					; reverse the left list and return
        (if (equal? (head Right) 1)			; otherwise, if the head is 1
          (roll_ball Left (cons 0 (tail Right)) 1)	; recurse, flipping the direction and changing the first element of Right to 0
          (roll_ball (cons 1 Left) (tail Right) 0)	; otherwise, recurse on L, and add 1 to the Left 
        )))))
```
# [tinylisp](https://github.com/dloscutoff/Esolangs/tree/master/tinylisp), ~~69~~ 62 bytes
```
(load library
(d F(q((L)(i(h L)(c 0(t L))(concat(map not(t L))(q(1
```
[Try it online!](https://tio.run/##XYo9DoAgFIN3TvHGuvEu4eQlEAZJkD9ZOD2SSIyYDu3Xtlhfnb1ia3BBGXJ2zypXAUMrErAtsDiomyaJ0kNPwWtVcKpIPpRRJnDDSt0lDYkv88Q8MU8709DLz@P3bzc "tinylisp – Try It Online")
Port of [tsh's answer](https://codegolf.stackexchange.com/a/242700/67312).
[Answer]
# x86-64 machine code, 19 bytes
```
6A 01 58 99 80 34 17 01 75 02 F7 D8 01 C2 39 F2 72 F2 C3
```
[Try it online!](https://tio.run/##jVJLj9MwED57fsUQtLvONq0aWBBq6R5YcURCqz0santwbac1Sp1gO5AS5a9TJmlpKSdkKdbM95hHLMtyuJZyvxd@ixwfIw6jdV6sRI4ZZBNWVn6DKbCyKNGJGphU38BNWF04/PDl6SN@fnrEuVNm4FS9TDrqV/sTPTCr16hJ4SdMKIVa1UkfM7ktj5E3xF6hA@Z0gDjCeAq6DtpZjB4ibL4XRmHG5UY4vM0TNDagjactwEtjZV4pje99UKYYbe4BfBDBSPTBVTJg0D5I4TU2vWqKvYuYv1uS/g/o50ucYQPNm6QZJ8fTtslfifQykV4m0ktGmhzPOXHgHBh3J0nbQjuFrrOtMJbH0ADLaKVcVKHA64CT0wA@BkboAe4UhloeT@l6McMwosnMYEAcxkpHcMajK4NRQpCYmyVt9AwM76M@zjiBHcMewv92xivzj3MVur3ym4W96RItMDixF/aTkBtjNcpC6UlfuytTd2US3FGoCmzO5uNXz2S/m3FeWW/WVqv@r93GWTyvBwMq2eKPjck18l3X47h@eN2Znhw4jb7a0ebihSWnmsB2v/8ls1ys/X64fXtHH3rpM@Lr/Dc "C++ (gcc) – Try It Online")
Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes in RDI the address of an array of 8-bit integers and its length in RSI, and modifies it in place.
In assembly:
```
f:  push 1      # Push 1 onto the stack.
    pop rax     # Pop it into RAX.
    cdq         # Sign extend EAX into EDX, making EDX 0.
                    # EDX will hold the current index into the array
                    #  and EAX will hold the current direction (±1).
r:  xor BYTE PTR [rdi+rdx], 1   # Flip the current number.
    jnz s           # Jump if the new value is not 0.
    neg eax         #  Otherwise, the old value was 1; invert the direction.
s:  add edx, eax    # Advance in the current direction.
    cmp edx, esi    # Compare the current index with the length.
    jb r            # Jump back if the index is lesser, as unsigned integers;
                    #  this will stop if it goes off either end,
                    #  equaling the length or overflowing to all 1s.
    ret             # Return.
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 36 bytes
```
i=>i.map((n,j)=>i[0]?j>0&n:i[j+1]^1)
```
[Try it online!](https://tio.run/##dY5BCoMwEEX3PUVWJaGpzmwF7UHCFIJVSbCJaOn100IVDXT4qxkej@ft2y7t7KbXNcRHl/o6ubpxxdNOUgbt1fcwQDffwDlUzvgL0h1VamNY4tgVYxxkLw3odaSUKEthUK@j018UcxRYFHMUWBRzK7DWPe2AIoP@NAfr9mACSIg8gNIH "JavaScript (Node.js) – Try It Online")
I don't have any proof of its correctness. It just passed all testcases. So it works.
[Answer]
# [R](https://www.r-project.org/), ~~69~~ ~~68~~ 66 bytes
Or **[R](https://www.r-project.org/)>=4.1, 59 bytes** by replacing the word `function` with a `\`.
*Edit: -1 byte thanks to [@Giuseppe](https://codegolf.stackexchange.com/users/67312/giuseppe).*
```
function(v,a=0){while((F=F+(T=(-T)^a))&&!is.na(a<-v[F]))v[F]=!a;v}
```
[Try it online!](https://tio.run/##K/qfZvs/rTQvuSQzP0@jTCfR1kCzujwjMydVQ8PN1k1bI8RWQzdEMy5RU1NNTTGzWC8vUSPRRrcs2i1WUxNE2iomWpfV/k/TSNYw0IFCTU1lBV07hWhDHSiM5ULIG6LKG6DKG6LKG6DKG6LqN0DVD7cOWd4QWR6iAUk/VADFfDTTY/8DAA "R – Try It Online")
The `T=(-T)^a` trick abuses the fact that going left may only last for one step.
---
# [R](https://www.r-project.org/), 41 bytes
Or **[R](https://www.r-project.org/)>=4.1, 34 bytes** by replacing the word `function` with a `\`.
```
function(v)"if"(v,c(0,v[-1]),c(!v[-1],1))
```
[Try it online!](https://tio.run/##K/qfZvs/rTQvuSQzP0@jTFMpM01Jo0wnWcNApyxa1zBWE8hUBLN0DDU1/6dpgGSgUFNTWUHXTiHaUAcKY7kQ8oao8gao8oao8gao8oao@g1Q9cOtQ5Y3RJaHaEDSDxVAMR/N9Nj/AA "R – Try It Online")
Port of [@tsh'sanswer](https://codegolf.stackexchange.com/a/242700/55372) (with explanation by [@Jonah](https://codegolf.stackexchange.com/a/242708/55372)).
[Answer]
# [J](http://jsoftware.com/), 18 bytes
```
{.{(1-0,~}.),:0,}.
```
[Try it online!](https://tio.run/##y/qvpKeepmBrpaCuoKNgoGAFxLp6Cs5BPm7/q/WqNQx1DXTqavU0dawMdGr1/mtypSZn5CsYwqGCrpVCGlAPFCKkDTCkDRHSBhjShghpAwzdhsi6DdDshrsFJm0AtQAmDeWjGo5i9H8A "J – Try It Online")
Based on [tsh's JS answer](https://codegolf.stackexchange.com/a/242700/15469).
## how
* `{.{...,:...` If the input begins with 1...
* `0,}.` Change the 1 to a 0 (we're just bouncing off the first element and halting)
* `1-0,~}.` Otherwise, remove the first element, append a 0, and then, for each element, return the opposite of its right neighbor.
## why does this work?
This is an induction argument suggested by alephalpha in the comments of tsh's answer.
* Consider the 1 element case. Clearly, a 1 becomes 0 and a 0 becomes 1. Also note that in the 0 case we're traveling right when we halt, and in the 1 case we're traveling left. Let's go ahead and add "begins with 0 exits right" to our induction hypothesis.
* Assuming the hypothesis, consider the two cases:
	+ `1 [n element input]` Clearly true as we bounce off first element.
	+ `0 [n element input]` We now consider the two cases for the `n element input`:
	
	
	
	```
	Begins with 1:
	>
	0 1 ...
	  >
	1 1 ...
	< 
	1 0 ...
	  >
	0 0 ... By induction, this will exit right,
	        so the initial element is proven 0, as needed.
	
	Begins with 0:
	>
	0 0 ...
	  >
	1 0 ... By induction, this will exit right,
	        so the initial element is proven 1, as needed.
	
	```
[Answer]
# [Python 3](https://docs.python.org/3/), 57 bytes
*-13 bytes thanks to `Wheat Wizard`, -5 thanks to `ophact`, -2 thanks to `loopy walt` and -1 thanks to `pxeger`*
```
def f(x):
 i=c=0
 while-~i*x[i:]:c^=x[i];x[i]^=1;i+=1-2*c
```
[Try it online!](https://tio.run/##TY7BCsMgEETv@Yq9RVOF2N4U@yNiLkbJgpgQAk0v/XVrW5HuwPKYmcNsz2NZ0y3n2QcI5KSyA9ROjx08Foyev3A4DUor3aQLWPV5kxYKL1rw6@ByWHeIgAmMGVmVZZVFY9FYNF@wqi//kr@OtWVNuW3HdJDIwKdZ98Dv0FMVSKSqJjS/AQ "Python 3 – Try It Online")
[Answer]
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), 90 bytes
```
(([]){(){}((()[{}])<{{}(<>)}{}>)<>([])}()){({}({})([{}()]))}{}{{}<>([]){{}({}<>)<>([])}}<>
```
[Try it online!](https://tio.run/##PcqxCoAwDATQX@l4Nwh1D/2R0qEOglgcXEO@PaYKkuVy97a7H9eyj366A7VRQTUArGqNovFIoakVSpnAwEBRqxGBwMa5h/yAvpv8PqJ7TvPWlB8 "Brain-Flak – Try It Online")
36 bytes to simulate the ball rolling, and 54 to ensure that the input is in the right direction.
[Answer]
# [Python 3](https://docs.python.org/3/), 84 bytes
Not a great score, but somewhat interesting.
This takes a list of bits encoded as an integer and a length argument and outputs a new list of bits encoded as an integer. It’s a literal implementation, not induction based.
Improvements are welcome, I haven’t gotten any of my attempts to make it smaller working. Maybe c would be a better language to port this answer to.
Thanks to pxeger for -5!
```
def f(x,l,b=1,d=1):
 a=1<<l;c=a>>b;d*=x&c<1or-1
 return f(x^c,l,b+d,d)if c&a-1else x
```
[Try it online!](https://tio.run/##JY3LCoMwEEX3fsWsamJHaCp2UY0/UlrQPGggjJJaSL8@VTOLw9w5XGb5re@ZmpS0sWBZRI@TFKil4PcCRin63ndKjsMwdbqS8aR6MYdaFBDM@g20d15qb501au4sqNNYC@M/BmKycwAPjuDBLthyZNeDt4MinxqRTU7txuf2eZuIJP2xLcHRyggnRyxyBENallAPUPIuu91YVnnOefoD "Python 3 – Try It Online")
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), 24 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
[ā<¾å_#D¾è©_¾ǝ®XαDUi¼ë.¼
```
[Try it online](https://tio.run/##yy9OTMpM/f8/@kijzaF9h5fGK7sAqRWHVsYf2nd87qF1Eec2uoRmHtpzeLXeoT1AZQY6IGioYxALAA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfaXS/@gjjTaH9h1eGq/sAqRWHFoZf2jf8bmH1kWc2@gSmnloz@HVeof2/FfSC9PRO7TVMPR/dLSBDhTG6ihAOYYIjiGCY4iQMdSBQggHIoesLDYWAA).
**Explanation:**
```
[           # Start an infinite loop:
 ā<         #  Push a list in the range [0,length) (without popping the list)
            #  (which will use the implicit input-list in the first iteration)
   ¾å_      #  If this list does not contain value `¾` (so we're out of bounds):
            #  (`¾` is 0 by default)
      #     #   Stop the infinite loop
 D          #  Duplicate the current list
  ¾è        #  Get the value at index `¾`
    ©       #  Store it in variable `®` (without popping)
     _      #  Invert the bit (with an ==0 check)
      ¾ǝ    #  Insert it back into the list at the same position `¾`
  ®Xα       #  Get the absolute difference between `®` and `X`
            #  (`X` is 1 by default)
     DU     #  Store this as new value `X`
       i    #  If this absolute difference `X` is 1:
        ¼   #   Increment `¾` by 1
       ë    #  Else:
        .¼  #   Decrement `¾` by 1 instead
            # (after which the list is output implicitly as result)
```
[Answer]
# [JavaScript (Node.js)](https://nodejs.org), 55 bytes
```
f=(n,d=1,a=0,C=n[a])=>C<2?f((n[a]^=1,n),d=C?-d:d,a+d):n
```
[Try it online!](https://tio.run/##VY8/b8MgEMV3PsVtBoVY0DEuyeCtQzJ0dF0FGYioLLBsEqWq@tld/EfI1S0/3r3j7n3Jhxya3nZh77zS42gEdlQJTqVgtBSukjURx/L15WQwnl6fsedItJSnvTooKneKHNzYeDcEsK67BxBwrRhdq0Yr88Q8MU86p2vNvHRYfc2HrrUBZx8uIwValuhnp5ug1eUelm3VZprOzBKzxGyjs42fpW11gVCrY4r4JyuQ8T3glAu8WeIR@EEwyb7VeetvOFsPif6MTh4KmR0iGqwfssVRISRqIJ1Kx4Nfhmbj2/vlnA@ht@5mzTf@H7CyNYnZ41U7ARz9jn8 "JavaScript (Node.js) – Try It Online")
[Answer]
# [C (gcc)](https://gcc.gnu.org/), ~~59~~ 58 bytes
```
f(a,l)int*a;{for(int i=0,d=1;i<l+0u;)i+=d*=(a[i]^=1)*2-1;}
```
[Try it online!](https://tio.run/##lVDRboMgFH2Wr2BdmkKlTpYtWULpjziXENT1Nk4XwZcZvt2Bbe3q28LDhXPPPfdw9O5T63GsiGI1hcZulRiqtiP@ikGmrJBcwL6O015QiGWxlURlkH9ITrfPOy7c@AiNrvuixHtjC2iT4wEhY5UFjY3tem2xLY3VypR4CKqNwKGo7C0XDl17JsuxxAMaXtmQsstxjv0B@D3A7wF@z@Dscm7AmXNmvMwjziEnUHD0paAhFA0o8gHgKYGT95QKXx4kNvBTthWZDdOnJZKlOfXkOKYo8irR8vvWq93Ip1x4zrwKzqsgrLJJCGnSiaLvzrcrsloDXjHfCvHTMHpt7A6r6e2dJCowGvo/ZbyGhXJv9VF1ZPPebALgIxp/AQ "C (gcc) – Try It Online")
-1 thanks to l4m2, making the numbers unsigned for the bounds check.
[Answer]
# JavaScript (ES6), 48 bytes
```
f=(a,d,i=0)=>1/a[i]?f(a,d^=!(a[i]^=1),i+1-2*d):a
```
[Try it online!](https://tio.run/##dY7BCsIwEETvfkW8ZXVrsh6F6IeELYSmkUhpxIq/HylWbMAwpxkej7m5l5u6R7w/mzH5PudgpEOP0WgwZ1LORr6EeWrNVs6tNQQY99Qcdx5OLndpnNLQH4Z0lUFajUsYQCglLOES3vxFqUR1FaUS1VWUSquuWn/XVihV0I9mZf0OlQMsRHmA8xs "JavaScript (Node.js) – Try It Online")
[Answer]
# [Wolfram Language (Mathematica)](https://www.wolfram.com/wolframscript/), 33 bytes
```
If[#>0,{0,##2},1-{##2,0},{}]&@@#&
```
[Try it online!](https://tio.run/##TYsxC4AgEIX/ysGB00Vee@La1i4OEkUNNYSb3G83QcF4w@Pje@8O8dzvEK8t5APmvBwOjaakCXES4iGVJi2UxCtrUeX1vZ7oEAYDZes9KBgtpPJoEYIG3IE7cDdMLRWq@89E8gc "Wolfram Language (Mathematica) – Try It Online")
A port of [@tsh's JavaScript answer](https://codegolf.stackexchange.com/a/242700/9288).
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
Ż¬;1ƊḢ?
```
[Try it online!](https://tio.run/##y0rNyan8///o7kNrrA2PdT3cscj@/@H2o5Me7pyhGfn/f7SBDhTG6kDZhnC2IZxtCBc31IFCMBsig6QmFgA "Jelly – Try It Online")
Port of [Jonah's excellent J solution](https://codegolf.stackexchange.com/a/242708/85334).
```
     Ḣ     Remove the first element of the input.
Ż     ?    If it's 1, prepend 0 to the remaining elements, else
 ¬         negate the remaining elements
  ;1Ɗ      and append a 1.
```
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 7 bytes
```
ḢCɓŻṙ⁹^
```
[Try it online!](https://tio.run/##y0rNyan8///hjkXOJycf3f1w58xHjTvj/h9uPzrp4c4ZmpH//0cb6EBhrA6UbQhnG8LZhnBxQx0oBLMhMkhqYgE "Jelly – Try It Online")
Another port of tsh's idea:
```
ḢC         h = 1 - input.pop(0)
  ɓŻ       input.prepend(0)
    ṙ⁹          .rotate_left(h)
      ^         .xor(h)
```
I've rearranged it a bunch of ways looking for a 6-byte formulation, but I can't find one.
In J, this is something like `{.(-.@[|.=)0,}.` (15 bytes).
[Answer]
# [Retina 0.8.2](https://github.com/m-ender/retina/wiki/The-Language/a950ad7d925ec9316e3e2fb2cf5d49fd15d23e3d), ~~35~~ 24 bytes
```
1T`d`10`0.*|1
^1(.*)
$+1
```
[Try it online!](https://tio.run/##K0otycxL/K@q4Z7w3zAkISXB0CDBQE@rxpArzlBDT0uTS0Xb8P9/AxDgAmJDEGkIIg1BbEMQ4AIxISIA "Retina 0.8.2 – Try It Online") Link includes test cases. Explanation: Uses @tsh's method (see @Jonah's explanation).
```
1T`d`10`0.*|1
```
Invert the first bits, or all of the bits if the first bit is a `0`.
```
^1(.*)
$+1
```
If the first bit is now a `1` then move it to the end.
Previous 35-byte version actually performed the steps given in the question:
```
^
>
{`>0
1>
>1
<0
0<
<1
}`1<
0>
\D
```
[Try it online!](https://tio.run/##JYgxCoBADAT7@Ydgme1DKsFPiJyFhc0VYie@PRoc2GHYc7@OvuUwzi1XgruFoSCEG@a4eJocC5aJTCv4prLKqlZB5f@8 "Retina 0.8.2 – Try It Online") Link includes test cases. Explanation:
```
^
>
```
Mark the initial position and direction of the ball.
```
{`
}`
```
Repeat until the ball goes out of range.
```
>0
1>
>1
<0
0<
<1
1<
0>
```
Flip the number at the current position and update the position and direction of the ball. (I don't know any shorter way of describing this in Retina; even `T`>1<`<0>`>1|1<` is three bytes longer than replacing `>1` and `<1` manually.)
```
\D
```
Delete the marker.
[Answer]
# [Pari/GP](http://pari.math.u-bordeaux.fr/), 49 bytes
```
a->i=d=1;while(i>0&&i<=#a,i+=d=-d+2*a[i]=!a[i]);a
```
[Try it online!](https://tio.run/##TYpRCsIwEESvslYoiU2g62/dXCTkY6FWF4osRRBPH1NSiAwM82ZGeRP/0LwAZfZBaCacPk9Z70bC2PdyozM7GUrv5@F64SiJTrvbiTOrrl/D4APoJq93id0OHSyGrXUQ4@gOpUI1YgNsgG1Bd6hC3f5vKdn8Aw "Pari/GP – Try It Online")
---
## [Pari/GP](http://pari.math.u-bordeaux.fr/), 48 bytes, does not work for empty input
```
a->i=d=1;until(i<1||i>#a,i+=d=-d+2*a[i]=!a[i]);a
```
[Try it online!](https://tio.run/##TYrBCoQwEEN/ZVYv7drCzl7d9kdKDwPiMiAyiB4E/71WKlQCIS@J0ML2L2kEl8h6doPDfptXnhT/8DjYt2S4y7Uduu@bAkf3ulz3lEhk2hWB9SALz2uOzQUNjIq0NhDCx9yKmUrEClgB64LmVoGyPW8x6nQC "Pari/GP – Try It Online")
---
## [Pari/GP](http://pari.math.u-bordeaux.fr/), 44 bytes, does not work for empty input
```
a->if(a[1],a[1]=0;a,[!a[i%#a+1]|i<-[1..#a]])
```
[Try it online!](https://tio.run/##TY3BCoNADER/JVUEl2bFnFv9kZBDLkqglCC9FPrv6y4rbBkI85hh4npY3D1tsCSNq22jMgmWs8wPRb4p29DrneRnz8g0Tb2KhKTur@@oEFfww96fbLsCHeSJEBCYZ7wkmaqlBtSAWkJ4qULN/mvl9wk "Pari/GP – Try It Online")
A port of [@tsh's JavaScript answer](https://codegolf.stackexchange.com/a/242700/9288).
[Answer]
# [Python 3](https://en.wikipedia.org/wiki/Python_(programming_language)), 172 bytes
```
d=1
c=0
l=[int(i)for i in input()]
while c>-1 and c<len(l):
    (d:=not(d))if(l[c]==1)else(d:=d)
    l[c]=not l[c]
    if d==1:
        c+=1
    else:
        c-=1
print(l)
```
I just passed all of the test cases
And if a bit gets changed, it becomes a boolean (False is 0 and True is 1)
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 18 bytes
```
Ｉ⎇§θ⁰Ｅθ∧κι⊞Ｏ⁻¹Φθκ¹
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvRMM5sbhEIyS1KC@xqFLDscQzLyW1QqNQR8FAU0fBN7EAxHTMS9HI1lHI1AQKBZQWZ/gXpBYlluQXafhm5pUWaxjqKLhl5pSkFoHUZoMUGWoCgfX//9HRBkAO0CwIGRv7X7csBwA "Charcoal – Try It Online") Link is to verbose version of code. Explanation: Uses @tsh's method (see @Jonah's explanation).
```
   θ                Input array
  § ⁰               First element
 ⎇                  If nonzero then
      θ             Input array
     Ｅ              Map over elements
        κ           Current index
       ∧            Logical And
         ι          Current element
                    Else
             ¹      Literal integer `1`
            ⁻       Vectorised subtract
               θ    Input array
              Φ     Filtered by
                κ   Current index
          ⊞Ｏ     ¹  Append literal integer `1`
Ｉ                   Cast to string
                    Implicitly print
```
]