Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package year_2023.day_2
- fun partOne() {
- println("[2023] Day two, part one...")
- class Set(
- val redCount: Int,
- val greenCount: Int,
- val blueCount: Int
- ) {
- override fun toString(): String {
- return "rgb: ($redCount, $greenCount, $blueCount)"
- }
- }
- val numberOfRed = 12
- val numberOfGreen = 13
- val numberOfBlue = 14
- val gamesBySets = 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 idxSum = 0
- gamesBySets.forEachIndexed { idx, listOfSets ->
- if (
- listOfSets.all { it.redCount <= numberOfRed } &&
- listOfSets.all { it.greenCount <= numberOfGreen } &&
- listOfSets.all { it.blueCount <= numberOfBlue }
- ) {
- idxSum += idx + 1
- }
- }
- println(idxSum)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement