The Deaf Rats of Hamelin

This commit is contained in:
2019-12-01 13:13:51 +01:00
parent 2c4b7f8f3f
commit 0ddcf2d424

View File

@@ -0,0 +1,27 @@
// https://www.codewars.com/kata/the-deaf-rats-of-hamelin/javascript
const countDeafRats = function(town) {
const [l, r] = town.split('P');
const cnt = s => {
const c = [0, 0];
const p = ['O~', '~O'];
while (1) {
let idx = p.map(p => s.indexOf(p));
let min = Math.min(...idx);
let max = Math.max(...idx);
let sidx;
if (0 <= min) {
sidx = min;
} else if (0 <= max) {
sidx = max;
} else {
break;
}
const sp = s.slice(sidx, sidx+2);
s = s.replace(sp, '');
++c[p.indexOf(sp)];
}
return c;
};
return cnt(l)[0] + cnt(r)[1];
};