Advent of Code 2022 day 7
12/07/2023, 5:00 AMMarcin Wisniowski
12/07/2023, 6:01 AMMichael de Kaste
12/07/2023, 6:01 AMMarcin Wisniowski
12/07/2023, 6:02 AMMarcin Wisniowski
12/07/2023, 6:03 AMbabel
12/07/2023, 6:12 AMJacob
12/07/2023, 6:38 AMandriyo
12/07/2023, 6:41 AMPeter Duong
12/07/2023, 6:43 AMPeter Duong
12/07/2023, 6:43 AMGrzegorz Aniol
12/07/2023, 6:43 AMandriyo
12/07/2023, 6:45 AMNorbert Kiesel
12/07/2023, 6:50 AMgroupingBy { it }.eachCount()
, and then compute the rank using a when
that looks at group sizes and values. And for tie-breaker, I used val order = if (joker) "AKQT98765432J" else "AKQJT98765432"
and then order.indexOf(a) compareTo order.indexOf(b)
.Michael de Kaste
12/07/2023, 6:52 AMephemient
12/07/2023, 7:00 AMephemient
12/07/2023, 7:02 AMRiccardo Lippolis
12/07/2023, 7:02 AMRiccardo Lippolis
12/07/2023, 7:04 AMephemient
12/07/2023, 7:07 AMephemient
12/07/2023, 7:08 AMRiccardo Lippolis
12/07/2023, 7:09 AMJohannes Gätjen
12/07/2023, 7:19 AMTobias Suchalla
12/07/2023, 8:27 AMTomasz Linkowski
12/07/2023, 8:38 AMJ
listed twice
listOf('A', 'K', 'Q', 'J', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'J')
instead of
listOf('A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'J')
Jaap Beetstra
12/07/2023, 8:39 AMJaap Beetstra
12/07/2023, 8:40 AMJakub Gwóźdź
12/07/2023, 8:46 AMphldavies
12/07/2023, 8:57 AMDan Fingal-Surma
12/07/2023, 9:17 AMDan Fingal-Surma
12/07/2023, 9:24 AMDan Fingal-Surma
12/07/2023, 9:30 AMcompareBy
, was looking for that, I started the chain defining a Comparator
directlyJohannes Gätjen
12/07/2023, 9:34 AMOMG that is so much better. Every time I have to think "wait is at, was looking for that, I started the chain defining acompareBy
directlyComparator
a - b
or b - a
?"Jakub Gwóźdź
12/07/2023, 9:36 AMDan Fingal-Surma
12/07/2023, 9:36 AMjava.util.Comparator.comparing
ritesh
12/07/2023, 9:45 AMritesh
12/07/2023, 9:45 AMDan Fingal-Surma
12/07/2023, 9:51 AMPaul Woitaschek
12/07/2023, 9:59 AMritesh
12/07/2023, 10:00 AMAndre T
12/07/2023, 10:35 AMAnirudh
12/07/2023, 10:42 AMgetHandStrength
on the first line and the jCardStrengths
for the per-card comparison.
so I did this (third image) but would like to use the attr in the groupBy
rather than the if
inside it:
val attr = when (handStrengthAttr == "hand") {
true -> Bet::hand
else -> Bet::bestHand
}
would that be a reference to a property?Jaap Beetstra
12/07/2023, 10:43 AMAnirudh
12/07/2023, 10:46 AMAJQJT
is currently best as AAQAT
(3 of a kind)
but with the flushes, the best would actual be AKQJT
(which currently counts as the worst) and requires replacing each J
separately, rather than all at once with the same cardephemient
12/07/2023, 10:57 AMJakub Gwóźdź
12/07/2023, 11:05 AMconst val order = "_j23456789TJQKA_"
data class Hand(val value: Int) : Comparable<Hand> {
constructor(type: Int, cards: String) : this(cards.fold(type) { acc, c -> acc * 16 + order.indexOf(c) })
val type: Int get() = value shr 20
override fun compareTo(other: Hand): Int = value.compareTo(other.value)
}
that makes ordering/comparing much easier. (why *16, you ask? because that makes it easier to check with override fun toString() = value.toString(16).padStart(8,'0')
🙂 )
of course you’d still need to identify the type of hand, but that’s also Int somewhere from the range from HighCard to FiveOfAKindFredrik Rødland
12/07/2023, 11:37 AMval LABELS = mapOf('A' to 14, 'K' to 13, 'Q' to 12, 'J' to 11, 'T' to 10) + (2..9).map { it.digitToChar() to it }
private val MAX = LABELS.values.max().toDouble()
val POWERS = (0..4).associateWith { idx -> (MAX.pow((5 - idx))).toInt() }
val TYPE_FACTOR = MAX.pow(6)
data class Hand(val cards: List<Int>, val groups: List<Int>, val bid: Int) {
private val typeValue = groups[0] * 10 + (groups.getOrNull(1) ?: 0) // 50, 41, 32, 31, 22, 21, 11
private val cardValue = (0..4).sumOf { cards[it] * POWERS[it]!! } // [0]*14^5 + [1]*14^4 ...
val sortValue = typeValue * TYPE_FACTOR + cardValue
}
Fredrik Rødland
12/07/2023, 11:41 AMwhen
posted aboveJakub Gwóźdź
12/07/2023, 11:42 AMfun normalHand(cards: String): Hand {
val counts = cards.groupBy { it }.map { (_, v) -> v.size }.sortedDescending()
val type = (counts[0] * 16 + counts.getOrElse(1) { 0 }) * 16
return Hand(type, cards)
}
fun jokeHand(cards: String): Hand {
if ('J' !in cards) return normalHand(cards)
val bestType = "234567890TQKA".maxOf { normalHand(cards.replace('J', it)) }.type
return Hand(bestType, cards.replace('J', 'j'))
}
phldavies
12/07/2023, 1:37 PMkingsley
12/07/2023, 2:06 PMPoisonedYouth
12/07/2023, 4:25 PMjoney
12/07/2023, 5:27 PMjoney
12/07/2023, 5:29 PMNeil Banman
12/07/2023, 5:38 PMephemient
12/07/2023, 5:45 PMNeil Banman
12/07/2023, 5:47 PMVitaly Legchilkin
12/07/2023, 6:10 PMStefan Niemeyer
12/07/2023, 6:42 PMEgbor Raphael
12/07/2023, 6:50 PMPaul Woitaschek
12/07/2023, 6:58 PMphldavies
12/07/2023, 7:00 PMacc*labels.length + labels.indexOf(c)
but I love the idea of starting with an initial value for the hand type and then just sorting on a single identity of the hand.Paul Woitaschek
12/07/2023, 7:01 PMtodd.ginsberg
12/07/2023, 7:01 PMtodd.ginsberg
12/07/2023, 7:02 PMNeil Banman
12/07/2023, 7:02 PMDavio
12/07/2023, 8:07 PMDavio
12/07/2023, 8:10 PMNorbert Kiesel
12/07/2023, 10:53 PMoverride compareTo(other: Hand) = compareValuesBy(this, other, { it.type }, { it.value })
. First time I used compareValuesBy
, so count that as a win because I learned something new.andriyo
12/07/2023, 10:56 PMandriyo
12/07/2023, 11:02 PMval handTypes = listOf(
listOf(1, 1, 1, 1, 1),
listOf(2, 1, 1, 1),
listOf(2, 2, 1),
listOf(3, 1, 1),
listOf(3, 2),
listOf(4, 1),
listOf(5),
)
looking at this with fresh eyes, it's just another string if concatenate all elements and pad with zeros.. oh wellNorbert Kiesel
12/07/2023, 11:04 PM.joinToString("")
Neil Banman
12/08/2023, 12:07 AMandriyo
12/08/2023, 12:12 AMNeil Banman
12/08/2023, 12:13 AMandriyo
12/08/2023, 12:19 AMtodd.ginsberg
12/08/2023, 12:40 AMNorbert Kiesel
12/08/2023, 12:47 AMhands.sorted()
should work. If we would have been asked to sort based on personal preferences of elfs or camels, then I would choose hands.sortedWith(...)
.Tolly Kulczycki
12/08/2023, 2:42 PM