diff --git a/src/aoc2023/Day02.kt b/src/aoc2023/Day02.kt index aec997c..5962dbd 100644 --- a/src/aoc2023/Day02.kt +++ b/src/aoc2023/Day02.kt @@ -57,21 +57,15 @@ Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green fun part1(input: List): Int { val limits = mapOf("red" to 12, "green" to 13, "blue" to 14) return input.map { - val lg = it.split(":") - val sets = lg[1].trim().split(";") - var possible = lg[0].split(" ")[1].toInt() - exit@ for (s in sets) { - val boxes = s.trim().split(",") - for (b in boxes) { - val (num, col) = b.trim().split(" ") - if (num.toInt() > limits[col]!!) { - possible = 0 - break@exit - } + "Game (\\d+): (.*)".toRegex().matchEntire(it)?.destructured + ?.let { (id, game) -> + var possible = id.toInt() + game.replace(";", "").replace(",", "") + .split(" ").chunked(2) + .forEach { (num, col) -> if (num.toInt() > limits[col]!!) possible = 0 } + possible } - } - possible - }.sum() + }.sumOf { it!! } } fun part2(input: List): Int { @@ -83,7 +77,7 @@ Game 5: 6 red, 1 blue, 3 green; 2 blue, 1 red, 2 green val boxes = s.trim().split(",") for (b in boxes) { val (num, col) = b.trim().split(" ") - powerMap.merge(col, num.toInt()) { a, b -> max(a, b) } + powerMap.merge(col, num.toInt(), ::max) } } powerMap.values.reduce { a, b -> a * b }