File tree 2 files changed +14
-4
lines changed
src/main/kotlin/nl/sanderp/aoc/aoc2015/day12
2 files changed +14
-4
lines changed Original file line number Diff line number Diff line change @@ -16,8 +16,9 @@ repositories {
16
16
}
17
17
18
18
dependencies {
19
- testImplementation(kotlin( " test " ) )
19
+ implementation( " org.jetbrains.kotlinx " , " kotlinx-serialization-json " , " 1.6.2 " )
20
20
21
+ testImplementation(kotlin(" test" ))
21
22
testImplementation(" org.junit.jupiter" , " junit-jupiter-params" , junitVersion)
22
23
testImplementation(" org.amshove.kluent" , " kluent" , " 1.68" )
23
24
}
Original file line number Diff line number Diff line change 1
1
package nl.sanderp.aoc.aoc2015.day12
2
2
3
+ import kotlinx.serialization.json.*
3
4
import nl.sanderp.aoc.common.readResource
4
5
5
- fun findNumbers (input : String ) = Regex (""" [0-9\-]+""" ).findAll(input).map { it.value.toInt() }
6
+ fun sumNumbers (element : JsonElement ) = sumNumbers(element) { false }
7
+
8
+ fun sumNumbers (element : JsonElement , ignoreObject : (JsonObject ) -> Boolean ): Int = when {
9
+ element is JsonPrimitive -> element.intOrNull ? : 0
10
+ element is JsonArray -> element.sumOf { sumNumbers(it, ignoreObject) }
11
+ element is JsonObject && ! ignoreObject(element) -> element.values.sumOf { sumNumbers(it, ignoreObject) }
12
+ else -> 0
13
+ }
6
14
7
15
fun main () {
8
- val input = readResource(" Day12.txt" )
9
- println (" Part one: ${findNumbers(input).sum()} " )
16
+ val input = Json .decodeFromString<JsonElement >(readResource(" Day12.txt" ))
17
+ println (" Part one: ${sumNumbers(input)} " )
18
+ println (" Part two: ${sumNumbers(input) { it.containsValue(JsonPrimitive (" red" )) }} " )
10
19
}
You can’t perform that action at this time.
0 commit comments