Add Code for Day 12
Day 12: Subterranean Sustainability - ended up looking for a repeating pattern - then figured out how many generation to repeat - calculated out 50000000000 generationsmaster
parent
439760c9ec
commit
f373431d8e
@ -0,0 +1,175 @@
|
|||||||
|
//
|
||||||
|
// Advent of Code 2018 "Day 12: Subterranean Sustainability"
|
||||||
|
//
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
class Sustainability {
|
||||||
|
var condition = ""
|
||||||
|
var pattern: [String: String] = [:]
|
||||||
|
var keyLength = 0
|
||||||
|
var paddingLength = 0
|
||||||
|
|
||||||
|
// Supply test data in the form of a String
|
||||||
|
init(withString noteList: String, andInitialCondition initial: String, padding pad: Int) {
|
||||||
|
paddingLength = pad
|
||||||
|
let padding = String(Array(repeating: ".", count: paddingLength))
|
||||||
|
condition = padding + initial + padding
|
||||||
|
let noteStrings = noteList.components(separatedBy: "\n")
|
||||||
|
parseData(withNotes: noteStrings)
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseData(withNotes noteArray: [String]) {
|
||||||
|
for oneNote in noteArray {
|
||||||
|
var line = oneNote
|
||||||
|
line = line.replacingOccurrences(of: " => ", with: " ")
|
||||||
|
let data = line.components(separatedBy: " ")
|
||||||
|
guard data.count == 2 else { print("Error"); return }
|
||||||
|
pattern[data[0]] = data[1]
|
||||||
|
keyLength = data[0].count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func nextGeneration(with previous: String) -> String {
|
||||||
|
var nextGen = String(Array(repeating: ".", count: keyLength/2))
|
||||||
|
for i in 0..<previous.count-keyLength {
|
||||||
|
let start = previous.index(previous.startIndex, offsetBy: i)
|
||||||
|
let end = previous.index(previous.startIndex, offsetBy: i + keyLength)
|
||||||
|
let range = start..<end
|
||||||
|
let substr = String(previous[range])
|
||||||
|
if pattern[substr] == nil {
|
||||||
|
nextGen += "."
|
||||||
|
} else {
|
||||||
|
nextGen = nextGen + pattern[substr]!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nextGen += String(Array(repeating: ".", count: keyLength/2+1))
|
||||||
|
return nextGen
|
||||||
|
}
|
||||||
|
|
||||||
|
func getValue(of plants: String) -> Int {
|
||||||
|
var value = 0
|
||||||
|
let start = -paddingLength
|
||||||
|
for i in 0..<plants.count {
|
||||||
|
let pot = start + i
|
||||||
|
value += plants[i] == "#" ? pot : 0
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class Day12: AOCDay {
|
||||||
|
lazy var tests: (() -> ()) = day12Tests
|
||||||
|
lazy var final: (() -> ()) = day12Final
|
||||||
|
|
||||||
|
func testSustainabilityInit() {
|
||||||
|
let _ = Sustainability(withString: testNotes, andInitialCondition: testInitialState, padding: 15)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testNextGeneration() {
|
||||||
|
let sus = Sustainability(withString: testNotes, andInitialCondition: testInitialState, padding: 15)
|
||||||
|
var gen = sus.condition
|
||||||
|
print(" 0", terminator: ": ")
|
||||||
|
print("\(gen)")
|
||||||
|
for i in 1...20 {
|
||||||
|
print(NSString(format:"%2d", i), terminator: ": ")
|
||||||
|
gen = sus.nextGeneration(with: gen)
|
||||||
|
print("\(gen)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testGetVaue() {
|
||||||
|
let sus = Sustainability(withString: testNotes, andInitialCondition: testInitialState, padding: 15)
|
||||||
|
var gen = sus.condition
|
||||||
|
for _ in 1...20 {
|
||||||
|
gen = sus.nextGeneration(with: gen)
|
||||||
|
}
|
||||||
|
let value = sus.getValue(of: gen)
|
||||||
|
XCTAssertEqual(test: "testGetVaue", withExpression: (value == 325))
|
||||||
|
}
|
||||||
|
|
||||||
|
func day12Tests() {
|
||||||
|
testSustainabilityInit()
|
||||||
|
testNextGeneration()
|
||||||
|
testGetVaue()
|
||||||
|
}
|
||||||
|
|
||||||
|
func day12Final() {
|
||||||
|
let sus = Sustainability(withString: finalNotes, andInitialCondition: finalInitialState, padding: 200)
|
||||||
|
var gen = sus.condition
|
||||||
|
for _ in 1...20 {
|
||||||
|
gen = sus.nextGeneration(with: gen)
|
||||||
|
}
|
||||||
|
let value = sus.getValue(of: gen)
|
||||||
|
print("Answer to part 1 is: \(value)")
|
||||||
|
|
||||||
|
|
||||||
|
let limitCount = 150
|
||||||
|
var inc = 0 //40
|
||||||
|
gen = sus.condition
|
||||||
|
for _ in 1...limitCount {
|
||||||
|
let preValue = sus.getValue(of: gen)
|
||||||
|
gen = sus.nextGeneration(with: gen)
|
||||||
|
let postValue = sus.getValue(of: gen)
|
||||||
|
inc = postValue - preValue
|
||||||
|
}
|
||||||
|
let valLimit = sus.getValue(of: gen)
|
||||||
|
let generations = 50000000000 - limitCount
|
||||||
|
let answer = generations * inc + valLimit
|
||||||
|
print("Answer to part 2 is: \(answer)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var testInitialState = "#..#.#..##......###...###"
|
||||||
|
var testNotes = """
|
||||||
|
...## => #
|
||||||
|
..#.. => #
|
||||||
|
.#... => #
|
||||||
|
.#.#. => #
|
||||||
|
.#.## => #
|
||||||
|
.##.. => #
|
||||||
|
.#### => #
|
||||||
|
#.#.# => #
|
||||||
|
#.### => #
|
||||||
|
##.#. => #
|
||||||
|
##.## => #
|
||||||
|
###.. => #
|
||||||
|
###.# => #
|
||||||
|
####. => #
|
||||||
|
"""
|
||||||
|
|
||||||
|
var finalInitialState = "##.......#.######.##..#...#.#.#..#...#..####..#.##...#....#...##..#..#.##.##.###.##.#.......###....#"
|
||||||
|
var finalNotes = """
|
||||||
|
.#### => .
|
||||||
|
....# => .
|
||||||
|
###.. => .
|
||||||
|
..#.# => .
|
||||||
|
##### => #
|
||||||
|
####. => .
|
||||||
|
#.##. => #
|
||||||
|
#.#.# => .
|
||||||
|
##.#. => #
|
||||||
|
.###. => .
|
||||||
|
#..#. => #
|
||||||
|
###.# => .
|
||||||
|
#.### => .
|
||||||
|
##... => #
|
||||||
|
.#.## => .
|
||||||
|
..#.. => .
|
||||||
|
#...# => #
|
||||||
|
..... => .
|
||||||
|
.##.. => .
|
||||||
|
...#. => .
|
||||||
|
#.#.. => .
|
||||||
|
.#..# => #
|
||||||
|
.#.#. => .
|
||||||
|
.#... => #
|
||||||
|
..##. => .
|
||||||
|
#..## => .
|
||||||
|
##.## => #
|
||||||
|
...## => #
|
||||||
|
..### => #
|
||||||
|
#.... => .
|
||||||
|
.##.# => #
|
||||||
|
##..# => #
|
||||||
|
"""
|
||||||
Loading…
Reference in New Issue