shorten 7

This commit is contained in:
basicbonobo 2023-12-17 14:35:17 +01:00
parent 993d7f9241
commit 3d6bf4f154
1 changed files with 22 additions and 31 deletions

View File

@ -20,9 +20,11 @@ fun card_count(hand: List<Int>): List<Pair<Int, Int>> = hand
/**
* In: [10, 5, ...
* Out: 10*16^4 + 5*16^3 + 5*16^2 + 11*16 + 5 + 3*16^5 = 3823029
* or
* 10*16^4 + 5*16^3 + 5*16^2 + -1*16 + 5 + 5*16^5 = 5919989
*/
fun score(h: List<Int>): Int {
return hand_absrank(card_count(h)) * 1048576 +
fun score(h: List<Int>, preprocess: (List<Pair<Int, Int>>) -> List<Pair<Int, Int>> = { x -> x }): Int {
return hand_absrank(preprocess(card_count(h))) * 1048576 +
h.foldRight(Pair(1, 0)) { it, (mul, acc) -> Pair(mul * 16, acc + it * mul) }.second
}
@ -39,43 +41,32 @@ fun jokered(h: List<Pair<Int, Int>>): List<Pair<Int, Int>> {
}}
}
/**
* In: [10, 5, ...
* Out: 10*16^4 + 5*16^3 + 5*16^2 + -1*16 + 5 + 5*16^5 = 5919989
*/
fun twore(h: List<Int>): Int {
return hand_absrank(jokered(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] ?:
fun cards(hand: String, jack_value: Int = 11): List<Int> = hand.toCharArray() // T55J5
.map { mapOf<Char, Int>('T' to 10, 'J' to jack_value, 'Q' to 12, 'K' to 13, 'A' to 14)[it] ?:
(it - '0').toInt() }
fun twards(hand: String): List<Int> = hand.toCharArray()
.map { mapOf<Char, Int>('T' to 10, 'J' to -1, 'Q' to 12, 'K' to 13, 'A' to 14)[it] ?:
(it - '0').toInt() }
fun solve(
hands_and_bids: List<List<String>>,
jack_value: Int = 11,
fudge: (List<Pair<Int, Int>>) -> List<Pair<Int, Int>> = { it }
): Int {
val sequenced = hands_and_bids.map { Pair<List<Int>, Int>(cards(it[0], jack_value), it[1].toInt()) }
val scored = sequenced.map { (hand, bid) -> listOf(hand, bid, score(hand, fudge)) }
val sorted = scored.toList().sortedBy { it[2] as Int }
val ranked = sorted.mapIndexed { i, it ->
Pair(i + 1, it[1]) }
val sum = ranked.fold(0) { acc, (rank, bid) -> acc + rank * bid as Int}
return sum
}
fun main() {
val hand_bid_strs = generateSequence(::readLine).map { it // "T55J5 684"
val hands_and_bids = generateSequence(::readLine).map { it // "T55J5 684"
.split(' ') // "T55J5" "684"
}.toList()
val hands = hand_bid_strs.map { Pair<List<Int>, Int>(cards(it[0]), it[1].toInt()) }
val scored = hands.map { (hand, bid) -> listOf(hand, bid, score(hand)) }
val sorted = scored.toList().sortedBy { it[2] as Int }
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)
val twands = hand_bid_strs.map { Pair<List<Int>, Int>(twards(it[0]), it[1].toInt()) }
val twored = twands.map { (hand, bid) -> listOf(hand, bid, twore(hand)) }
val tworted = twored.toList().sortedBy { it[2] as Int }
val twanked = tworted.mapIndexed { i, l ->
Pair(i + 1, l[1]) }
val twum = twanked.fold(0) { acc, (rank, bid) -> acc + rank * bid as Int}
println(twum)
println(solve(hands_and_bids))
println(solve(hands_and_bids, -1, ::jokered))
}