-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay02.swift
37 lines (28 loc) · 851 Bytes
/
Day02.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
import AOCCore
import Foundation
struct Day02: Day {
var title = "Red-Nosed Reports"
var rawInput: String?
func part1() throws -> Int {
let reports = input().lines.map(\.integers)
return reports
.count(where: isSafe)
}
func part2() throws -> Int {
let reports = input().lines.map(\.integers)
return reports
.count { levels in
(0..<levels.count).contains { index in
var new = levels
new.remove(at: index)
return isSafe(levels: new)
}
}
}
private func isSafe(levels: [Int]) -> Bool {
let diff = zip(levels, levels.dropFirst()).map(-)
return
diff.map(\.signum).allSame &&
diff.map(abs).allSatisfy { $0 <= 3 }
}
}