Finish Day 3

master
Peter 7 years ago
parent 72bafa3d08
commit 538c76c1eb

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

@ -5,7 +5,7 @@
import Foundation import Foundation
let showTests = true let showTests = true
let onlyOneDay = 3 let onlyOneDay = 0
var allTests: [(() -> ())] = [] var allTests: [(() -> ())] = []
var allFinal: [(() -> ())] = [] var allFinal: [(() -> ())] = []

Loading…
Cancel
Save