day7, part2
This commit is contained in:
parent
28aef4c64c
commit
d959473a29
|
@ -14,7 +14,11 @@ func Run() int {
|
||||||
filename := "day7/input"
|
filename := "day7/input"
|
||||||
input := ReadFile(filename)
|
input := ReadFile(filename)
|
||||||
slices.SortFunc(input, CompareHands)
|
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 })
|
slices.SortFunc([]int{}, func(a int, b int) int { return 0 })
|
||||||
result := 0
|
result := 0
|
||||||
for i, hand := range input {
|
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
|
// cmp(a, b) should return a negative number when a < b, a positive number when
|
||||||
// a > b and zero when a == b.
|
// a > b and zero when a == b.
|
||||||
const values string = "23456789TJQKA"
|
const values string = "J23456789TQKA"
|
||||||
|
|
||||||
func CompareHands(a Hand, b Hand) int {
|
func CompareHands(a Hand, b Hand) int {
|
||||||
if a.HandType != b.HandType {
|
if a.HandType != b.HandType {
|
||||||
|
@ -67,27 +71,44 @@ func compareString(a string, b string) int {
|
||||||
|
|
||||||
func GetHandType(groupped map[rune]int) HandType {
|
func GetHandType(groupped map[rune]int) HandType {
|
||||||
var handType HandType
|
var handType HandType
|
||||||
sizes := make([]int, 0, len(groupped))
|
jokerSize, found := groupped['J']
|
||||||
for _, size := range groupped {
|
if !found {
|
||||||
sizes = append(sizes, size)
|
jokerSize = 0
|
||||||
}
|
}
|
||||||
slices.Sort(sizes)
|
sizes := make([]int, 0, len(groupped))
|
||||||
switch {
|
for card, size := range groupped {
|
||||||
case slices.Equal(sizes, []int{5}):
|
if card != 'J' {
|
||||||
handType = FiveKind
|
sizes = append(sizes, size)
|
||||||
case slices.Equal(sizes, []int{1, 4}):
|
}
|
||||||
handType = FourKind
|
}
|
||||||
case slices.Equal(sizes, []int{2, 3}):
|
slices.SortFunc(sizes, func (a, b int) int { return b - a })
|
||||||
handType = FullHouse
|
var highest int
|
||||||
case slices.Equal(sizes, []int{1, 1, 3}):
|
if len(sizes) == 0 {
|
||||||
handType = ThreeKind
|
highest = jokerSize
|
||||||
case slices.Equal(sizes, []int{1, 2, 2}):
|
} else {
|
||||||
handType = TwoPair
|
highest = sizes[0] + jokerSize
|
||||||
case slices.Equal(sizes, []int{1, 1, 1, 2}):
|
}
|
||||||
handType = OnePair
|
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
|
return handType
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue