-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.swift
73 lines (68 loc) · 2.06 KB
/
15.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
import Foundation
var sensors: [[Int]] = []
while let line = readLine() { sensors.append(line.numbers) }
var target2m: [Interval]
var beacons: Int
(target2m, beacons) = parse(2000000)
target2m.sort {$1.start > $0.start}
let merged2m = merge(target2m)
for y in 0...40000000 {
var targety: [Interval]
(targety, _) = parse(y)
targety.sort {$1.start > $0.start}
let merged = merge(targety)
if merged.count > 1 {
print(merged2m.first!.end - merged2m.first!.start + 1 - beacons,
(merged.first!.end + 1) * 4000000 + y)
break
}
}
func parse(_ targety: Int) -> ([Interval], Int) {
var result: [Interval] = []
var beacons: Set<Int> = []
for s in sensors {
let sx = s[0], sy = s[1], bx = s[2], by = s[3]
let distance = abs(bx - sx) + abs(by - sy)
let offset = distance - abs(sy - targety)
if offset >= 0 {
result.append(Interval(sx - offset, sx + offset))
if by == targety {
beacons.insert(bx)
}
}
}
return (result, beacons.count)
}
func merge(_ intervals: [Interval]) -> [Interval] {
var merged: [Interval] = []
for interval in intervals {
if merged.isEmpty {
merged.append(interval)
continue
}
let i = merged.first!
if interval.start > i.end + 1 {
merged.append(Interval(interval.start, interval.end))
} else {
merged[0] = Interval(i.start, max(i.end, interval.end))
}
}
return merged
}
struct Interval {
var start, end: Int
init(_ s: Int, _ e: Int) { self.start = s; self.end = e }
}
extension String {
var numbers: [Int] {
guard let regex = try? NSRegularExpression(pattern: "-?\\d+", options: [.caseInsensitive]) else {
return []
}
let matches = regex.matches(in: self, options: [], range: NSMakeRange(0, self.count))
return matches.map { match in
String(self[Range(match.range, in: self)!])
}.map {
Int($0)!
}
}
}