-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day7.kt
39 lines (34 loc) · 1.58 KB
/
Day7.kt
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
35
36
37
38
39
package com.github.ephemient.aoc2023
class Day7(input: String) {
private val hands = input.lines().mapNotNull { line ->
val (hand, bid) = line.split(' ', limit = 2).takeIf { it.size == 2 } ?: return@mapNotNull null
IndexedValue(bid.toIntOrNull() ?: return@mapNotNull null, hand)
}
private fun solve(cards: String): Int = hands
.map { (bid, hand) -> IndexedValue(bid, hand.map(cards::indexOf)) }
.sortedWith(compareBy(comparator, selector = { it.value }))
.withIndex()
.sumOf { (i, value) -> (i + 1) * value.index }
fun part1(): Int = solve("23456789TJQKA")
fun part2(): Int = solve("23456789TQKA")
companion object {
private val comparator = compareBy<List<Int>> { hand ->
val counts = hand.filter { it >= 0 }.groupingBy { it }.eachCount().values.sortedDescending()
val jokers = hand.count { it < 0 }
when {
counts.getOrElse(0) { 0 } + jokers >= 5 -> 6
counts.getOrElse(0) { 0 } + jokers >= 4 -> 5
counts.getOrElse(0) { 0 } + counts.getOrElse(1) { 0 } + jokers >= 5 -> 4
counts.getOrElse(0) { 0 } + jokers >= 3 -> 3
counts.getOrElse(0) { 0 } + counts.getOrElse(1) { 0 } + jokers >= 4 -> 2
counts.getOrElse(0) { 0 } + jokers >= 2 -> 1
else -> 0
}
}.thenComparator { a, b ->
for (i in 0..minOf(a.lastIndex, b.lastIndex)) {
if (a[i] != b[i]) return@thenComparator a[i] - b[i]
}
a.size - b.size
}
}
}