parent
9802ed1f35
commit
72bafa3d08
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,196 @@
|
||||
//
|
||||
// Advent of Code 2018 "Day 3: No Matter How You Slice It"
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct ElfSquare {
|
||||
var ID = 0
|
||||
var loc = (fromLeft: 0, fromTop: 0)
|
||||
var size = (width: 0, height: 0)
|
||||
}
|
||||
|
||||
// We know:
|
||||
// - the number of claims
|
||||
// - the size and position of each claim
|
||||
// - the claim size and positions are all integers => using array cells to solve
|
||||
|
||||
// Read in data
|
||||
// parse data
|
||||
// find out the size of the sheet
|
||||
// lay down all the claims
|
||||
// count the overlapping cells
|
||||
class Slice {
|
||||
var fabric: [[Int]] = [] // var threeDoubles = Array(repeating: 0.0, count: 3)
|
||||
var claims: [ElfSquare] = []
|
||||
|
||||
|
||||
// Supply filename for input ex: '/home/peterr/AOC2018/Sources/AOC2018/data/day03.txt'
|
||||
init(withFile filename: String) {
|
||||
let slices = Tools.readFile(fromPath: filename)
|
||||
var sliceArray = slices.components(separatedBy: "\n")
|
||||
// This gurd statement is just an excuse to use guard
|
||||
// I'm assuming the last string is an empty string, if not DON'T remove the last string
|
||||
guard let lastStr = sliceArray.last, lastStr.count == 0 else { return }
|
||||
sliceArray.removeLast() // empty string
|
||||
parseData(withSlices: sliceArray)
|
||||
}
|
||||
|
||||
// Supply test data in the form of a String
|
||||
init(withString slices: String) {
|
||||
let sliceArray = slices.components(separatedBy: "\n")
|
||||
parseData(withSlices: sliceArray)
|
||||
}
|
||||
|
||||
func parseData(withSlices sliceArray: [String]) {
|
||||
|
||||
func getTuple(from tupleString: String, withSeparator sep: String) -> (Int, Int) {
|
||||
var retVal = (-1, -1)
|
||||
let elements = tupleString.components(separatedBy: sep)
|
||||
if (elements.count > 1) {
|
||||
let first = Int(elements[0]) ?? -1
|
||||
let second = Int(elements[1]) ?? -1
|
||||
retVal = (first, second)
|
||||
}
|
||||
return retVal
|
||||
}
|
||||
|
||||
// #1 @ 1,3: 4x4
|
||||
for line in sliceArray {
|
||||
var claim = line
|
||||
var square = ElfSquare()
|
||||
// remove all spaces and convert delimeters to ':'
|
||||
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)")
|
||||
}
|
||||
}
|
||||
|
||||
// find out the size of the sheet
|
||||
func getFabricSize() -> (width: Int, height: Int) {
|
||||
var retVal = (-1, -1)
|
||||
var maxHeight = 0
|
||||
var maxWidth = 0
|
||||
for claim in claims {
|
||||
let height = claim.loc.fromTop + claim.size.height
|
||||
let width = claim.loc.fromLeft + claim.size.width
|
||||
maxHeight = max(maxHeight, height)
|
||||
maxWidth = max(maxWidth, width)
|
||||
}
|
||||
maxHeight += 1
|
||||
maxWidth += 1
|
||||
retVal = (maxWidth, maxHeight)
|
||||
fabric = Array(repeating: Array(repeating: 0, count: maxWidth), count: maxHeight)
|
||||
return retVal
|
||||
}
|
||||
|
||||
// lay down all the claims
|
||||
func apply(claim: ElfSquare) {
|
||||
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 {
|
||||
fabric[j][i] = -1
|
||||
} else {
|
||||
fabric[j][i] = claim.ID
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// count the overlapping cells
|
||||
func countOverlappingFrames() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Day03: AOCDay {
|
||||
lazy var tests: (() -> ()) = day03Tests
|
||||
lazy var final: (() -> ()) = day03Final
|
||||
|
||||
let testSample = """
|
||||
#1 @ 1,3: 4x4
|
||||
#2 @ 3,1: 4x4
|
||||
#3 @ 5,5: 2x2
|
||||
"""
|
||||
|
||||
func testDisplaySample(fabric: [[Int]]) {
|
||||
for j in 0..<fabric.count {
|
||||
for i in 0..<fabric[j].count {
|
||||
if fabric[j][i] == 0 {
|
||||
print(".", terminator: " ")
|
||||
} else if fabric[j][i] == -1 {
|
||||
print("X", terminator: " ")
|
||||
} else {
|
||||
print("\(fabric[j][i])", terminator: " ")
|
||||
}
|
||||
}
|
||||
print("")
|
||||
}
|
||||
}
|
||||
|
||||
// Read in data
|
||||
func testSliceInitWithString() {
|
||||
let slice = Slice(withString: testSample)
|
||||
XCTAssertEqual(test: "testSliceInitWithString [1].ID", withExpression: (slice.claims[1].ID == 2))
|
||||
XCTAssertEqual(test: "testSliceInitWithString [1].loc", withExpression: (slice.claims[1].loc == (3,1)))
|
||||
XCTAssertEqual(test: "testSliceInitWithString [1].size", withExpression: (slice.claims[1].size == (4, 4)))
|
||||
}
|
||||
|
||||
// Read in data
|
||||
func testSliceInitWithFile() {
|
||||
let slice = Slice(withFile: "/home/peterr/AOC2018/Sources/AOC2018/data/day03.txt")
|
||||
XCTAssertEqual(test: "testSliceInitWithFile [699].ID", withExpression: (slice.claims[699].ID == 700))
|
||||
XCTAssertEqual(test: "testSliceInitWithFile [700].loc", withExpression: (slice.claims[700].loc == (821, 673)))
|
||||
XCTAssertEqual(test: "testSliceInitWithFile [701].size", withExpression: (slice.claims[701].size == (13, 20)))
|
||||
}
|
||||
|
||||
// find out the size of the sheet
|
||||
func testGetFabricSize() {
|
||||
let slice = Slice(withString: testSample)
|
||||
let dims = slice.getFabricSize()
|
||||
print("dimensions = \(dims)")
|
||||
XCTAssertEqual(test: "testGetFabricSize", withExpression: (dims == (7, 7)))
|
||||
}
|
||||
|
||||
// lay down all the claims
|
||||
func testApplyClaim() {
|
||||
let slice = Slice(withString: testSample)
|
||||
_ = slice.getFabricSize()
|
||||
testDisplaySample(fabric: slice.fabric)
|
||||
slice.apply(claim: slice.claims[0])
|
||||
print("")
|
||||
testDisplaySample(fabric: slice.fabric)
|
||||
slice.apply(claim: slice.claims[1])
|
||||
print("")
|
||||
testDisplaySample(fabric: slice.fabric)
|
||||
slice.apply(claim: slice.claims[2])
|
||||
print("")
|
||||
testDisplaySample(fabric: slice.fabric)
|
||||
}
|
||||
|
||||
// count the overlapping cells
|
||||
func testCountOverlappingFrames() {
|
||||
}
|
||||
|
||||
func day03Tests() {
|
||||
testSliceInitWithString()
|
||||
testSliceInitWithFile()
|
||||
testGetFabricSize()
|
||||
testApplyClaim()
|
||||
testCountOverlappingFrames()
|
||||
}
|
||||
|
||||
func day03Final() {
|
||||
let retVal = "None"
|
||||
print("Answer to part 1 is: \(retVal)")
|
||||
print("Answer to part 2 is: \(retVal)")
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue