-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathy24day3.kt
More file actions
22 lines (20 loc) · 770 Bytes
/
Copy pathy24day3.kt
File metadata and controls
22 lines (20 loc) · 770 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fun main() {
val input = generateSequence { readlnOrNull() }.joinToString() // OMG, it was multiline!
// part 1
"""mul\((\d{1,3}),(\d{1,3})\)""".toRegex()
.findAll(input).map {
it.destructured.let { (a, b) -> a.toLong() * b.toLong() }
}.sum().also(::println)
// part 2
var enabled = true
"""mul\((\d{1,3}),(\d{1,3})\)|\Qdo()\E|\Qdon't()\E""".toRegex()
.findAll(input).filter {
when (it.groupValues[0]) {
"do()" -> { enabled = true; false }
"don't()" -> { enabled = false; false }
else -> enabled
}
}.map {
it.groupValues.subList(1, 3).map(String::toLong).fold(1L) { a, b -> a * b }
}.sum().also(::println)
}