But I’m struggling with the second part. My code validates the “example” data fine, but it always tells me that for the actual data, my value is too low.
While its kind simple code, it works and reasonably fast. Throw this in an Xcode playground. I’m lost as to what I’m doing wrong, could anyone provide any hints?
let sampleData = """
two1nine
eightwothree
abcone2threexyz
xtwone3four
4nineeightseven2
zoneight234
7pqrstsixteen
"""
let words:[(String, Int)] = [ ("one", 1 ), ("two", 2), ("three", 3), ("four", 4), ("five", 5), ("six", 6), ("seven", 7), ("eight", 8), ("nine", 9) ]
let lines = sampleData.split( whereSeparator: \.isNewline)
var totalValue:Int = 0
var numbers:[Int]
var leftOvers:String = ""
for cLine in lines {
leftOvers = ""
numbers = []
for character in cLine {
if let i = Int( String( character ) ) {
numbers.append( i )
continue
}
leftOvers = leftOvers + String( character )
if leftOvers.count < 3 { continue }
for checkWord in words {
if !leftOvers.contains( checkWord.0 ) { continue }
numbers.append( checkWord.1 )
leftOvers = ""
}
}
if let firstNumber = numbers.first, let lastNumber = numbers.last {
print( "\(( firstNumber * 10 ) + lastNumber)")
totalValue += ( ( firstNumber * 10 ) + lastNumber )
}
}
print( "Total: \(totalValue)")
why not just process input line by line, filter numbers from string and I think you can directly get the first and the last characters from string with Swift…
I tried too many times and was blocked, so I looked up how other people did it. So I’ll forfeit the second point. Not a good start for day 1. Hopefully I can make it up later.
At least this poorly optimized code completed all 1,000 entries in less than a second.
Why do you use tuples in an array here and not simply make a dictionary, i.e.:
let words = ["one":1, "two":2, ...]
(I am trying Swift this time as well, and already learned that a for loop where the upper value changes during the loop won’t get re-evaluated like it happens in most other languages, so that I quickly ran into an out-of-bounds error that took a while to understand.)
No real reason, except to loop through 'em. I coulda done that with a dictionary and the .keys array like you say. I just didn’t think to even consider it.
Oh… I guess you’d have to do it the told fashioned way… Which I’d have to look up how to do it Like you, I’m still learning Swift and thought it would be a fun challenge to use Swift for it.
Concurrency is so stupidly simple in Swift, it really shows how bad that side of Xojo is.
I will confess that I’ve only tried to load plists so far and that was more complicated than it is with Objective-C (one liner). I’d say it was about the same complexity as trying to do it with Xojo.
Where Swift really excels is serialization either to plists or a database.
My RegEx skills suck big time, so I just used a simple string search and recorded indices for sorting purposes with my solution for the second part of day 1 using 8th programming language:
It’s 8th. I went for easy solution for day 2 part 2 and created words for colors. That allowed me to just split strings and strip away ‘,’ from strings making it a valid 8th syntax. After that eval could simply be used.