39 lines
927 B
Kotlin
39 lines
927 B
Kotlin
package main
|
|
|
|
import java.io.File
|
|
|
|
fun main() {
|
|
var sum = 0
|
|
File("../input.txt").forEachLine{
|
|
var firstDigit = ""
|
|
var lastDigit = ""
|
|
|
|
for (c in replaceStrWithDigits(it)) {
|
|
if (c.isDigit()) {
|
|
lastDigit = c.toString()
|
|
if (firstDigit == "") {
|
|
firstDigit = c.toString()
|
|
}
|
|
}
|
|
}
|
|
|
|
sum += (firstDigit + lastDigit).toInt()
|
|
}
|
|
println(sum)
|
|
}
|
|
|
|
fun replaceStrWithDigits(line: String): String {
|
|
var s = line
|
|
var words = arrayOf("one", "two", "three", "four", "five", "six", "seven", "eight", "nine")
|
|
var i = 0
|
|
while (i < s.length) {
|
|
for (w in words.indices) {
|
|
if (s.slice(i..s.length-1).startsWith(words[w])) {
|
|
s = s.replaceRange(i, i + 1, (w + 1).toString())
|
|
break
|
|
}
|
|
}
|
|
i += 1
|
|
}
|
|
return s
|
|
}
|