day7, part2

This commit is contained in:
efim 2023-12-07 07:37:43 +00:00
parent 28aef4c64c
commit d959473a29
1 changed files with 41 additions and 20 deletions

View File

@ -14,7 +14,11 @@ func Run() int {
filename := "day7/input"
input := ReadFile(filename)
slices.SortFunc(input, CompareHands)
log.Printf("read the input %+v\n", input)
// log.Printf("read the input %+v\n", input)
for _, hand := range input {
// fmt.Printf("%+v\n", hand)
fmt.Printf("%+s\t\t%d\n", hand.Cards, hand.HandType)
}
slices.SortFunc([]int{}, func(a int, b int) int { return 0 })
result := 0
for i, hand := range input {
@ -44,7 +48,7 @@ type Hand struct {
// cmp(a, b) should return a negative number when a < b, a positive number when
// a > b and zero when a == b.
const values string = "23456789TJQKA"
const values string = "J23456789TQKA"
func CompareHands(a Hand, b Hand) int {
if a.HandType != b.HandType {
@ -67,27 +71,44 @@ func compareString(a string, b string) int {
func GetHandType(groupped map[rune]int) HandType {
var handType HandType
sizes := make([]int, 0, len(groupped))
for _, size := range groupped {
sizes = append(sizes, size)
jokerSize, found := groupped['J']
if !found {
jokerSize = 0
}
slices.Sort(sizes)
switch {
case slices.Equal(sizes, []int{5}):
handType = FiveKind
case slices.Equal(sizes, []int{1, 4}):
handType = FourKind
case slices.Equal(sizes, []int{2, 3}):
handType = FullHouse
case slices.Equal(sizes, []int{1, 1, 3}):
handType = ThreeKind
case slices.Equal(sizes, []int{1, 2, 2}):
handType = TwoPair
case slices.Equal(sizes, []int{1, 1, 1, 2}):
handType = OnePair
sizes := make([]int, 0, len(groupped))
for card, size := range groupped {
if card != 'J' {
sizes = append(sizes, size)
}
}
slices.SortFunc(sizes, func (a, b int) int { return b - a })
var highest int
if len(sizes) == 0 {
highest = jokerSize
} else {
highest = sizes[0] + jokerSize
}
if highest == 5 {
return FiveKind
}
if highest == 4 {
return FourKind
}
other := sizes[1]
if highest == 3 {
if other == 2 {
return FullHouse
}
return ThreeKind
}
if highest == 2 {
if other == 2 {
return TwoPair
}
return OnePair
}
log.Printf("calc type for %+v.\nsizes: %+v;\n %d", groupped, sizes, handType)
// log.Printf("calc type for %+v.\nsizes: %+v;\n %d", groupped, sizes, handType)
return handType
}