diff --git a/src/main/kotlin/Day03.kt b/src/main/kotlin/Day03.kt new file mode 100644 index 0000000..9f36bc1 --- /dev/null +++ b/src/main/kotlin/Day03.kt @@ -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() +}