Skip to content

Commit

Permalink
perf boost
Browse files Browse the repository at this point in the history
  • Loading branch information
gereons committed Dec 1, 2023
1 parent b6c7bbe commit fc7d96a
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions Sources/Day01/Day01.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,35 @@ final class Day01: AOCDay {
"1", "2", "3", "4", "5", "6", "7", "8", "9"
]

private func valueFor(_ index: Int) -> Int {
index < 9 ? index + 1 : index - 8
}

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 index == 0 { break }
var line = line

while !line.isEmpty {
for (index, str) in search.enumerated() {
if line.hasPrefix(str) {
return valueFor(index)
}
}
line.removeFirst()
}
return value > 9 ? value - 9 : value
fatalError()
}

private func lastNumber(in line: String) -> Int {
var maxIndex = Int.min
var value = 0
for (strIndex, str) in search.enumerated() {
if let index = line.lastIndexOf(str), index > maxIndex {
maxIndex = index
value = strIndex + 1
if index == line.count - str.count { break }
var line = line

while !line.isEmpty {
for (index, str) in search.enumerated() {
if line.hasSuffix(str) {
return valueFor(index)
}
}
line.removeLast()
}
return value > 9 ? value - 9 : value
fatalError()
}
}

0 comments on commit fc7d96a

Please sign in to comment.