Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Clear&Readable solution in Clear category for [old] Matrix Pattern by SaintDron
"use strict";
function matrix(pattern, image) {
for (let ir = 0; ir < image.length; ir++) {
for (let ic = 0; ic < image[0].length; ic++) {
if (pattern.every((pr, r) => pr.every((v, c) => image[ir + r] && image[ir + r][ic + c] === v)))
pattern.forEach((pr, r) => pr.forEach((_, c) => image[ir + r][ic + c] += 2));
}
}
return image;
}
var assert = require('assert');
if (!global.is_checking) {
// These "asserts" are used for self-checking and not for an auto-testing
assert.deepEqual(matrix([[1, 0], [1, 1]],
[[0, 1, 0, 1, 0],
[0, 1, 1, 0, 0],
[1, 0, 1, 1, 0],
[1, 1, 0, 1, 1],
[0, 1, 1, 0, 0]]), [[0, 3, 2, 1, 0],
[0, 3, 3, 0, 0],
[3, 2, 1, 3, 2],
[3, 3, 0, 3, 3],
[0, 1, 1, 0, 0]])
assert.deepEqual(matrix([[1, 1], [1, 1]],
[[1, 1, 1],
[1, 1, 1],
[1, 1, 1]]), [[3, 3, 1],
[3, 3, 1],
[1, 1, 1]])
assert.deepEqual(matrix([[0, 1, 0], [1, 1, 1]],
[[0, 0, 1, 0, 0, 0, 0, 0, 1, 0],
[0, 1, 1, 1, 0, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 1, 0, 0, 1, 1, 1, 0, 1, 0],
[1, 1, 1, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0],
[0, 1, 1, 0, 0, 0, 1, 1, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]), [[0, 2, 3, 2, 0, 0, 0, 2, 3, 2],
[0, 3, 3, 3, 0, 0, 0, 3, 3, 3],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 2, 3, 2, 0, 0, 0],
[2, 3, 2, 0, 3, 3, 3, 0, 1, 0],
[3, 3, 3, 0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 1, 1, 1, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 2, 3, 2, 0],
[0, 1, 1, 0, 0, 0, 3, 3, 3, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
console.log("Coding complete? Click 'Check' to earn cool rewards!");
}
Aug. 3, 2018
Comments: