2024-12-03 07:26:53 +00:00
|
|
|
#!/usr/bin/env kotlin
|
2024-12-03 06:40:58 +00:00
|
|
|
import java.io.File
|
|
|
|
import java.util.Scanner
|
|
|
|
|
|
|
|
val scanner = Scanner(File("day03input.txt"))
|
|
|
|
var partOneSum = 0
|
|
|
|
var partTwoSum = 0
|
|
|
|
var doState = true
|
|
|
|
|
|
|
|
while (scanner.hasNextLine()) {
|
|
|
|
val line = scanner.nextLine()
|
|
|
|
val regex = Regex("(mul\\((\\d+),(\\d+)\\))|(do\\(\\))|(don't\\(\\))")
|
|
|
|
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")
|