Water filling of custom profile
Task for water filling of custom profile, described by numbers array, with visualization and without it.
by spmbt
HTML
(Only for console.log)<br>
<br>
Task for water filling of custom profile with visualization and without it.<br>
<br>
<i>Show results in console, please.</i>
JavaScript
console.clear();
var a =[], b =[]
,width =6 //0..99
,h =12; //0..99
for(var i =0; i < width; i++)
a.push(Math.random() * h +1 |0), b.push(0);
//a=[9,0,10,9,10,0]; //static test (length = width)
var c = 0;
for(var hh =1; hh < h +1; hh++){
var land =0
,lastC =0
,visL =[]; //visualLeft
for(var i =0; i < width; i++){
land = land || a[i] >= hh;
if(land && a[i] < hh){
c++;
lastC++;
visL.push(b[i]); //for visualize
b[i] = hh;
}
if(a[i] >= hh){
lastC =0;
visL =[];
}
}
c -= lastC;
while(lastC-- && lastC >=0) //for visualize
b[--i] = visL[lastC];
}
for(var j =0; j < h; j++){ //visualization
var s ='';
for(var i =0; i < width; i++)
s += h - a[i] <= j ? 'M' : (h - b[i] <= j ?'.':'_');
console.log(s +' '+ (h - j) );
}
console.log('c = '+ c +' - number of points above');
console.log('a = '+ a.join(',') +' - profile');
console.log('b = '+ b.join(',') +' - filled levels');
console.log(' ')
//static test without visualization
var width =6
,h =12;
a=[9,0,10,9,10,0] //c = 10
,c = 0;
for(var hh =1; hh < h +1; hh++){
var land =0
,lastC =0;
for(var i =0; i < width; i++){
land = land || a[i] >= hh;
if(land && a[i] < hh){
c++;
lastC++;
}
if(a[i] >= hh)
lastC =0;
}
c -= lastC;
}
console.log('c = '+ c);
console.log('a = '+ a.join('\t,'));