JavaScript ES6, 8383 81 bytes
f=(z,a=[])=>z.replace(/ *0/g,e=>e.replace('0'0,a.fill(0,l=e.length)[--l]=a[l]+1||1))
This uses an array that holds the current number for each indentation level. Everything past that level is reset to 0 using fill()
. EDIT: 2 bytes saved thanks to vihan1086's tip.
The Stack Snippet below can be used for testing because it is ungolfed slightly and uses better-supported ES5 syntax. The second function is a polyfill for fill()
as there is not a short way to do it without ES6.
f=function(z){
a=[]
return z.replace(/ *0/g,function(e){
return e.replace('0'0,a.fill(0,l=e.length)[--l]=a[l]+1||1)
})
}
if(!Array.prototype.fill){
Array.prototype.fill = function(val, start){
var res = this;
for(var i = start; i<this.length; i++){
res[i] = val;
}
return res;
};
}
run=function(){document.getElementById('output').innerText=f(document.getElementById('input').value)};document.getElementById('run').onclick=run;run()
<textarea id="input" rows="15" cols="10">
0
0
0
0
0
0
0
0
0
0
0
0
0
0</textarea>
<pre id="output" style="display:inline-block; vertical-align:top; margin:0"></pre><br />
<button id="run">Run</button>