-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathy23day7part2.kt
More file actions
34 lines (34 loc) · 1.14 KB
/
Copy pathy23day7part2.kt
File metadata and controls
34 lines (34 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
fun main() {
val cards = "A, K, Q, T, 9, 8, 7, 6, 5, 4, 3, 2, J".split(", ")
.reversed().withIndex().associate { (i, it) -> it[0] to i }
fun String.type() = groupingBy { it }.eachCount().filterKeys { it != 'J' }.run {
when {
// Five of a kind
size <= 1 -> 7
// Four of a kind
size == 2 && values.any { it == 1 } -> 6
// Full house
size == 2 -> 5
// Three of a kind
size == 3 && values.count { it == 1 } >= 2 -> 4
// Two pair
size == 3 -> 3
// One pair
size == 4 -> 2
// High card
size == 5 -> 1
else -> error("Game over")
}
}
fun String.rankingCost() = fold(type().toLong()) { acc, c ->
(acc shl 4) + cards.getValue(c)
}
generateSequence { readlnOrNull() }.map { line ->
val hand = line.substringBefore(' ')
val bid = line.substringAfter(' ').toLong()
hand to bid
}
.sortedBy { (hand, _) -> hand.rankingCost() }
.mapIndexed { i, (_, bid) -> (i + 1) * bid }
.sum().let(::println)
}