|
|
|
|
@ -21,8 +21,9 @@ struct ElfSquare {
|
|
|
|
|
// lay down all the claims
|
|
|
|
|
// count the overlapping cells
|
|
|
|
|
class Slice {
|
|
|
|
|
var fabric: [[Int]] = [] // var threeDoubles = Array(repeating: 0.0, count: 3)
|
|
|
|
|
var fabric: [[Int]] = []
|
|
|
|
|
var claims: [ElfSquare] = []
|
|
|
|
|
var corruptedClaims: [Int : Bool] = [:] // build list of claims that overlap
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Supply filename for input ex: '/home/peterr/AOC2018/Sources/AOC2018/data/day03.txt'
|
|
|
|
|
@ -63,14 +64,12 @@ class Slice {
|
|
|
|
|
claim.removeFirst()
|
|
|
|
|
claim = claim.replacingOccurrences(of: " ", with: "")
|
|
|
|
|
claim = claim.replacingOccurrences(of: "@", with: ":")
|
|
|
|
|
// print("modified claim : \(claim)")
|
|
|
|
|
var parts = claim.components(separatedBy: ":")
|
|
|
|
|
// print("Parts = \(parts)")
|
|
|
|
|
square.ID = Int(parts[0]) ?? -1
|
|
|
|
|
square.loc = getTuple(from: parts[1], withSeparator: ",")
|
|
|
|
|
square.size = getTuple(from: parts[2], withSeparator: "x")
|
|
|
|
|
claims.append(square)
|
|
|
|
|
// print("square = \(square)")
|
|
|
|
|
corruptedClaims[square.ID] = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ -97,6 +96,10 @@ class Slice {
|
|
|
|
|
for j in claim.loc.fromTop..<(claim.loc.fromTop+claim.size.height) {
|
|
|
|
|
for i in claim.loc.fromLeft..<(claim.loc.fromLeft+claim.size.width) {
|
|
|
|
|
if fabric[j][i] != 0 {
|
|
|
|
|
if fabric[j][i] > 0 {
|
|
|
|
|
corruptedClaims[fabric[j][i]] = true
|
|
|
|
|
}
|
|
|
|
|
corruptedClaims[claim.ID] = true
|
|
|
|
|
fabric[j][i] = -1
|
|
|
|
|
} else {
|
|
|
|
|
fabric[j][i] = claim.ID
|
|
|
|
|
@ -106,7 +109,23 @@ class Slice {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// count the overlapping cells
|
|
|
|
|
func countOverlappingFrames() {
|
|
|
|
|
func countOverlappingClaims() -> Int {
|
|
|
|
|
var retVal = 0
|
|
|
|
|
if fabric.count == 0 {
|
|
|
|
|
_ = getFabricSize()
|
|
|
|
|
}
|
|
|
|
|
for claim in claims {
|
|
|
|
|
apply(claim: claim)
|
|
|
|
|
}
|
|
|
|
|
for row in fabric {
|
|
|
|
|
retVal += row.filter { $0 == -1 }.count
|
|
|
|
|
}
|
|
|
|
|
return retVal
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find the one cell that does not overlap
|
|
|
|
|
func findUncorruptedClaim() -> Int {
|
|
|
|
|
return corruptedClaims.filter{ $0.value == false }.keys.first ?? -1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
@ -156,8 +175,7 @@ class Day03: AOCDay {
|
|
|
|
|
func testGetFabricSize() {
|
|
|
|
|
let slice = Slice(withString: testSample)
|
|
|
|
|
let dims = slice.getFabricSize()
|
|
|
|
|
print("dimensions = \(dims)")
|
|
|
|
|
XCTAssertEqual(test: "testGetFabricSize", withExpression: (dims == (7, 7)))
|
|
|
|
|
XCTAssertEqual(test: "testGetFabricSize", withExpression: (dims == (8, 8)))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// lay down all the claims
|
|
|
|
|
@ -178,6 +196,16 @@ class Day03: AOCDay {
|
|
|
|
|
|
|
|
|
|
// count the overlapping cells
|
|
|
|
|
func testCountOverlappingFrames() {
|
|
|
|
|
let slice = Slice(withString: testSample)
|
|
|
|
|
let overlapArea = slice.countOverlappingClaims()
|
|
|
|
|
XCTAssertEqual(test: "testCountOverlappingFrames", withExpression: (overlapArea == 4))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func testFindUncorruptedClaim() {
|
|
|
|
|
let slice = Slice(withString: testSample)
|
|
|
|
|
_ = slice.countOverlappingClaims()
|
|
|
|
|
let uncorruptedClaimID = slice.findUncorruptedClaim()
|
|
|
|
|
XCTAssertEqual(test: "testFindUncorruptedClaim", withExpression: (uncorruptedClaimID == 3))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func day03Tests() {
|
|
|
|
|
@ -186,11 +214,13 @@ class Day03: AOCDay {
|
|
|
|
|
testGetFabricSize()
|
|
|
|
|
testApplyClaim()
|
|
|
|
|
testCountOverlappingFrames()
|
|
|
|
|
testFindUncorruptedClaim()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func day03Final() {
|
|
|
|
|
let retVal = "None"
|
|
|
|
|
print("Answer to part 1 is: \(retVal)")
|
|
|
|
|
print("Answer to part 2 is: \(retVal)")
|
|
|
|
|
let slice = Slice(withFile: "/home/peterr/AOC2018/Sources/AOC2018/data/day03.txt")
|
|
|
|
|
let overlapArea = slice.countOverlappingClaims()
|
|
|
|
|
print("Answer to part 1 is: \(overlapArea)")
|
|
|
|
|
print("Answer to part 2 is: \(slice.findUncorruptedClaim())")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|