This commit is contained in:
basicbonobo 2023-12-07 14:31:08 +01:00
parent 15b75bbbcd
commit 263b702d80
6 changed files with 1059 additions and 0 deletions

1
.gitignore vendored
View File

@ -10,3 +10,4 @@ cookie
tags
22
TAGS
*.jar

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
all:
kotlinc -include-runtime -d k7.jar seven.kt

1000
input7 Normal file

File diff suppressed because it is too large Load Diff

2
run Executable file
View File

@ -0,0 +1,2 @@
#!/usr/bin/env bash
exec java -jar k7.jar

49
seven.kt Normal file
View File

@ -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)
}

5
test7 Normal file
View File

@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483