Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear OOP solution in Clear category for [old] Keys and Locks by SaintDron
"use strict";
function keysAndLocks(lock, some_key) {
class Figure {
constructor(str) {
this.arr = str.trim().split('\n').map(r => [...r]);
}
clip() {
this.arr = this.arr.filter(row => row.includes('#'));
return this;
}
rotate() {
this.arr = this.arr[0].map((_, c) => this.arr.map(row => row[c]).reverse());
return this;
}
toJSON() {
return JSON.stringify(this.arr);
}
static compare(f1, f2) {
for (let i = 0; i < 4; i++) {
if (f1.toJSON() === f2.toJSON())
return true;
f2.rotate();
}
return false;
}
}
lock = new Figure(lock).clip().rotate().clip();
some_key = new Figure(some_key).clip().rotate().clip();
return Figure.compare(lock, some_key);
}
var assert = require('assert');
if (!global.is_checking) {
console.log('Example:')
console.log(keysAndLocks(`
0##0
0##0
00#0
00##
00##`,
`
00000
000##
#####
##000
00000`))
// These "asserts" are used for self-checking and not for an auto-testing
assert.equal(keysAndLocks(`
0##0
0##0
00#0
00##
00##`,
`
00000
000##
#####
##000
00000`), true)
assert.equal(keys_and_locks(`
###0
00#0`,
`
00000
00000
#0000
###00
0#000
0#000`), false)
assert.equal(keys_and_locks(`
0##0
0#00
0000`,
`
##000
#0000
00000
00000
00000`), true)
console.log("Coding complete? Click 'Check' to earn cool rewards!");
}
Sept. 13, 2018
Comments: