-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution_2020_12.swift
164 lines (143 loc) · 4.47 KB
/
Solution_2020_12.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// Solution_2020_12.swift
//
//
// Created by Ivan Chalov on 12.12.20.
//
import Foundation
import Algorithms
import Prelude
private enum Command {
case move(Direction)
case rotate(clockwise: Bool)
case forward
init(string: String) {
switch string {
case "N", "E", "W", "S":
self = .move(Direction(rawValue: string)!)
case "L":
self = .rotate(clockwise: false)
case "R":
self = .rotate(clockwise: true)
case "F":
self = .forward
default:
fatalError("Incorrect Input")
}
}
}
private enum Direction: String, CaseIterable {
case north = "N"
case east = "E"
case south = "S"
case west = "W"
var unitVector: (x: Int, y: Int) {
switch self {
case .north: return ( 0, 1)
case .east: return ( 1, 0)
case .south: return ( 0, -1)
case .west: return (-1, 0)
}
}
mutating func rotate(by degrees: Int, clockwise: Bool = true) {
let directions = Direction.allCases
var value = degrees / 90 % directions.count
if !clockwise { value = directions.count - value }
self = directions[(directions.firstIndex(of: self)! + value) % directions.count]
}
}
private struct Instruction {
var command: Command
var value: Int
}
extension Instruction {
init(string: String) {
self.init(
command: Command(string: String(string.prefix(1))),
value: Int(string.dropFirst())!
)
}
}
private protocol Navigation {
var position: (x: Int, y: Int) { get }
var manhattanDistance: Int { get }
func advance(_ coordinates: inout (x: Int, y: Int), by value: Int, in direction: Direction)
}
extension Navigation {
var manhattanDistance: Int {
abs(position.x) + abs(position.y)
}
func advance(_ coordinates: inout (x: Int, y: Int), by value: Int, in direction: Direction) {
let unitVector = direction.unitVector
coordinates.x += value * unitVector.x
coordinates.y += value * unitVector.y
}
}
private struct Navigation1: Navigation {
var position: (x: Int, y: Int) = (0, 0)
var facing: Direction = .east
mutating func apply(_ instruction: Instruction) {
switch instruction.command {
case let .move(direction):
advance(&position, by: instruction.value, in: direction)
case let .rotate(clockwise: clockwise):
facing.rotate(by: instruction.value, clockwise: clockwise)
case .forward:
advance(&position, by: instruction.value, in: facing)
}
}
}
private struct Navigation2: Navigation {
var position: (x: Int, y: Int) = (0, 0)
var waypoint: (x: Int, y: Int) = (10, 1)
mutating func apply(_ instruction: Instruction) {
switch instruction.command {
case let .move(direction):
advance(&waypoint, by: instruction.value, in: direction)
case let .rotate(clockwise: clockwise):
rotate(by: instruction.value, clockwise: clockwise)
case .forward:
moveToWaypoint(times: instruction.value)
}
}
mutating func rotate(by degrees: Int, clockwise: Bool = true) {
let radians = degrees.radians() * (clockwise ? -1 : 1)
waypoint = (
x: lround(cos(radians) * Double(waypoint.x) - sin(radians) * Double(waypoint.y)),
y: lround(sin(radians) * Double(waypoint.x) + cos(radians) * Double(waypoint.y))
)
}
mutating func moveToWaypoint(times: Int) {
position = (
x: position.x + times * waypoint.x,
y: position.y + times * waypoint.y
)
}
}
private extension Int {
func radians() -> Double {
Double(self) * .pi / 180
}
}
struct Solution_2020_12: Solution {
var input: Input
func run() throws {
let input = try self.input.get()
.split(whereSeparator: \.isNewline)
.map(String.init)
.map(Instruction.init(string:))
// ------- Part 1 -------
var navigation1 = Navigation1()
input.forEach { navigation1.apply($0) }
let part1 = navigation1.manhattanDistance
print(part1)
// ------- Part 2 -------
var navigation2 = Navigation2()
input.forEach { navigation2.apply($0) }
let part2 = navigation2.manhattanDistance
print(part2)
// ------- Test -------
assert(part1 == 845, "WA")
assert(part2 == 27016, "WA")
}
}