Add Code for Day 14
Day 14: Chocolate Charts (brute forced part 2, not elegant)master
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")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue