Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Created August 8, 2019 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crazy4groovy/aacd9b8f39bdc4cf404cb8b39c3352ac to your computer and use it in GitHub Desktop.
Save crazy4groovy/aacd9b8f39bdc4cf404cb8b39c3352ac to your computer and use it in GitHub Desktop.
How many straight lines, starting from point {x: 0, y: 0}, are needed to intersect with all the points in a given array?
function normalize({x, y}) {
const nx = x / Math.abs(x)
const ny = y / Math.abs(x)
return nx + '|' + ny
}
/*
console.log(normalize({x: 1, y: 2}))
console.log(normalize({x: 2, y: 4}))
console.log(normalize({x: -2, y: 4}))
console.log(normalize({x: 2, y: -4}))
console.log(normalize({x: -2, y: -4}))
console.log(normalize({x: -1, y: -2}))
console.log(normalize({x: -1, y: -4}))
*/
function solution(A) {
const nA = A.map(i => normalize(i))
const uniqueA = [...new Set(nA)]
return uniqueA.length
}
/*
console.log(solution([{x:1, y:1}, {x:2, y:2}])) // 1
console.log(solution([{x:-1, y:1}, {x:-2, y:2}])) // 1
console.log(solution([{x:-1, y:-1}, {x:-2, y:-2}])) // 1
console.log(solution([{x:-1, y:-1}, {x:2, y:2}])) // 2
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment