-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path09.swift
49 lines (44 loc) · 1.15 KB
/
09.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
struct Pos: Hashable {
var x = 0, y = 0
mutating func adjust(_ to: Pos) {
if abs(to.x - self.x) > 1 ||
abs(to.y - self.y) > 1 {
self.x += direction(self.x, to.x)
self.y += direction(self.y, to.y)
}
}
func direction(_ from: Int, _ to: Int) -> Int {
return from == to ? 0 : (from > to ? -1 : 1)
}
}
struct Rope {
var knots: [Pos] = []
var visited: Set<Pos> = []
init(_ nknots: Int) {
for _ in 1...nknots { self.knots.append(Pos()) }
}
mutating func move(_ dx: Int, _ dy: Int) {
knots[0].x += dx
knots[0].y += dy
for i in 1..<knots.count {
knots[i].adjust(knots[i - 1])
}
visited.insert(knots.last!)
}
}
var rope1 = Rope(2), rope2 = Rope(10)
while let line = readLine() {
var dx = 0, dy = 0, n = Int(line.dropFirst(2))!
switch line.prefix(1) {
case "R": dx = 1
case "L": dx = -1
case "D": dy = 1
case "U": dy = -1
default: break
}
for _ in 1...n {
rope1.move(dx, dy)
rope2.move(dx, dy)
}
}
print(rope1.visited.count, rope2.visited.count)