Advertisement
hrabrica

AoC | Day 7 | Part 2

Dec 7th, 2023
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.03 KB | Source Code | 0 0
  1. package year_2023.day_7
  2.  
  3. import java.time.Instant
  4.  
  5. fun partTwo() {
  6.     println("[2023] Day seven, part two...")
  7.  
  8.     val cardsByValue = listOf("A", "K", "Q", "T", "9", "8", "7", "6", "5", "4", "3", "2").reversed()
  9.     val handBidPairs = input().split("\n")
  10.         .map { it.trim() }
  11.         .map {
  12.             it.split(" ")[0].trim() to
  13.                     it.split(" ")[1].toInt()
  14.         }
  15.  
  16.     fun getHandValue(hand: List<String>): Int {
  17.         return when {
  18.             hand.count { it == hand[0] } == 5 -> 10
  19.             hand.any { char -> hand.count { it == char } == 4 } -> 9
  20.             hand.any { char -> hand.count { it == char } == 3 } && hand.toSet().size == 2 -> 8
  21.             hand.any { char -> hand.count { it == char } == 3 } -> 7
  22.             hand.any { char -> hand.count { it == char } == 2 } && hand.toSet().size == 3 -> 6
  23.             hand.any { char -> hand.count { it == char } == 2 } && hand.toSet().size == 4 -> 5
  24.             else -> 0
  25.         }
  26.     }
  27.  
  28.     val comparator = Comparator<Pair<String, Int>> { (hand1, _), (hand2, _) ->
  29.         val hand1Cards = hand1.split("").filter { it.isNotBlank() }.toMutableList()
  30.         val hand2Cards = hand2.split("").filter { it.isNotBlank() }.toMutableList()
  31.         var hand1Value = getHandValue(hand1Cards)
  32.         var hand2Value = getHandValue(hand2Cards)
  33.  
  34.         if (hand1.contains("J")) {
  35.             cardsByValue.forEach {
  36.                 val replacedHand1 = hand1Cards.joinToString("")
  37.                     .replace("J", it)
  38.                     .split("")
  39.                     .filter { it.isNotBlank() }
  40.  
  41.                 val replacedJValue = getHandValue(replacedHand1)
  42.                 if (replacedJValue > hand1Value) {
  43.                     hand1Value = replacedJValue
  44.                 }
  45.             }
  46.         }
  47.  
  48.         if (hand2.contains("J")) {
  49.             cardsByValue.forEach {
  50.                 val replacedHand2 = hand2Cards.joinToString("")
  51.                     .replace("J", it)
  52.                     .split("")
  53.                     .filter { it.isNotBlank() }
  54.  
  55.                 val replacedJValue = getHandValue(replacedHand2)
  56.                 if (replacedJValue > hand2Value) {
  57.                     hand2Value = replacedJValue
  58.                 }
  59.             }
  60.         }
  61.  
  62.         if (hand1Value > hand2Value) {
  63.             return@Comparator 1
  64.         }
  65.         if (hand1Value < hand2Value) {
  66.             return@Comparator -1
  67.         }
  68.  
  69.         repeat(hand1Cards.size) { idx ->
  70.             if (cardsByValue.indexOf(hand1Cards[idx]) > cardsByValue.indexOf(hand2Cards[idx])) {
  71.                 return@Comparator 1
  72.             }
  73.  
  74.             if (cardsByValue.indexOf(hand1Cards[idx]) < cardsByValue.indexOf(hand2Cards[idx])) {
  75.                 return@Comparator -1
  76.             }
  77.         }
  78.  
  79.         println("Shouldn't happen")
  80.         0
  81.     }
  82.  
  83.     val sortedHandBidPairs = handBidPairs.sortedWith(comparator)
  84.     var sum = 0
  85.     sortedHandBidPairs.forEachIndexed { index, (_, bid) ->
  86.         sum += (index + 1) * bid
  87.     }
  88.  
  89.     println(sum)
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement