https://kotlinlang.org logo
#advent-of-code
Title
# advent-of-code
s

Safi K

12/08/2023, 6:11 PM
I really need help with day 7 part 1. I spent many hours debugging the code. Is using
Comparator
a bad choice for this problem?
e

ephemient

12/08/2023, 6:13 PM
It is not necessarily a bad choice, but it may be easier to derive a simpler key you can sort by instead
s

Safi K

12/08/2023, 6:15 PM
I found a test input on Reddit and from the result, it looks like two pairs are compared and sorted, not every element with the rest
image.png
I am really out of ideas because at this point it's really hard to debug because of the inner implementation of Comparator interface..
This is how I used it:
d

Dave Leeds

12/08/2023, 6:42 PM
@Safi K - Is your full code up on Github or anywhere?
l

Lidonis Calhau

12/08/2023, 7:57 PM
I used
Comparator
for hands with no real issue
d

Davio

12/08/2023, 9:10 PM
I used a comparator as well, or comparable
But a hand is sorted in two phases, first its strength is determined, high card to five of a kind, and only within those groups hands are sorted by the cards. Note that you have to look at the hands as listed, so A2222 beats 2AAAA because of the first card
👍 1
Maybe one of your comparators sorts the other way around accidentally
e

evgenim

12/08/2023, 10:28 PM
I did with Comparator as-well
n

Norbert Kiesel

12/09/2023, 12:00 AM
I use
override fun compareTo(other: Hand) = compareValuesBy(this, other, { it.type}, { it.value })
with
class Hand(...) : Comparable<Hand>
. My Hand objects have a type (e.g. FULL_HOUSE) and a value (computed from cards), both in ascending order. See https://github.com/nkiesel/AdventOfCode_2023/blob/main/src/test/kotlin/Day07.kt for full code
6 Views