Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fun partTwo() {
- println("[2024] Day three, part two...")
- val input = input()
- val regex = "mul\\(\\d+,\\d+\\)|do\\(\\)|don't\\(\\)".toRegex()
- val validMulsDosAndDonts = regex.findAll(input)
- .map { it.value }
- .toList()
- val enabledMuls = mutableListOf<String>()
- var mulsEnabled = true
- validMulsDosAndDonts.forEach { command ->
- if (command == "do()") {
- mulsEnabled = true
- return@forEach
- }
- if (command == "don't()") {
- mulsEnabled = false
- return@forEach
- }
- if (mulsEnabled) {
- enabledMuls.add(command)
- }
- }
- val mappedMuls = enabledMuls.map {
- val values = it
- .substringAfter("(")
- .substringBeforeLast(")")
- .split(",")
- .map { it.toLong() }
- values[0] to values[1]
- }
- val result = mappedMuls.sumOf { it.first * it.second }
- println("Result: $result")
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement