Skip to content

Commit

Permalink
day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gereons committed Dec 1, 2023
1 parent 975f61b commit f96358d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 38 deletions.
28 changes: 0 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
| :---: | :---: | :---: |
<!--- advent_readme_stars table --->

### 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
50 changes: 45 additions & 5 deletions Sources/Day01/Day01.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
24 changes: 19 additions & 5 deletions Tests/Day01Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit f96358d

Please sign in to comment.