From 9c6493b800a1638780ac38e1672dbe51ddafc02a Mon Sep 17 00:00:00 2001 From: Peter Date: Sat, 1 Dec 2018 22:23:45 -0600 Subject: [PATCH] Day 1 completion Day 1: Chronal Calibration --- Sources/AOC2018/data/day01.txt | 990 +++++++++++++++++++++++++++++++++ Sources/AOC2018/day01.swift | 139 ++++- 2 files changed, 1126 insertions(+), 3 deletions(-) create mode 100644 Sources/AOC2018/data/day01.txt diff --git a/Sources/AOC2018/data/day01.txt b/Sources/AOC2018/data/day01.txt new file mode 100644 index 0000000..f325281 --- /dev/null +++ b/Sources/AOC2018/data/day01.txt @@ -0,0 +1,990 @@ ++13 +-16 ++18 ++11 ++15 ++10 +-4 +-19 ++14 +-19 +-17 +-19 +-2 ++6 ++2 ++21 ++5 ++12 +-10 +-10 ++18 +-11 ++2 ++17 +-5 ++14 ++14 +-3 +-12 ++10 ++7 +-3 ++6 +-2 ++8 +-17 +-18 +-19 +-14 ++9 +-16 +-1 +-1 ++11 +-3 ++8 +-12 ++10 ++14 ++12 +-18 ++19 +-5 ++13 ++1 ++4 ++2 ++11 +-3 +-2 ++18 ++8 +-4 ++15 +-7 +-3 ++18 ++16 ++2 +-4 +-12 +-19 +-4 ++12 ++15 +-17 ++1 +-16 +-5 ++15 ++26 ++9 ++9 +-3 +-5 ++3 ++3 +-11 +-8 +-11 +-16 ++14 ++14 ++14 ++17 ++14 ++11 +-9 ++18 +-10 +-4 +-11 +-10 +-9 ++8 ++12 +-6 +-1 +-2 ++12 +-17 +-1 ++17 +-10 ++9 ++11 ++6 ++3 ++18 ++19 ++11 +-3 ++15 +-3 +-2 ++18 ++17 ++13 ++13 +-6 ++16 ++10 ++7 +-6 +-15 +-15 +-1 ++15 ++6 +-2 +-12 ++19 +-1 ++17 ++18 +-8 +-11 +-18 ++12 +-3 +-14 ++6 +-12 ++1 +-11 +-9 ++13 +-3 +-15 +-19 +-16 +-3 ++7 ++7 +-17 +-18 ++3 +-2 +-12 +-1 ++16 ++15 ++10 ++7 +-13 ++7 ++13 ++14 ++5 ++18 +-14 +-8 +-4 ++15 +-20 +-17 +-17 ++11 +-15 +-14 +-10 +-1 ++5 +-11 ++18 +-4 +-19 ++15 +-8 ++14 +-20 +-12 ++10 +-11 +-12 ++16 +-7 +-7 ++4 +-18 +-8 ++11 ++13 +-6 ++24 ++8 ++4 +-19 ++11 ++15 ++3 +-17 ++30 ++9 ++10 ++6 +-2 ++3 ++14 ++8 +-3 +-12 ++1 ++5 +-8 ++11 ++10 ++16 ++6 ++16 ++1 ++21 ++19 ++18 ++18 ++15 ++15 ++8 ++2 +-6 +-16 ++5 +-10 ++18 +-12 ++16 ++6 ++18 +-6 +-2 ++5 +-14 +-17 +-14 +-13 +-7 ++16 +-8 +-11 +-3 ++8 +-13 ++6 ++3 +-5 ++6 ++5 ++1 ++12 ++14 ++10 ++12 +-20 ++12 +-15 ++8 ++4 +-7 +-9 +-1 ++11 +-22 +-4 +-14 +-4 +-18 ++6 ++2 ++8 +-4 +-1 ++16 +-17 +-1 +-2 +-10 +-10 +-10 ++16 ++3 +-12 ++2 +-1 ++4 +-15 ++18 +-2 +-9 +-6 +-22 ++7 ++16 +-5 ++17 ++13 ++2 +-1 ++3 +-11 ++10 +-25 +-14 ++17 +-15 ++3 ++43 ++19 ++12 ++1 +-8 ++4 ++8 +-11 +-40 +-29 +-30 +-12 ++18 +-15 +-5 +-14 ++10 +-16 ++1 +-34 +-13 +-20 +-7 ++12 ++9 ++16 +-2 +-2 ++9 ++23 ++15 +-8 +-38 +-37 +-8 +-20 ++11 +-24 +-2 ++14 +-24 +-18 ++2 +-7 ++14 ++1 ++19 +-5 +-23 +-11 +-15 ++23 ++8 +-14 ++16 +-25 +-19 ++8 +-3 +-20 ++11 +-15 +-11 +-14 ++16 +-31 ++18 ++25 ++20 +-6 ++11 ++48 +-7 +-18 +-2 +-31 ++21 +-49 ++50 +-20 +-44 ++8 +-81 ++24 +-16 +-18 ++26 +-60 ++19 +-36 +-14 +-41 ++25 +-72 +-46 ++1 +-46 +-61361 ++12 +-18 +-7 +-9 ++6 ++14 +-13 +-8 ++19 ++19 ++11 ++9 ++2 ++9 +-18 +-3 ++20 +-4 +-11 ++4 ++14 ++3 +-11 ++2 ++2 +-13 +-17 ++3 ++19 +-13 +-16 ++11 ++14 +-5 ++12 ++16 ++17 ++15 ++6 ++6 ++2 +-1 ++15 ++6 +-3 +-5 +-6 ++20 ++16 ++7 ++1 +-5 ++9 ++12 +-2 ++11 +-16 ++6 +-14 +-3 +-11 +-8 +-18 +-9 ++2 ++12 +-15 ++20 ++1 ++1 ++10 ++5 ++17 ++7 +-2 +-14 ++1 ++10 +-1 ++12 ++12 +-37 +-13 +-3 ++5 ++3 ++18 +-14 +-26 ++11 +-14 +-3 +-14 +-6 +-21 ++2 +-8 +-3 ++18 +-3 ++18 +-10 +-15 +-6 ++10 ++5 ++17 ++14 +-16 +-10 ++6 +-2 +-1 +-6 +-12 ++14 +-5 +-11 +-11 ++7 +-6 ++9 ++20 +-11 +-21 ++14 +-7 +-8 +-29 +-12 +-2 +-3 +-17 ++9 +-7 ++13 ++18 +-9 +-11 ++12 ++11 +-1 +-24 +-7 +-14 +-13 +-18 ++9 +-16 ++19 ++18 ++16 ++2 +-13 +-19 ++7 +-19 +-1 +-8 +-16 ++2 ++15 +-16 ++18 ++13 +-19 +-3 +-2 +-4 +-14 ++9 ++10 +-14 +-15 ++6 ++8 +-12 +-11 +-3 +-12 +-4 +-18 ++14 +-13 +-10 +-14 ++15 ++3 +-15 ++2 ++1 +-9 +-11 +-1 +-17 ++16 +-5 +-2 ++19 +-8 +-19 +-3 +-19 ++12 +-17 ++6 ++16 ++19 ++4 +-5 +-4 +-11 ++13 ++15 ++7 +-11 ++16 ++4 +-1 ++18 +-15 ++20 +-11 ++8 +-13 ++19 ++7 ++11 ++19 +-1 +-7 +-17 +-2 +-19 +-11 ++15 +-11 +-16 ++15 +-10 ++25 +-5 ++9 ++17 +-16 ++12 ++10 ++5 +-2 +-17 ++11 ++16 +-4 +-3 +-4 +-4 +-3 +-7 ++4 +-9 +-19 ++1 +-5 ++29 ++2 ++28 ++15 ++20 +-15 +-13 ++20 ++20 ++3 +-10 +-11 ++20 ++5 ++14 ++6 +-18 +-13 ++1 ++15 ++30 +-23 ++28 +-7 +-10 +-19 +-21 ++20 +-5 ++12 +-9 +-10 +-9 +-15 +-23 +-1 ++17 +-2 +-10 +-1 +-13 ++28 ++22 +-6 +-8 ++43 ++42 +-8 ++27 ++4 +-18 ++49 ++19 +-8 ++9 +-2 +-1 ++37 +-12 +-11 ++17 ++15 +-6 ++24 ++19 ++6 +-74 ++6 ++26 +-317 ++4 +-23 +-9 +-17 +-17 ++13 +-19 ++8 ++14 +-6 +-6 ++1 ++10 ++10 +-1 ++9 +-7 ++18 +-9 +-8 +-4 ++7 ++18 +-3 +-16 ++18 +-5 ++11 ++12 +-1 ++9 ++11 +-14 +-9 ++19 ++24 ++30 +-27 ++76 +-35 +-34 +-82 ++14 +-25 +-10 +-3 +-7 +-14 ++16 +-4 +-2 +-5 +-18 ++9 +-7 +-1 ++6 +-15 ++11 ++18 +-4 ++16 +-9 ++11 +-16 +-25 +-11 ++21 ++6 ++5 +-10 ++7 ++20 ++22 +-10 ++4 +-10 +-16 +-6 +-18 +-31 +-69 +-12 +-18 +-25 +-8 ++49 ++50 +-19 ++60 +-16 +-96 ++200 ++34 +-148 +-153 +-45 +-1 +-45 +-61788 +-16 +-5 +-8 +-6 ++3 +-6 ++16 ++18 ++10 +-14 ++9 +-20 ++6 ++18 ++9 ++13 ++7 ++11 +-5 +-11 ++17 ++13 +-4 +-4 +-3 +-11 +-17 +-2 ++4 ++9 +-15 +-3 ++19 +-9 +-12 +-17 ++5 +-14 ++17 +-7 +-23 +-11 ++3 +-13 ++4 ++19 +-14 +-17 ++10 ++1 ++4 +-11 ++9 +-17 ++7 +-16 ++12 +-8 ++1 +-9 +-9 +-7 +-7 ++1 ++1 ++4 ++2 +-16 ++7 ++4 +-18 +-7 ++12 ++7 +-1 +-14 ++9 +-17 +-8 ++13 ++19 +-18 +-17 +-14 +-10 +-9 +-12 +-19 +-17 ++14 +-16 ++18 +-15 +-14 +-17 +-9 +-14 +-15 +-19 +-19 ++10 +-17 +-2 ++11 ++15 ++9 ++1 ++17 ++17 +-10 +-16 ++15 ++4 +-7 ++10 ++5 +-16 +-5 +-5 +-3 +-8 ++17 +-5 ++124726 diff --git a/Sources/AOC2018/day01.swift b/Sources/AOC2018/day01.swift index 291dad0..a908bd6 100644 --- a/Sources/AOC2018/day01.swift +++ b/Sources/AOC2018/day01.swift @@ -5,18 +5,151 @@ import Foundation class Chronal { + fileprivate var adjustments: [Int] = [] + let startingFreq = 0 + + // Supply filename for input ex: '/home/peterr/AOC2018/Sources/AOC2018/data/day01.txt' + init(withFile filename: String) { + let adjString = Tools.readFile(fromPath: filename) + var adjustmentArray: [String] = [] + adjustmentArray = adjString.components(separatedBy: "\n") + adjustments = [] + for val in adjustmentArray { + if let intVal = Int(val) { + adjustments.append(intVal) + } + } + } + + // Supply test data in the form of a String + init(withString adjString: String) { + var adjustmentArray: [String] = [] + adjustmentArray = adjString.components(separatedBy: ", ") + adjustments = [] + for val in adjustmentArray { + if let intVal = Int(val) { + adjustments.append(intVal) + } + } + } + + // Apply all the adjustments that this class was initialized with + // Return the resulting frequency as an Optional (nil => no adjustments) + func applyAdjustments() -> Int? { + var retVal: Int? + if adjustments.count > 0 { + var sum = 0 + for val in adjustments { + sum += val + } + retVal = sum + } + return retVal + } + + func repeatedAjustmentUntilTwice() -> Int { + var retVal = -1 + if adjustments.count > 0 { + var usedFreqs = [Int: Int]() + var sum = 0 + usedFreqs[sum] = 1 + var noRepeats = true + repeat { + for val in adjustments { + sum += val + if usedFreqs[sum] != nil { + retVal = sum + noRepeats = false + break + } else { + usedFreqs[sum] = 1 + } + } + } while noRepeats + } + return retVal + } } class Day01: AOCDay { lazy var tests: (() -> ()) = day01Tests lazy var final: (() -> ()) = day01Final + func testInitWithFile() { + let chrona = Chronal(withFile: "/home/peterr/AOC2018/Sources/AOC2018/data/day01.txt") + XCTAssertEqual(test: "testInitWithFile", withExpression: (chrona.adjustments.count == 990)) + if chrona.adjustments.count == 990 { + XCTAssertEqual(test: "testInitWithFile index[10]", withExpression: (chrona.adjustments[10] == -17)) + XCTAssertEqual(test: "testInitWithFile index[989]", withExpression: (chrona.adjustments[989] == 124726)) + } + } + + // +1, +1, +1 results in 3 + // +1, +1, -2 results in 0 + // -1, -2, -3 results in -6 + func testInitWithString() { + var chrona = Chronal(withString: "+1, +1, +1") + XCTAssertEqual(test: "testInitWithString", withExpression: (chrona.adjustments.count == 3)) + if chrona.adjustments.count == 3 { + XCTAssertEqual(test: "testInitWithString index[1]", withExpression: (chrona.adjustments[1] == 1)) + } + chrona = Chronal(withString: "+1, +1, -2") + XCTAssertEqual(test: "testInitWithString", withExpression: (chrona.adjustments.count == 3)) + if chrona.adjustments.count == 3 { + XCTAssertEqual(test: "testInitWithString index[1]", withExpression: (chrona.adjustments[1] == 1)) + } + chrona = Chronal(withString: "-1, -2, -3") + XCTAssertEqual(test: "testInitWithString", withExpression: (chrona.adjustments.count == 3)) + if chrona.adjustments.count == 3 { + XCTAssertEqual(test: "testInitWithString index[1]", withExpression: (chrona.adjustments[1] == -2)) + } + } + + func testApplyAdjustments() { + var chrona = Chronal(withString: "+1, +1, +1") + var val = chrona.applyAdjustments() + XCTAssertEqual(test: "testApplyAdjustments", withExpression: (val == 3)) + chrona = Chronal(withString: "+1, +1, -2") + val = chrona.applyAdjustments() + XCTAssertEqual(test: "testApplyAdjustments", withExpression: (val == 0)) + chrona = Chronal(withString: "-1, -2, -3") + val = chrona.applyAdjustments() + XCTAssertEqual(test: "testApplyAdjustments", withExpression: (val == -6)) + } + + // +1, -1 first reaches 0 twice. + // +3, +3, +4, -2, -4 first reaches 10 twice. + // -6, +3, +8, +5, -6 first reaches 5 twice. + // +7, +7, -2, -7, -4 first reaches 14 twice. + + func testRepeatedAjustmentUntilTwice() { + var chrona = Chronal(withString: "+1, -1") + var val = chrona.repeatedAjustmentUntilTwice() + XCTAssertEqual(test: "testRepeatedAjustmentUntilTwice", withExpression: (val == 0)) + chrona = Chronal(withString: "+3, +3, +4, -2, -4") + val = chrona.repeatedAjustmentUntilTwice() + XCTAssertEqual(test: "testRepeatedAjustmentUntilTwice", withExpression: (val == 10)) + chrona = Chronal(withString: "-6, +3, +8, +5, -6") + val = chrona.repeatedAjustmentUntilTwice() + XCTAssertEqual(test: "testRepeatedAjustmentUntilTwice", withExpression: (val == 5)) + chrona = Chronal(withString: "+7, +7, -2, -7, -4") + val = chrona.repeatedAjustmentUntilTwice() + XCTAssertEqual(test: "testRepeatedAjustmentUntilTwice", withExpression: (val == 14)) + } + func day01Tests() { + testInitWithFile() + testInitWithString() + testApplyAdjustments() + testRepeatedAjustmentUntilTwice() } func day01Final() { - let retVal = "None" - print("Answer to part 1 is: \(retVal)") - print("Answer to part 2 is: \(retVal)") + let chrona = Chronal(withFile: "/home/peterr/AOC2018/Sources/AOC2018/data/day01.txt") + if let val = chrona.applyAdjustments() { + print("Answer to part 1 is: \(val)") + } + let twice = chrona.repeatedAjustmentUntilTwice() + print("Answer to part 2 is: \(twice)") } }