-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay03.kt
48 lines (41 loc) · 1.33 KB
/
Day03.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package tr.emreone.adventofcode.days
import tr.emreone.kotlin_utils.Resources
import tr.emreone.kotlin_utils.automation.Day
import tr.emreone.kotlin_utils.automation.log
class Day03 : Day(
3,
2024,
"Mull It Over",
session = Resources.resourceAsString("session.cookie")
) {
override fun part1(): Long {
return "(mul\\((?<first>\\d{1,3}),(?<second>\\d{1,3})\\))".toRegex()
.findAll(inputAsString)
.sumOf {
val a = it.groups["first"]?.value ?: "0"
val b = it.groups["second"]?.value ?: "0"
a.toLong() * b.toLong()
}
}
override fun part2(): Long {
val matches = "(do\\(\\))|(don't\\(\\))|(mul\\((?<first>\\d{1,3}),(?<second>\\d{1,3})\\))".toRegex()
.findAll(inputAsString)
.toList()
var enabled = true
var result = 0L
matches.forEach {
if (it.groups[0]?.value == "do()") {
enabled = true
}
else if (it.groups[0]?.value == "don't()") {
enabled = false
}
else if (enabled) {
val a = it.groups["first"]?.value ?: "0"
val b = it.groups["second"]?.value ?: "0"
result += a.toLong() * b.toLong()
}
}
return result
}
}