Created
January 5, 2021 20:57
-
-
Save pricklywiggles/4d96da7cee3d537770bda4bf860acde9 to your computer and use it in GitHub Desktop.
Cassidoo-177
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
function canReach([sx,sy], [dx,dy]) { | |
return Math.sqrt((sx-dx)**2 + Math.abs(sy-dy)**2) < 5 | |
} | |
function canToggle({ switch: current, hub, light: end }) { | |
if (canReach(current, end)) | |
return true; | |
if (hub.length == 0) return false; | |
for(let [i,node] of hub.entries()) { | |
if (canReach(current, node)) { | |
let newHub = hub.filter((node, ci) => ci !== i); | |
let reached = canToggle({ switch: node, hub: newHub, light: end}) | |
if (reached) return true; | |
} | |
} | |
return false; | |
} | |
let tests = [{ | |
switch: [0,1], | |
hub: [[2,1], [2,5]], | |
light: [1,6] | |
},{ | |
switch: [0,1], | |
hub: [[2,1], [2,5]], | |
light: [1,9] | |
},{ | |
switch: [0,1], | |
hub: [[2,1], [2,5]], | |
light: [1,10] | |
},{ | |
switch: [0,1], | |
hub: [], | |
light: [1,4] | |
},{ | |
switch: [0,1], | |
hub: [], | |
light: [1,9] | |
} | |
] | |
tests.map(canToggle) | |
// Output: [ true, true, false, true, false ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment