aoc_2024/day03.kts

31 lines
896 B
Plaintext
Raw Normal View History

#!/usr/bin/env kotlin
2024-12-03 06:40:58 +00:00
import java.io.File
import java.util.Scanner
2024-12-05 13:43:19 +00:00
val scanner = Scanner(System.`in`)
2024-12-03 06:40:58 +00:00
var partOneSum = 0
var partTwoSum = 0
var doState = true
while (scanner.hasNextLine()) {
val line = scanner.nextLine()
2024-12-06 04:33:43 +00:00
val regex = Regex("""(mul\((\d+),(\d+)\))|(do\(\))|(don't\(\))""")
2024-12-03 06:40:58 +00:00
val match = regex.findAll(line)
for (matchResult in match) {
if (matchResult.value == "don't()") {
doState = false
} else if (matchResult.value == "do()") {
doState = true
} else {
val (a, b, c) = matchResult.destructured
val factorOne = b.toInt()
val factorTwo = c.toInt()
partOneSum += factorOne * factorTwo
if (doState) {
partTwoSum += factorOne * factorTwo
}
}
}
}
println("Part One Sum: $partOneSum")
println("Part Two Sum: $partTwoSum")