Skip to content

Instantly share code, notes, and snippets.

@pricklywiggles
Created January 25, 2021 19:46
Show Gist options
  • Save pricklywiggles/1f35444eac010c5c21f557dd3f43351c to your computer and use it in GitHub Desktop.
Save pricklywiggles/1f35444eac010c5c21f557dd3f43351c to your computer and use it in GitHub Desktop.
Casidoo-180
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