🎄Advent of Code - day7 P2 ✨
This commit is contained in:
parent
fbbbf35a51
commit
00fd42aa67
2 changed files with 96 additions and 3 deletions
|
@ -1,6 +1,7 @@
|
||||||
package AdventOfCode2023.day7
|
package AdventOfCode2023.day7
|
||||||
|
|
||||||
import AdventOfCode.Puzzle
|
import AdventOfCode.Puzzle
|
||||||
|
import kotlin.math.max
|
||||||
|
|
||||||
data class Card(val data: String, val bid: Int) {
|
data class Card(val data: String, val bid: Int) {
|
||||||
constructor(data: List<String>) : this(data[0], data[1].toInt())
|
constructor(data: List<String>) : this(data[0], data[1].toInt())
|
||||||
|
@ -46,6 +47,96 @@ data class Card(val data: String, val bid: Int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun Map<Char, Int>.keyForMaxValue(): Char {
|
||||||
|
var maxValue = entries.first().value
|
||||||
|
var maxKey = entries.first().key
|
||||||
|
|
||||||
|
for (entry in entries) {
|
||||||
|
// skip joker
|
||||||
|
if (entry.value > maxValue) {
|
||||||
|
maxValue = entry.value
|
||||||
|
maxKey = entry.key
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return maxKey
|
||||||
|
}
|
||||||
|
|
||||||
|
data class CardWithJoker(val data: String, val bid: Int) {
|
||||||
|
constructor(data: List<String>) : this(data[0], data[1].toInt())
|
||||||
|
|
||||||
|
/*
|
||||||
|
32T3K is still the only one pair; it doesn't contain any jokers, so its strength doesn't increase.
|
||||||
|
KK677 is now the only two pair, making it the second-weakest hand.
|
||||||
|
T55J5, KTJJT, and QQQJA are now all four of a kind! T55J5 gets rank 3, QQQJA gets rank 4, and KTJJT gets rank 5.
|
||||||
|
*/
|
||||||
|
operator fun compareTo(second: CardWithJoker?): Int {
|
||||||
|
val cardValues = listOf("", "J", "2", "3", "4", "5", "6", "7", "8", "9", "T", "Q", "K", "A")
|
||||||
|
if (second == null) {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
// ADD joker to stronger group in map
|
||||||
|
val thisGroups = this.data.groupBy { it }.let {
|
||||||
|
val newMap = mutableMapOf<Char, Int>()
|
||||||
|
var jokerFactor = 0
|
||||||
|
for (entry in it.entries) {
|
||||||
|
if (entry.key == 'J') {
|
||||||
|
jokerFactor = entry.value.count()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newMap[entry.key] = entry.value.count()
|
||||||
|
}
|
||||||
|
// we have jokers only
|
||||||
|
if (newMap.isEmpty()) {
|
||||||
|
newMap['J'] = 5
|
||||||
|
return@let newMap
|
||||||
|
}
|
||||||
|
val maxEntry: Char = newMap.keyForMaxValue()
|
||||||
|
newMap[maxEntry] = newMap[maxEntry]?.plus(jokerFactor)!!
|
||||||
|
newMap
|
||||||
|
}.map { it.value }.sortedDescending()
|
||||||
|
val secondGroups = second.data.groupBy { it }.let {
|
||||||
|
val newMap = mutableMapOf<Char, Int>()
|
||||||
|
var jokerFactor = 0
|
||||||
|
for (entry in it.entries) {
|
||||||
|
if (entry.key == 'J') {
|
||||||
|
jokerFactor = entry.value.count()
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
newMap[entry.key] = entry.value.count()
|
||||||
|
}
|
||||||
|
// we have jokers only
|
||||||
|
if (newMap.isEmpty()) {
|
||||||
|
newMap['J'] = 5
|
||||||
|
return@let newMap
|
||||||
|
}
|
||||||
|
val maxEntry: Char = newMap.keyForMaxValue()
|
||||||
|
newMap[maxEntry] = newMap[maxEntry]?.plus(jokerFactor)!!
|
||||||
|
newMap
|
||||||
|
}.map { it.value }.sortedDescending()
|
||||||
|
|
||||||
|
for (i in thisGroups.indices) {
|
||||||
|
if (thisGroups[i] > secondGroups[i]) {
|
||||||
|
return 1
|
||||||
|
} else if (thisGroups[i] < secondGroups[i]) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compare high cards if the TYPE of hands are the same
|
||||||
|
for (i in this.data.indices) {
|
||||||
|
val firstCardPower = cardValues.indexOf<String>(element = this.data[i].toString())
|
||||||
|
val secondCardPower = cardValues.indexOf<String>(element = second.data[i].toString())
|
||||||
|
if (firstCardPower>secondCardPower) {
|
||||||
|
return 1
|
||||||
|
} else if (firstCardPower < secondCardPower) {
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class CamelCards : Puzzle("2023", "7") {
|
class CamelCards : Puzzle("2023", "7") {
|
||||||
override fun partOne() {
|
override fun partOne() {
|
||||||
inputData.map { Card(it.split(Regex("\\s"))) }.sortedWith(Card::compareTo).mapIndexed { i, card ->
|
inputData.map { Card(it.split(Regex("\\s"))) }.sortedWith(Card::compareTo).mapIndexed { i, card ->
|
||||||
|
@ -54,6 +145,8 @@ class CamelCards : Puzzle("2023", "7") {
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun partTwo() {
|
override fun partTwo() {
|
||||||
TODO("Not yet implemented")
|
inputData.map { CardWithJoker(it.split(Regex("\\s"))) }.sortedWith(CardWithJoker::compareTo).mapIndexed { i, card ->
|
||||||
|
card.bid * (i + 1)
|
||||||
|
}.sum().also { println("The sum is $it") }
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -4,8 +4,8 @@ import kotlin.system.measureTimeMillis
|
||||||
fun main(args: Array<String>) {
|
fun main(args: Array<String>) {
|
||||||
val t = CamelCards()
|
val t = CamelCards()
|
||||||
val time = measureTimeMillis {
|
val time = measureTimeMillis {
|
||||||
t.partOne()
|
//t.partOne()
|
||||||
// t.partTwo()
|
t.partTwo()
|
||||||
}
|
}
|
||||||
println("Took $time ms.")
|
println("Took $time ms.")
|
||||||
}
|
}
|
Loading…
Reference in a new issue