Created
January 25, 2021 19:46
-
-
Save pricklywiggles/1f35444eac010c5c21f557dd3f43351c to your computer and use it in GitHub Desktop.
Casidoo-180
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const rotate = v => { | |
let n = v.length-1; | |
for (let y=0; y < Math.floor(v.length/2); y++) { | |
for (let x=0+y; x < v.length-y-1; x++) { | |
[v[y][x], v[n-x][y], v[n-y][n-x], v[x][n-y]] = [v[n-x][y], v[n-y][n-x], v[x][n-y], v[y][x]] | |
} | |
} | |
return v; | |
} | |
let tests = [ | |
[[[1,2,3],[4,5,6],[7,8,9]], [[7,4,1],[8,5,2],[9,6,3]]], | |
[[[1,2,3,4],[4,5,6,7],[7,8,9,10],[11,12,13,14]], [[11,7,4,1],[12,8,5,2],[13,9,6,3],[14,10,7,4]]], | |
[[[1,2,3],[4,5,6],[7,8,9]], [[7,4,1],[8,5,2],[9,6,3]],], | |
[[[5]], [[5]],], | |
[[[]], [[]]] | |
] | |
let isEqual = (v1, v2) => JSON.stringify(v1) === JSON.stringify(v2) | |
tests.forEach(([input, expected]) => { | |
let observed = rotate(input) | |
if (!isEqual(observed, expected)) | |
console.log(`Failure on ${input}: result was ${observed}`); | |
}) | |
// Output: No failures. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment