🎄Advent of Code - Day 2 part 2

This commit is contained in:
Denis-Cosmin Nutiu 2023-12-02 12:27:50 +02:00
parent c58688c25c
commit 9feaac9deb
2 changed files with 18 additions and 16 deletions

View file

@ -38,7 +38,7 @@ class CubeConundrum : Puzzle {
var gamePossible = true var gamePossible = true
it.gameData?.forEach { gameData -> it.gameData?.forEach { gameData ->
val cubes = gameData.split(",") val cubes = gameData.split(",")
cubes.forEach cubeForEach@ { cubeSet -> cubes.forEach cubeForEach@{ cubeSet ->
val cubeData = cubeSet.trim().split(" ") val cubeData = cubeSet.trim().split(" ")
if (cubeData[0].toInt() > gameMaxConstraints.getOrDefault(cubeData[1], Int.MAX_VALUE)) { if (cubeData[0].toInt() > gameMaxConstraints.getOrDefault(cubeData[1], Int.MAX_VALUE)) {
gamePossible = false gamePossible = false
@ -59,24 +59,26 @@ class CubeConundrum : Puzzle {
What is the sum of the power of these sets? What is the sum of the power of these sets?
*/ */
val games = getGameList(); val games = getGameList();
var gamePower = 0 val sum = games.map {
val gameMaxConstraints = mapOf( var gamePower = 0
"red" to 12, var minimumPerGame = mutableMapOf(
"green" to 13, "red" to 0,
"blue" to 14 "green" to 0,
) "blue" to 0
games.forEach { )
it.gameData?.forEach { gameData -> it.gameData?.forEach { gameData ->
val cubes = gameData.split(",") val cubes = gameData.split(",")
cubes.forEach cubeForEach@ { cubeSet -> cubes.forEach cubeForEach@{ cubeSet ->
val cubeData = cubeSet.trim().split(" ") val cubeData = cubeSet.trim().split(" ")
println() minimumPerGame[cubeData[1]] =
Math.max(minimumPerGame[cubeData[1]]!!, cubeData[0].toInt())
} }
} }
gamePower = 0 gamePower = minimumPerGame["red"]!! * minimumPerGame["green"]!! * minimumPerGame["blue"]!!
}
println("The sum of the game ids is $gamePower.") gamePower
}.sum()
println("The sum of the game power is $sum.")
} }
} }

View file

@ -2,6 +2,6 @@ import AdventOfCode2023.day2.CubeConundrum
fun main(args: Array<String>) { fun main(args: Array<String>) {
val t = CubeConundrum() val t = CubeConundrum()
t.partOne() // t.partOne()
// t.partTwo() t.partTwo()
} }