Created
January 16, 2021 05:53
-
-
Save FelixLuciano/613ab4366c89d058e77ef5c361aaa00f to your computer and use it in GitHub Desktop.
You’re trying to build an IoT mesh network. Signals can only travel the maximum of 5 units. You’re given coordinates for the switch, the light, and the mesh hubs (which capture and forward signals). Return true if the switch can successfully toggle the light.
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 distance = ([x1, y1], [x2, y2]) => Math.hypot(x1-x2, y1-y2) | |
function canToggle ({ switch:toggle, hub, light }, radius = 5) { | |
if (distance(toggle, light) <= radius) return true | |
if (!hub.length) return false | |
const steps = hub.filter(step => distance(step, toggle) <= radius) | |
return steps.some(step => { | |
const next = hub.filter(point => !point.every((a, i) => a === step[i])) | |
return canToggle({switch:step, hub:next, light}, radius) | |
}) | |
} | |
let network = { switch: [0,1], hub: [[2,1], [2,5]], light: [1,6] } | |
canToggle(network) // -> true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment