Skip to content

Instantly share code, notes, and snippets.

@pricklywiggles
Created January 5, 2021 20:57
Show Gist options
  • Save pricklywiggles/4d96da7cee3d537770bda4bf860acde9 to your computer and use it in GitHub Desktop.
Save pricklywiggles/4d96da7cee3d537770bda4bf860acde9 to your computer and use it in GitHub Desktop.
Cassidoo-177
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