Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package year_2023.day_2
- fun partTwo() {
- println("[2023] Day two, part two...")
- class Set(
- val redCount: Int,
- val greenCount: Int,
- val blueCount: Int
- ) {
- override fun toString(): String {
- return "rgb: ($redCount, $greenCount, $blueCount)"
- }
- }
- val gamesBySetPt2s = input().split("\n")
- .map { line ->
- line.substringAfter(":")
- .split(";")
- .map { rawSet ->
- val pairsInRawSet = rawSet.split(",")
- .map { it.trim() }
- .map {
- val count = it.split(" ")[0].toInt()
- val color = it.split(" ")[1]
- count to color
- }
- Set(
- redCount = pairsInRawSet.singleOrNull { it.second == "red" }?.first ?: 0,
- greenCount = pairsInRawSet.singleOrNull { it.second == "green" }?.first ?: 0,
- blueCount = pairsInRawSet.singleOrNull { it.second == "blue" }?.first ?: 0
- )
- }
- }
- var sumOfPowers = 0
- gamesBySetPt2s.forEach { listOfSets ->
- val currentGamePower = listOfSets.maxBy { it.redCount }.redCount *
- listOfSets.maxBy { it.greenCount }.greenCount * listOfSets.maxBy { it.blueCount }.blueCount
- sumOfPowers += currentGamePower
- }
- println(sumOfPowers)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement