Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3a70cd1

Browse files
feat: 2 / 교점에 별 만들기 / 새로운 문제
1 parent 35e9513 commit 3a70cd1

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

‎level-2/교점에-별-만들기.js‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//https://github.com/codeisneverodd/programmers-coding-test
2+
//더 좋은 풀이가 존재할 수 있습니다.
3+
//정답 1 - codeisneverodd
4+
function solution(line) {
5+
const getCrossPoint = ([A, B, E], [C, D, F]) => {
6+
if (A * D - B * C === 0) return [Infinity, Infinity];
7+
return [(B * F - E * D) / (A * D - B * C), (E * C - A * F) / (A * D - B * C)];
8+
}; //문제 설명 최하단 참조
9+
10+
const crossPoints = line.flatMap((lineA, i) =>
11+
line
12+
.slice(i + 1)
13+
.map(lineB => getCrossPoint(lineA, lineB))
14+
.filter(([x, y]) => Number.isInteger(x) && Number.isInteger(y))
15+
);
16+
17+
const generateCanvas = crossPoints => {
18+
const xPoints = [...crossPoints.map(([x, y]) => x)];
19+
const yPoints = [...crossPoints.map(([x, y]) => y)];
20+
const [minX, maxX] = [Math.min(...xPoints), Math.max(...xPoints)];
21+
const [minY, maxY] = [Math.min(...yPoints), Math.max(...yPoints)];
22+
const xLength = Math.abs(maxX - minX) + 1;
23+
const yLength = Math.abs(maxY - minY) + 1;
24+
25+
return {
26+
canvas: Array.from({ length: yLength }, () => Array(xLength).fill('.')),
27+
draw([x, y], value) {
28+
this.canvas[Math.abs(y - maxY)][Math.abs(x - minX)] = value;
29+
},
30+
print() {
31+
return this.canvas.map(row => row.join(''));
32+
},
33+
};
34+
};
35+
36+
const canvas = generateCanvas(crossPoints);
37+
38+
crossPoints.forEach(point => {
39+
canvas.draw(point, '*');
40+
});
41+
42+
return canvas.print();
43+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /