Skip to content

Instantly share code, notes, and snippets.

@ReeganExE
Last active September 15, 2017 06:38
Show Gist options
  • Save ReeganExE/a16bb4d74e05563cc7559b1a15b0ef9c to your computer and use it in GitHub Desktop.
Save ReeganExE/a16bb4d74e05563cc7559b1a15b0ef9c to your computer and use it in GitHub Desktop.
(function(input) {
String.prototype.yeah = function(sub) {
const r = new RegExp(sub, 'g');
const rs = [];
let i = null;
while ((i = r.exec(this)) !== null) {
rs.push(i.index);
}
return rs;
}
class Quyen {
static fromString(str) {
return new Quyen(str.split('|').map(row => row.split(' ')));
}
constructor(matrix) {
this.mix = matrix;
}
findSomething() {
const rows = this.mix;
const rs = [];
for (let r = 0, m; r < rows.length; r++) {
m = rows[r].join('')
.yeah('010')
.map(idx => idx + 1)
.filter(idx => this.checkAround(r, idx));
if (m.length) {
rs.push(m.map(idx => [r, idx]));
}
}
return rs;
}
checkAround(r, c) {
const top = this.top(r, c);
const bot = this.bot(r, c);
return (top != '1') && (bot != '1');
}
top(r, c) {
r = this.mix[r - 1];
return r ? r[c] : null;
}
bot(r, c) {
r = this.mix[r + 1];
return r ? r[c] : null;
}
}
return Quyen.fromString(input).findSomething();
})('1 0 0 0 1|0 1 0 0 0|0 0 1 1 0|0 1 0 0 0');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment