-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.swift
55 lines (45 loc) · 1.48 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import Foundation
import Common
enum Command {
case forward(Int)
case down(Int)
case up(Int)
}
let input = Bundle.module.readLines(fromResource: "Input").map { line -> Command in
let parts = line.split(separator: " ")
let value = Int(parts[1])!
switch parts[0] {
case "down":
return .down(value)
case "up":
return .up(value)
default:
return .forward(value)
}
}
typealias State1 = (position: Int, depth: Int)
func reducePartOne(_ state: State1, _ command: Command) -> State1 {
switch command {
case let .forward(x):
return State1(state.position + x, state.depth)
case let .down(y):
return State1(state.position, state.depth + y)
case let .up(y):
return State1(state.position, state.depth - y)
}
}
let partOne = input.reduce(State1(position: 0, depth: 0), reducePartOne)
print("Part one: \(partOne.position * partOne.depth)")
typealias State2 = (position: Int, depth: Int, aim: Int)
func reducePartTwo(_ state: State2, _ command: Command) -> State2 {
switch command {
case let .forward(x):
return State2(state.position + x, state.depth + state.aim * x, state.aim)
case let .down(y):
return State2(state.position, state.depth, state.aim + y)
case let .up(y):
return State2(state.position, state.depth, state.aim - y)
}
}
let partTwo = input.reduce(State2(position: 0, depth: 0, aim: 0), reducePartTwo)
print("Part two: \(partTwo.position * partTwo.depth)")