This commit is contained in:
parent
15b75bbbcd
commit
263b702d80
|
@ -10,3 +10,4 @@ cookie
|
|||
tags
|
||||
22
|
||||
TAGS
|
||||
*.jar
|
||||
|
|
|
@ -0,0 +1,49 @@
|
|||
/**
|
||||
* absolute ranking (as opposed to hands ranked against another set of hands)
|
||||
* only care about the partitioning, not the pips
|
||||
*/
|
||||
fun hand_absrank(h: List<Pair<Int, Int>>): Int = h
|
||||
.fold("") { acc, (_, r) -> acc + r.toString() }
|
||||
.let {
|
||||
mapOf("5" to 6, "41" to 5, "32" to 4, // https://oeis.org/A322761
|
||||
"311" to 3, "221" to 2, "2111" to 1, "11111" to 0)[it]!!
|
||||
}
|
||||
|
||||
/**
|
||||
* In: [10, 5, ...
|
||||
* Out: [(5, 3), ...
|
||||
*/
|
||||
fun card_count(hand: List<Int>): List<Pair<Int, Int>> = hand
|
||||
.groupingBy { it }.eachCount()
|
||||
.toList().sortedByDescending { it.second }
|
||||
|
||||
/**
|
||||
* In: [10, 5, ...
|
||||
* Out: 10*16^4 + 5*16^3 + 5*16^2 + 11*16 + 5 + 3*16^5 = 3823029
|
||||
*/
|
||||
fun score(h: List<Int>): Int {
|
||||
return hand_absrank(card_count(h)) * 1048576 +
|
||||
h.foldRight(Pair(1, 0)) { it, (mul, acc) -> Pair(mul * 16, acc + it * mul) }.second
|
||||
}
|
||||
|
||||
/**
|
||||
* In: T55J5
|
||||
* Out: [10, 5, ...]
|
||||
*/
|
||||
fun cards(hand: String): List<Int> = hand.toCharArray() // T55J5
|
||||
.map { mapOf<Char, Int>('T' to 10, 'J' to 11, 'Q' to 12, 'K' to 13, 'A' to 14)[it] ?:
|
||||
(it - '0').toInt() }
|
||||
|
||||
fun main() {
|
||||
val hands = generateSequence(::readLine).map { it // "T55J5 684"
|
||||
.split(' ') // "T55J5" "684"
|
||||
.let { Pair<List<Int>, Int>(cards(it[0]), it[1].toInt()) }
|
||||
} // [10, 5, ...], 684
|
||||
val scored = hands.map { (hand, bid) -> listOf(hand, bid, score(hand)) }
|
||||
val sorted = scored.toList().sortedBy { it[2] as Int }
|
||||
//sorted.forEach { println(it) }
|
||||
val ranked = sorted.mapIndexed { i, l ->
|
||||
Pair(i + 1, l[1]) }
|
||||
val sum = ranked.fold(0) { acc, (rank, bid) -> acc + rank * bid as Int}
|
||||
println(sum)
|
||||
}
|
Loading…
Reference in New Issue