forked from lunchScreen/Problem_Solving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[프로그래머스] 신재웅_교점에 별 만들기.swift
68 lines (58 loc) · 2.14 KB
/
[프로그래머스] 신재웅_교점에 별 만들기.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import Foundation
func solution(_ line:[[Int]]) -> [String] {
let points = pointsOf(line)
return points.starMaker()
}
func pointsOf(_ line: [[Int]]) -> Points {
var points: [Point] = []
for index1 in 0..<line.count {
for index2 in 0..<line.count {
guard index1 < index2,
let point = intersection(line[index1], line[index2]) else { continue }
points.append(point)
}
}
return Points(points)
}
func intersection(_ left: [Int], _ right: [Int]) -> Point? {
guard left.count == 3 && right.count == 3 else { exit(-1) }
let a = left[0]; let b = left[1]; let e = left[2]
let c = right[0]; let d = right[1]; let f = right[2]
let denominator = a * d - b * c
let parallel = a * d - b * c
guard parallel != 0, denominator != 0 else { return nil }
let x = Double(b * f - e * d) / Double(denominator)
let y = Double(e * c - a * f) / Double(denominator)
guard x == ceil(x) && y == ceil(y) else { return nil }
return Point(x: Int(x), y: Int(y))
}
struct Point: Hashable {
let x: Int
let y: Int
}
struct Points: CustomStringConvertible {
var description: String {
points.map { "(\($0.x), \($0.y))" }.joined(separator: " ")
}
private var points: Set<Point> = []
private var minX: Int { return points.sorted{ $0.x < $1.x }.first!.x }
private var maxX: Int { return points.sorted{ $0.x < $1.x }.last!.x }
private var minY: Int { return points.sorted{ $0.y < $1.y }.first!.y }
private var maxY: Int { return points.sorted{ $0.y < $1.y }.last!.y }
private var width: Int { return self.maxX - self.minX + 1 }
private var height: Int { return self.maxY - self.minY + 1 }
init(_ points: [Point]) {
points.forEach {
self.points.update(with: $0)
}
}
func starMaker() -> [String] {
var matrix: [[String]] = Array(repeating: Array(repeating: ".", count: width), count: height)
points.forEach{
let x = $0.x - minX
let y = $0.y - minY
matrix[height - y - 1][x] = "*"
}
return matrix.map { $0.joined() }
}
}