점 네 개의 좌표를 담은 이차원 배열 dots
가 다음과 같이 매개변수로 주어집니다.
주어진 네 개의 점을 두 개씩 이었을 때, 두 직선이 평행이 되는 경우가 있으면 1을 없으면 0을 return 하도록 solution 함수를 완성해보세요.
dots
의 길이 = 4dots
의 원소는 [x, y] 형태이며 x, y는 정수입니다.function solution(dots) {
const dots1And2Slope = (dots[0][0] - dots[1][0]) / (dots[0][1] - dots[1][1])
const dots3And4Slope = (dots[2][0] - dots[3][0]) / (dots[2][1] - dots[3][1])
const dots1And3Slope = (dots[0][0] - dots[2][0]) / (dots[0][1] - dots[2][1])
const dots2And4Slope = (dots[1][0] - dots[3][0]) / (dots[1][1] - dots[3][1])
const dots1And4Slope = (dots[0][0] - dots[3][0]) / (dots[0][1] - dots[3][1])
const dots2And3Slope = (dots[1][0] - dots[2][0]) / (dots[1][1] - dots[2][1])
if (
dots1And2Slope === dots3And4Slope ||
dots1And3Slope === dots2And4Slope ||
dots1And4Slope === dots2And3Slope
)
return 1
else return 0
}
function solution(dots) {
const leans = []
for(let i = 0; i < dots.length; i++) {
const dotA = dots[i];
for(let j = i + 1; j < dots.length; j++) {
const dotB = dots[j]
const lean = (dotB[1] - dotA[1]) / (dotB[0] - dotA[0])
if(leans.includes(lean)) return 1
else leans.push(lean)
}
}
return 0;
}
이번에도 1시간 정도 고민해보다가 안 풀리면 다음 날 다시 시도해 볼 생각이었다. 그렇게 조급함을 내려놓고 차분하게 생각을 거듭하다 보니까, 기존 방법과는 다른 해결책이 떠올랐다. 앞으로도 계속 풀리 때까지 이 방법으로 계속 시도해보자.