Advertisement
hrabrica

AoC | Day 2 | Part 1

Dec 2nd, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.62 KB | None | 0 0
  1. package year_2023.day_2
  2.  
  3. fun partOne() {
  4. println("[2023] Day two, part one...")
  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 numberOfRed = 12
  17. val numberOfGreen = 13
  18. val numberOfBlue = 14
  19.  
  20. val gamesBySets = input().split("\n")
  21. .map { line ->
  22. line.substringAfter(":")
  23. .split(";")
  24. .map { rawSet ->
  25. val pairsInRawSet = rawSet.split(",")
  26. .map { it.trim() }
  27. .map {
  28. val count = it.split(" ")[0].toInt()
  29. val color = it.split(" ")[1]
  30. count to color
  31. }
  32.  
  33. Set(
  34. redCount = pairsInRawSet.singleOrNull { it.second == "red" }?.first ?: 0,
  35. greenCount = pairsInRawSet.singleOrNull { it.second == "green" }?.first ?: 0,
  36. blueCount = pairsInRawSet.singleOrNull { it.second == "blue" }?.first ?: 0
  37. )
  38. }
  39. }
  40.  
  41. var idxSum = 0
  42. gamesBySets.forEachIndexed { idx, listOfSets ->
  43. if (
  44. listOfSets.all { it.redCount <= numberOfRed } &&
  45. listOfSets.all { it.greenCount <= numberOfGreen } &&
  46. listOfSets.all { it.blueCount <= numberOfBlue }
  47. ) {
  48. idxSum += idx + 1
  49. }
  50.  
  51. }
  52.  
  53. println(idxSum)
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement