#Part 2 AoC

1 messages · Page 1 of 1 (latest)

thick smelt
#

My solution does work with the small example input in part 2 and i also did get some small bundles of the main input and calculated them by hand and with my solution, always got the rigth result. But on the whole main input i do get a wrong answer? I mean thats a very simple and easy puzzle but somehow im maybe forgetting something? Here is the code: ```kotlin
fun part2(): Int {
var result = 0
for (line in data) {
val regex = """zero|one|two|three|four|five|six|seven|eight|nine""".toRegex()
val map = mapOf("zero" to "0", "one" to "1", "two" to "2", "three" to "3", "four" to "4", "five" to "5", "six" to "6", "seven" to "7", "eight" to "8", "nine" to "9")
val parsedLine = line.replace(regex) { map[it.value] ?: it.value }

    val index1 = parsedLine.indexOfFirst { char -> char.isDigit()}
    val index2 = parsedLine.indexOfLast { char -> char.isDigit()}

    val firstValue = parsedLine[index1].digitToInt() * 10
    val secondValue = parsedLine[index2].digitToInt()
    result += firstValue + secondValue;
}

return result

}``` (Its the first time im using Kotlin so dont trash talk this code pepekek )

tulip badgerBOT
#

<@&1008423204219531294> please have a look, thanks.

spare apex
#

You don't handle numbers being insider of each other correctly. Specifically there's cases like eightwo which should be parsed as 82

#

In the examples the only one with a number in another happens to have a digit at the end which causes your code to work

thick smelt
#

Oh bruh i see, didnt thought about that

#

ty peepo_heart