Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package year_2023.day_7
- fun partOne() {
- println("[2023] Day seven, part one...")
- val cardsByValue = listOf("A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2").reversed()
- val handBidPairs = input().split("\n")
- .map { it.trim() }
- .map {
- it.split(" ")[0].trim() to
- it.split(" ")[1].toInt()
- }
- fun getHandValue(hand: List<String>): Int {
- return when {
- hand.count { it == hand[0] } == 5 -> 10
- hand.any { char -> hand.count { it == char } == 4 } -> 9
- hand.any { char -> hand.count { it == char } == 3 } && hand.toSet().size == 2 -> 8
- hand.any { char -> hand.count { it == char } == 3 } -> 7
- hand.any { char -> hand.count { it == char } == 2 } && hand.toSet().size == 3 -> 6
- hand.any { char -> hand.count { it == char } == 2 } && hand.toSet().size == 4 -> 5
- else -> 0
- }
- }
- val comparator = Comparator<Pair<String, Int>> { (hand1, _), (hand2, _) ->
- val hand1Cards = hand1.split("").filter { it.isNotBlank() }
- val hand2Cards = hand2.split("").filter { it.isNotBlank() }
- val hand1Value = getHandValue(hand1Cards)
- val hand2Value = getHandValue(hand2Cards)
- if (hand1Value > hand2Value) {
- return@Comparator 1
- }
- if (hand1Value < hand2Value) {
- return@Comparator -1
- }
- repeat(hand1Cards.size) { idx ->
- if (cardsByValue.indexOf(hand1Cards[idx]) > cardsByValue.indexOf(hand2Cards[idx])) {
- return@Comparator 1
- }
- if (cardsByValue.indexOf(hand1Cards[idx]) < cardsByValue.indexOf(hand2Cards[idx])) {
- return@Comparator -1
- }
- }
- println("Shouldn't happen")
- 0
- }
- val sortedHandBidPairs = handBidPairs.sortedWith(comparator)
- var sum = 0
- sortedHandBidPairs.forEachIndexed { index, (_, bid) ->
- sum += (index + 1) * bid
- }
- println(sum)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement