| Bytes | Lang | Time | Link |
|---|---|---|---|
| 047 | JavaScript Node.js | 250829T132033Z | Fhuvi |
| 119 | C64 Basic | 250824T162728Z | OSI8 |
| 064 | Python | 250824T104901Z | Albert.L |
| 073 | Julia | 250825T193208Z | MarcMush |
| 094 | Python 3 | 250822T200727Z | Adam |
| 092 | Julia | 250823T171756Z | M-- |
| 106 | R | 250823T061937Z | M-- |
| 110 | Google Sheets | 250822T220049Z | doubleun |
| 025 | Charcoal | 250822T232051Z | Neil |
| 085 | Nibbles | 250822T221437Z | Adam |
| 088 | Janet | 250822T203634Z | Adam |
| 011 | Jelly | 250822T191035Z | Adam |
| 135 | Python3 | 250822T171537Z | Ajax1234 |
| 048 | JavaScript ES6 | 250822T105926Z | Arnauld |
| 025 | 05AB1E | 250822T140446Z | Kevin Cr |
| 022 | Retina 0.8.2 | 250822T115008Z | Neil |
JavaScript (Node.js), 47 48 bytes
-1 byte thanks to doubleunary
Input and output are strings. Because of the implementation, this particular solution can't handle more than eight zeroes in a row, if that is acceptable.
s=>s.replace(/([^0])0+(?=\1)/g,m=>m[0]+--m/9|0)
The regex matches all zeroes contained between two same strictly positive digits, and also captures the first digit (but not the last because it might be used for another group of zeroes).
Then, we concatenate the first digit with : the number matched minus one and divided by 9 (without the fractional part).
Example of this ugly trick on 500 :
500 - 1 => 499
499 / 9 => 55.444...
55.444... | 0 => 55
"5"+55 => 555
C64 Basic, 124 119 bytes
0dIa(99)
1inputx:a(i)=x:fOk=jto-i*(x=a(j))
2a(k)=a(j):nE:j=(x=0)*(1-k)-(x>0)*i:i=i+1:ifpE(512)tH1
3fOj=0toi-2:?a(j);:nE
After starting the program, enter the individual array values and confirm with RETURN. After the last value, simply press RETURN without entering any value.
109 bytes (with restrictions):
Ten bytes could be saved by omitting the DIM line. But then you would be limited to a maximum of ten input values.
0inputx:a(i)=x:fOk=jto-i*(x=a(j))
1a(k)=a(j):nE:j=(x=0)*(1-k)-(x>0)*i:i=i+1:ifpE(512)gO
2fOj=0toi-2:?a(j);:nE
Python, 64 bytes
f=lambda L,p=0:L and[p:=L[0]or(max(L,key=bool)==p)*p]+f(L[1:],p)
How?
Uses max(L,key=bool) to find the first nonzero entry of L or 0 if there is none.
Previous Python, 79 bytes
def f(L):
p=q=l=0
for c in L:
if c:L[p:q]=(q-p)*[l*(l==c)];l=c;p=q+1
q+=1
Expects a list and changes it in place.
Julia, 73 bytes
~=keys
!x=[x[i:j].=x[i] for i=~x,j=~x if x[i]==x[j]>0<all(x[i+1:j-1].<1)]
Based on M--'s answer
Python 3, 99 94 bytes
lambda a:[x*(x==y)for x,y in zip(f(a),f(a[::-1])[::-1])]
f=lambda a,v=0:[v:=x or v for x in a]
Uses the same approach as my Jelly answer.
Julia, 92 bytes
f(x)=(n=length(x);for i=1:n,j=i+1:n;x[i]==x[j]>0&&all(x[i+1:j-1].==0)&&(x[i:j].=x[i]);end;x)
Similar to my second attempt using R.
R, 106 bytes
f=\(x){for(i in 1:length(x))for(j in(i+1):length(x))if(x[i]==x[j]&x[i]&all(!x[(i+1):(j-1)]))x[i:j]=x[i];x}
R, 102 92 bytes* with warnings
f=\(x,n=seq(x)){for(i in n)for(j in n)if(x[i]==x[j]&x[i]&!any(x[(i+1):(j-1)]))x[i:j]=x[i];x}
* Thanks to SamR's comment
Google Sheets, 110 bytes
=index(let(d,A:A,s,row(d),f,lambda(a,scan(0,a,lambda(a,n,if(n,n,a)))),if(f(d)=sort(f(sort(d,s,)),s,),f(d),d)))
Uses Adamátor's method.

Ungolfed:
=let(
d, tocol(A1:A, 1),
reverse_, lambda(a, sort(a, sequence(rows(a)),)),
fill_, lambda(a, scan(0, a, lambda(a, n, if(n, n, a)))),
arrayformula(if(fill_(d) = reverse_(fill_(reverse_(d))), fill_(d), d))
)
Charcoal, 25 bytes
IEEθ↨Φ…θ⊕κλ⁰∧¬⌕Φ✂θκLθ¹λιι
Try it online! Link is to verbose version of code. Explanation: The inner Map performs a cumulative Or reduction on the array, while the outer map checks that each resulting value is the first non-zero value in the rest of the original array (which is equivalent to comparing against a reverse cumulative Or reduction), replacing invalid values with zero.
θ Input array
E Map over values
θ Input array
… Truncated to length
κ Current index
⊕ Incremented
Φ Filtered on
λ Non-zero values
↨ ⁰ Interpret as base `0` (take last element or `0` if none)
E Map over values
⌕ Index of
ι Current value in
θ Input array
✂ ¹ Sliced from
κ Current index to
θ Input array
L Length
Φ Filtered on
λ Non-zero values
¬ Is zero
∧ Logical And
ι Current value
I Cast to string
Implicitly print each value on its own line
Janet, 88 bytes
|(let[f|(accumulate|(case $1 0 $ $1)0 $)r reverse](map|(case $ $1 $ 0)(f $)(r(f(r $)))))
Uses the same approach as my Jelly answer.
Jelly, 11 bytes
o@\
UÇU=Ç×Ç
We left scan the array with the reversed logical OR function, causing all zeros to be replaced with the previous non-zero element:
[0,2,0,1,0,2,0,0,2,0]
[0,2,2,1,1,2,2,2,2,2]
Next we do the same thing, but in reverse:
[0,2,0,1,0,2,0,0,2,0]
[2,2,1,1,2,2,2,2,2,0]
Out of the two resulting arrays, we take equal elements and set all other elements to 0:
[0,2,2,1,1,2,2,2,2,2]
[2,2,1,1,2,2,2,2,2,0]
[0,2,0,1,0,2,2,2,2,0]
I haven’t used Jelly in a really long time, so there is probably a shorter way to express this algorithm.
Python3, 135 bytes
def f(s):
for i in range(len(s)):
for I in range(i+1,len(s)):s[i:I]=[s[i:I],[s[i]]*(I-i)][s[i]==s[I]and not any(s[i+1:I])]
return s
JavaScript (ES6), 48 bytes
a=>a.map(p=(v,i)=>v|a.find(v=>i--*v<0)!=p?p=v:p)
Commented
a => // a[] = input array
a.map(p = // start with p non-numeric
(v, i) => // for each value v at index i in a[]:
v | // if v is not zero
a.find(v => // or the first non-zero value in a[]
i-- * v < 0 // at an index higher than i (1)
) != p ? // is not equal to p (2):
p = v // set p to v
: // else:
p // use the current value of p
) // end of map()
(1) The expression i-- * v < 0 is true if and only if i is negative and v is non-zero.
(2) If there's no such non-zero value at all, find() returns undefined, which cannot be equal to p.
05AB1E, 25 bytes
®.øγü3εÅsy˜ÔÀ`©Qs_*i0®:]˜
Try it online or verify all test cases.
Explanation:
®.ø # Surround the (implicit) input-list with leading/trailing -1
γ # Adjacent-group all the same values together
ü3 # Split it into overlapping triplets
ε # Map over each triplet:
Ås # Keep the middle list of the current triplet
y # Push the triplet of three lists again
˜ # Flatten it to a single list
Ô # Connected-uniquify it to a triplet of integers [a,b,c]
À # Rotate once towards the left: [b,c,a]
` # Pop and push all its values to the stack
© # Store the top value in variable `®` (without popping)
Q # Pop the top two and check if they're the same: c==a
s # Swap so the third value is at the top
_ # Check that it's 0: b==0
*i # If both are truthy:
0®: # Replace all 0s with value `®` in the middle list
] # Close both the if-statement and map
˜ # Flatten the list of lists
# (after which the result is output implicitly)
Retina 0.8.2, 22 bytes
(?<=(.)0*)0(?=0*\1)
$1
Try it online! Link includes test cases. Uses strings of different printable Unicode characters to represent the emitters, except 0 represents an inactive gate, for ease of converting the test cases. Explanation: Uses lookarounds to find matching emitters surrounding inactive gates and replaces the latter with the former.
