Skip to content

Commit 411601d

Browse files
committed
Making stars at intersection points / 심화
1 parent 5777723 commit 411601d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
function solution(line) {
2+
const points = new Set();
3+
4+
// 교점 찾기(크래머 공식)
5+
for (let i = 0; i < line.length - 1; i++) {
6+
for (let j = i + 1; j < line.length; j++) {
7+
const [a, b, e] = line[i];
8+
const [c, d, f] = line[j];
9+
10+
const denominator = a * d - b * c;
11+
if (denominator === 0) continue;
12+
13+
const x = (b * f - e * d) / denominator;
14+
const y = (e * c - a * f) / denominator;
15+
16+
if (x === Math.floor(x) && y === Math.floor(y)) {
17+
points.add(`${x},${y}`); // set에서 중복없이 하려고 배열 대신 문자열 이용 -> 나중에 다시 변환 필요
18+
}
19+
}
20+
}
21+
22+
// 좌표 배열로 변환, 최대/최소 구하기
23+
const coords = Array.from(points).map((p) => p.split(",").map(Number));
24+
const [minX, maxX] = [
25+
Math.min(...coords.map((p) => p[0])),
26+
Math.max(...coords.map((p) => p[0])),
27+
];
28+
const [minY, maxY] = [
29+
Math.min(...coords.map((p) => p[1])),
30+
Math.max(...coords.map((p) => p[1])),
31+
];
32+
33+
// 격자 생성, 별 찍기
34+
const grid = Array(maxY - minY + 1)
35+
.fill()
36+
.map(() => Array(maxX - minX + 1).fill("."));
37+
coords.forEach(([x, y]) => {
38+
grid[maxY - y][x - minX] = "*";
39+
});
40+
41+
return grid.map((row) => row.join(""));
42+
}

0 commit comments

Comments
 (0)