Add Code for Day 14

Day 14: Chocolate Charts
(brute forced part 2, not elegant)
master
Peter 7 years ago
parent d511a8cd65
commit 14c93b8482

@ -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..<elf.count {
let currentRecipe = elf[index]
let moveCount = score[elf[index]] + 1
elf[index] = moveCount + currentRecipe
if elf[index] >= score.count {
elf[index] -= score.count
}
}
}
func nextRecipe() {
var sum = 0
for index in 0..<elf.count {
sum += score[elf[index]]
}
let strsum = Array(String(sum))
for i in 0..<strsum.count {
let newScore = String(strsum[i])
score.append(Int(newScore)!)
scoreStr += newScore
}
}
func get10RecipeScore(after index: Int) -> String {
var retVal = ""
repeat {
nextRecipe()
moveForward()
} while score.count < index+10
for i in index..<index+10 {
retVal += String(score[i])
}
return retVal
}
func getNumBefore(pattern: String) -> 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")
}
}

@ -5,7 +5,7 @@
import Foundation import Foundation
let showTests = true let showTests = true
let onlyOneDay = 13 let onlyOneDay = 14
var allTests: [(() -> ())] = [] var allTests: [(() -> ())] = []
var allFinal: [(() -> ())] = [] var allFinal: [(() -> ())] = []
@ -25,6 +25,7 @@ allTests.append(Day10().tests)
allTests.append(Day11().tests) allTests.append(Day11().tests)
allTests.append(Day12().tests) allTests.append(Day12().tests)
allTests.append(Day13().tests) allTests.append(Day13().tests)
allTests.append(Day14().tests)
// Compile list of Answers // Compile list of Answers
allFinal.append(Day01().final) allFinal.append(Day01().final)
@ -40,6 +41,7 @@ allFinal.append(Day10().final)
allFinal.append(Day11().final) allFinal.append(Day11().final)
allFinal.append(Day12().final) allFinal.append(Day12().final)
allFinal.append(Day13().final) allFinal.append(Day13().final)
allFinal.append(Day14().final)
if onlyOneDay > 0 { if onlyOneDay > 0 {
print("\nDay \(onlyOneDay)") print("\nDay \(onlyOneDay)")

Loading…
Cancel
Save