I really need help with day 7 part 1. I spent many...
# advent-of-code
s
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
It is not necessarily a bad choice, but it may be easier to derive a simpler key you can sort by instead
s
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
@Safi K - Is your full code up on Github or anywhere?
l
I used
Comparator
for hands with no real issue
d
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
I did with Comparator as-well
n
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