-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.swift
34 lines (29 loc) · 1.05 KB
/
main.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
import Foundation
func main() throws {
let directionsText: String = try readInput(fromTestFile: false)[0]
var directions = [(0, 1), (1, 0), (0, -1), (-1, 0)]
var position = (0, 0)
var locations = Set<Coordinate>()
var partTwoDone = false
for d in directionsText.components(separatedBy: ", ") {
let matches = Regex("(\\w)(\\d+)").getMatches(in: d)
if matches[0] == "R" {
directions.rotateLeft(positions: 1)
} else {
directions.rotateRight(positions: 1)
}
let value = Int(matches[1])!
for _ in 1...value {
position.0 += directions[0].0 * 1
position.1 += directions[0].1 * 1
let coord = Coordinate(position.0, position.1)
if !partTwoDone && locations.contains(coord) {
print("Part two:", coord.getManhattanDistance(to: .init(0, 0)))
partTwoDone = true
}
locations.insert(coord)
}
}
print("Part one:", abs(position.0) + abs(position.1))
}
Timer.time(main)