Advertisement
hrabrica

AoC | Day 2 | Part 2

Dec 2nd, 2023
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. package year_2023.day_2
  2.  
  3. fun partTwo() {
  4. println("[2023] Day two, part two...")
  5.  
  6. class Set(
  7. val redCount: Int,
  8. val greenCount: Int,
  9. val blueCount: Int
  10. ) {
  11. override fun toString(): String {
  12. return "rgb: ($redCount, $greenCount, $blueCount)"
  13. }
  14. }
  15.  
  16. val gamesBySetPt2s = input().split("\n")
  17. .map { line ->
  18. line.substringAfter(":")
  19. .split(";")
  20. .map { rawSet ->
  21. val pairsInRawSet = rawSet.split(",")
  22. .map { it.trim() }
  23. .map {
  24. val count = it.split(" ")[0].toInt()
  25. val color = it.split(" ")[1]
  26. count to color
  27. }
  28.  
  29. Set(
  30. redCount = pairsInRawSet.singleOrNull { it.second == "red" }?.first ?: 0,
  31. greenCount = pairsInRawSet.singleOrNull { it.second == "green" }?.first ?: 0,
  32. blueCount = pairsInRawSet.singleOrNull { it.second == "blue" }?.first ?: 0
  33. )
  34. }
  35. }
  36.  
  37. var sumOfPowers = 0
  38. gamesBySetPt2s.forEach { listOfSets ->
  39. val currentGamePower = listOfSets.maxBy { it.redCount }.redCount *
  40. listOfSets.maxBy { it.greenCount }.greenCount * listOfSets.maxBy { it.blueCount }.blueCount
  41.  
  42. sumOfPowers += currentGamePower
  43. }
  44.  
  45. println(sumOfPowers)
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement