Skip to content

Commit

Permalink
Day03 - I hate RegEx
Browse files Browse the repository at this point in the history
  • Loading branch information
derNiklaas committed Dec 3, 2024
1 parent c21182d commit 738a3e0
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/main/kotlin/Day03.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import utils.AoCDay
import utils.splitAndMapToInt

class Day03 : AoCDay() {

private val mulRegEx = """mul\(\d*,\d*\)""".toRegex()
private val inactiveRegex = """don't\(\).*?do\(\)""".toRegex()
private val code = input.joinToString("")
override fun part1(): Any {
val matches = mulRegEx.findAll(code)
var result = 0L
matches.forEach { match ->
val numbers = match.value.drop(4).dropLast(1).splitAndMapToInt(",")
result += numbers[0] * numbers[1]
}
return result
}

override fun part2(): Any {
var activeCode = code.replace(inactiveRegex, "")
val matches = mulRegEx.findAll(activeCode)
var result = 0L
matches.forEach { match ->
val numbers = match.value.drop(4).dropLast(1).splitAndMapToInt(",")
result += numbers[0] * numbers[1]
}
return result
}
}

fun main() {
Day03().execute()
}

0 comments on commit 738a3e0

Please sign in to comment.