diff --git a/README.md b/README.md index ed0ad72..c76e887 100644 --- a/README.md +++ b/README.md @@ -36,31 +36,3 @@ To run tests, use `swift test` for all tests, or e.g. `swift test --filter AoCTe | Day | Part 1 | Part 2 | | :---: | :---: | :---: | - -### Time Log - -* Day1 -* Day2 -* Day3 -* Day4 -* Day5 -* Day6 -* Day7 -* Day8 -* Day9 -* Day10 -* Day11 -* Day12 -* Day13 -* Day14 -* Day15 -* Day16 -* Day17 -* Day18 -* Day19 -* Day20 -* Day21 -* Day22 -* Day23 -* Day24 -* Day25 diff --git a/Sources/Day01/Day01.swift b/Sources/Day01/Day01.swift index 63f62c8..7834565 100644 --- a/Sources/Day01/Day01.swift +++ b/Sources/Day01/Day01.swift @@ -7,17 +7,57 @@ import AoCTools final class Day01: AOCDay { - + let lines: [String] + init(input: String?) { - let input = input ?? Self.input - print(input) + lines = (input ?? Self.input).lines } func part1() -> Int { - return 0 + lines.reduce(0) { acc, line in + let chars = line.map { $0 } + let first = chars.first { $0.isNumber }! + let last = chars.last { $0.isNumber }! + return acc + Int(String(first))! * 10 + Int(String(last))! + } } func part2() -> Int { - return 0 + lines.reduce(0) { acc, line in + let first = firstNumber(in: line) + let last = lastNumber(in: line) + return acc + first * 10 + last + } + } + + private let search = [ + "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", + "1", "2", "3", "4", "5", "6", "7", "8", "9" + ] + + private func firstNumber(in line: String) -> Int { + var minIndex = Int.max + var value = 0 + for (strIndex, str) in search.enumerated() { + if let index = line.indexOf(str), index < minIndex { + minIndex = index + value = strIndex + 1 + } + } + if value > 9 { value -= 9 } + return value + } + + private func lastNumber(in line: String) -> Int { + var maxIndex = Int.min + var value = 0 + for (strIndex, str) in search.enumerated() { + if let index = line.indicesOf(str).last, index > maxIndex { + maxIndex = index + value = strIndex + 1 + } + } + if value > 9 { value -= 9 } + return value } } diff --git a/Tests/Day01Tests.swift b/Tests/Day01Tests.swift index 16940ec..9c32fad 100644 --- a/Tests/Day01Tests.swift +++ b/Tests/Day01Tests.swift @@ -7,25 +7,39 @@ import XCTest final class Day01Tests: XCTestCase { let testInput = """ +1abc2 +pqr3stu8vwx +a1b2c3d4e5f +treb7uchet +""" + + let testInput2 = """ +two1nine +eightwothree +abcone2threexyz +xtwone3four +4nineeightseven2 +zoneight234 +7pqrstsixteen """ func testDay01_part1() throws { let day = Day01(input: testInput) - XCTAssertEqual(day.part1(), 0) + XCTAssertEqual(day.part1(), 142) } func testDay01_part1_solution() throws { let day = Day01(input: Day01.input) - XCTAssertEqual(day.part1(), 0) + XCTAssertEqual(day.part1(), 53974) } func testDay01_part2() throws { - let day = Day01(input: testInput) - XCTAssertEqual(day.part2(), 0) + let day = Day01(input: testInput2) + XCTAssertEqual(day.part2(), 281) } func testDay01_part2_solution() throws { let day = Day01(input: Day01.input) - XCTAssertEqual(day.part2(), 0) + XCTAssertEqual(day.part2(), 52840) } }