diff --git a/Sources/AOC2018/day14.swift b/Sources/AOC2018/day14.swift new file mode 100644 index 0000000..e5817bb --- /dev/null +++ b/Sources/AOC2018/day14.swift @@ -0,0 +1,125 @@ +// +// Advent of Code 2018 "Day 14: Chocolate Charts" +// + +import Foundation + +class Chocolate { + var score: [Int] = [3, 7, 1, 0] + var scoreStr = "3710" + var elf: [Int] = [0, 1] + var lf2 = 3 + + func moveForward() { + for index in 0..= score.count { + elf[index] -= score.count + } + } + } + + func nextRecipe() { + var sum = 0 + for index in 0.. String { + var retVal = "" + repeat { + nextRecipe() + moveForward() + } while score.count < index+10 + for i in index.. Int { + var distance = 0 + let range = scoreStr.range(of: pattern) + if let range = range { + distance = scoreStr.distance(from: scoreStr.startIndex, to: range.lowerBound) + } + return distance + } + +} + +class Day14: AOCDay { + lazy var tests: (() -> ()) = day14Tests + lazy var final: (() -> ()) = day14Final + + func testNextRecipe() { + let cho = Chocolate() + cho.nextRecipe() + guard cho.score.count == 6 else { + XCTAssertEqual(test: "testNextRecipe fail count", withExpression: (false)) + return + } + XCTAssertEqual(test: "testNextRecipe", withExpression: (cho.score[4] == 1 && cho.score[5] == 0)) + } + + func testMoveForward() { + let cho = Chocolate() + cho.nextRecipe() + cho.moveForward() + XCTAssertEqual(test: "testMoveForward", withExpression: (cho.elf[0] == 4 && cho.elf[1] == 3)) + } + + func testGet10RecipeScore() { + var cho = Chocolate() + var answer = cho.get10RecipeScore(after: 9) + XCTAssertEqual(test: "testGet10RecipeScore 9", withExpression: (answer == "5158916779")) + cho = Chocolate() + answer = cho.get10RecipeScore(after: 5) + XCTAssertEqual(test: "testGet10RecipeScore 5", withExpression: (answer == "0124515891")) + cho = Chocolate() + answer = cho.get10RecipeScore(after: 18) + XCTAssertEqual(test: "testGet10RecipeScore 18", withExpression: (answer == "9251071085")) + cho = Chocolate() + answer = cho.get10RecipeScore(after: 2018) + XCTAssertEqual(test: "testGet10RecipeScore 2018", withExpression: (answer == "5941429882")) + } + + func testGetNumBefore() { + let cho = Chocolate() + _ = cho.get10RecipeScore(after: 2018) + var answer = cho.getNumBefore(pattern: "51589") + XCTAssertEqual(test: "testGetNumBefore 51589", withExpression: (answer == 9)) + answer = cho.getNumBefore(pattern: "01245") + XCTAssertEqual(test: "testGetNumBefore 01245", withExpression: (answer == 5)) + answer = cho.getNumBefore(pattern: "92510") + XCTAssertEqual(test: "testGetNumBefore 92510", withExpression: (answer == 18)) + answer = cho.getNumBefore(pattern: "59414") + XCTAssertEqual(test: "testGetNumBefore 59414", withExpression: (answer == 2018)) + } + + func day14Tests() { + testNextRecipe() + testMoveForward() + testGet10RecipeScore() + testGetNumBefore() + } + + func day14Final() { + let cho = Chocolate() + let answer = cho.get10RecipeScore(after: 580741) + print("Answer to part 1 is: \(answer)") + // let answer = cho.get10RecipeScore(after: 112580741) + // let dist = cho.getNumBefore(pattern: "580741") + // print("Answer to part 2 is: \(dist)") + print("Brute forced part 2 - takes too long") + } +} diff --git a/Sources/AOC2018/main.swift b/Sources/AOC2018/main.swift index 3580578..a20fafe 100644 --- a/Sources/AOC2018/main.swift +++ b/Sources/AOC2018/main.swift @@ -5,7 +5,7 @@ import Foundation let showTests = true -let onlyOneDay = 13 +let onlyOneDay = 14 var allTests: [(() -> ())] = [] var allFinal: [(() -> ())] = [] @@ -25,6 +25,7 @@ allTests.append(Day10().tests) allTests.append(Day11().tests) allTests.append(Day12().tests) allTests.append(Day13().tests) +allTests.append(Day14().tests) // Compile list of Answers allFinal.append(Day01().final) @@ -40,6 +41,7 @@ allFinal.append(Day10().final) allFinal.append(Day11().final) allFinal.append(Day12().final) allFinal.append(Day13().final) +allFinal.append(Day14().final) if onlyOneDay > 0 { print("\nDay \(onlyOneDay)")