45 lines
1.5 KiB
Kotlin
45 lines
1.5 KiB
Kotlin
package aoc2024
|
|
|
|
import println
|
|
import readInput
|
|
|
|
/*
|
|
--- Day 3: Mull It Over ---
|
|
https://adventofcode.com/2024/day/3
|
|
*/
|
|
fun main() {
|
|
|
|
val inlineTestInput = """
|
|
xmul(2,4)%&mul[3,7]!@^do_not_mul(5,5)+mul(32,64]then(mul(11,8)mul(8,5))
|
|
"""
|
|
val inlineTestInput2 = """
|
|
xmul(2,4)&mul[3,7]!^don't()_mul(5,5)+mul(32,64](mul(11,8)undo()?mul(8,5)don't()mul(10,10)
|
|
"""
|
|
|
|
fun part1(input: List<String>): Int {
|
|
return "mul\\((\\d+),(\\d+)\\)".toRegex().findAll(input.joinToString(""))
|
|
.sumOf { it.groupValues[1].toInt() * it.groupValues[2].toInt() }
|
|
}
|
|
|
|
fun part2(input: List<String>): Int {
|
|
return "mul\\((\\d+),(\\d+)\\)".toRegex()
|
|
.findAll((input.joinToString("") + "do()").replace("(don't\\(\\).*?)+(do\\(\\)|$)".toRegex(), ""))
|
|
.sumOf { it.groupValues[1].toInt() * it.groupValues[2].toInt() }
|
|
}
|
|
|
|
// test if implementation meets criteria from the description, like:
|
|
val testInput = inlineTestInput.trim().reader().readLines()
|
|
val testInput2 = inlineTestInput2.trim().reader().readLines()
|
|
//val testInput = readInput("aoc2024/Day03_test")
|
|
val testInputPart1Result = part1(testInput)
|
|
println("Part 1 Test: $testInputPart1Result")
|
|
val testInputPart2Result = part2(testInput2)
|
|
println("Part 2 Test: $testInputPart2Result")
|
|
check(testInputPart1Result == 161)
|
|
check(testInputPart2Result == 48)
|
|
|
|
val input = readInput("aoc2024/Day03")
|
|
part1(input).println()
|
|
part2(input).println()
|
|
}
|