-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
50 lines (41 loc) · 1.43 KB
/
Contents.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
//: [Previous](@previous)
import Foundation
let input: [String] = try!
String(
contentsOf: Bundle.main.url(forResource: "input", withExtension: "txt")!,
encoding: .utf8
)
.components(separatedBy: .newlines)
// coulda used GCRect but that converts to GCFloat
typealias Claim = (ID: Int, x: Int, y: Int, width: Int, height: Int)
let claims = input
.map{
$0.components(separatedBy: CharacterSet(charactersIn: "#@,x: ")).compactMap(Int.init)
}
.filter { $0.count >= 5 }
.map {
Claim(ID: $0[0], x: $0[1], y: $0[2], width: $0[3], height: $0[4])
}
let gridSize = claims.map { max($0.width + $0.x, $0.height + $0.y) }.reduce(0, max) + 1
var grid: [[Int]] = Array(repeating: Array(repeating: 0, count: gridSize), count: gridSize)
var intersecting = Array(repeating: false, count: claims.count + 1)
for c in claims {
for x in c.x..<c.x+c.width {
for y in c.y..<c.y+c.height {
// 0 = not claimed; 0< = claimed once
let val = grid[x][y]
if val == 0 {
grid[x][y] = c.ID
continue
} else if val > 0 {
intersecting[val] = true
}
intersecting[c.ID] = true
}
}
}
let p1 = grid.map { $0.filter { $0 > 1 }.count }.reduce(0, +)
let p2 = intersecting[1...].enumerated().first{ _,v in !v }!.offset + 1
print("Part 1", p1)
print("Part 2", p2)
//: [Next](@next)