mirror of
https://github.com/onyx-and-iris/aoc2023.git
synced 2024-11-25 03:40:49 +00:00
109 lines
1.8 KiB
Go
109 lines
1.8 KiB
Go
package main
|
|
|
|
var hands = []*hand{}
|
|
|
|
type hand struct {
|
|
cards string
|
|
bid int
|
|
matches map[rune]int
|
|
numPairs int
|
|
_kind int
|
|
}
|
|
|
|
func newHand(cards string, bid int) *hand {
|
|
h := &hand{cards: cards, bid: bid}
|
|
setKind(h)
|
|
return h
|
|
}
|
|
|
|
func (h *hand) kind() int {
|
|
return h._kind
|
|
}
|
|
|
|
func (h *hand) getMatches() map[rune]int {
|
|
if h.matches == nil {
|
|
h.matches = matches(h.cards)
|
|
}
|
|
return h.matches
|
|
}
|
|
|
|
func setKind(h *hand) {
|
|
if isFiveOfAKind(h) {
|
|
h._kind = fiveofakind
|
|
} else if isHighCard(h) {
|
|
h._kind = highcard
|
|
} else if isFourOfAKind(h) {
|
|
h._kind = fourofakind
|
|
} else if isFullHouse(h) {
|
|
h._kind = fullhouse
|
|
} else if isThreeOfAKind(h) {
|
|
h._kind = threeofakind
|
|
} else if isTwoPair(h) {
|
|
h._kind = twopair
|
|
} else if isOnePair(h) {
|
|
h._kind = onepair
|
|
}
|
|
}
|
|
|
|
func isFiveOfAKind(h *hand) bool {
|
|
return len(h.getMatches()) == 1
|
|
}
|
|
|
|
func isHighCard(h *hand) bool {
|
|
return len(h.getMatches()) == 5
|
|
}
|
|
|
|
func isFourOfAKind(h *hand) bool {
|
|
if len(h.getMatches()) == 2 {
|
|
for _, c := range h.matches {
|
|
if c == 4 {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
return false
|
|
}
|
|
|
|
func isFullHouse(h *hand) bool {
|
|
if len(h.getMatches()) == 2 {
|
|
return !isFourOfAKind(h)
|
|
}
|
|
return false
|
|
}
|
|
|
|
func isThreeOfAKind(h *hand) bool {
|
|
if len(h.getMatches()) == 3 {
|
|
for _, c := range h.matches {
|
|
if c == 3 {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
return false
|
|
}
|
|
|
|
func getNumPairs(h *hand) int {
|
|
if h.numPairs == 0 {
|
|
allowed := []int{1, 2}
|
|
for _, n := range h.getMatches() {
|
|
if !contains(allowed, n) {
|
|
return -1 // there may be a pair but its a fullhouse
|
|
}
|
|
if n == 2 {
|
|
h.numPairs++
|
|
}
|
|
}
|
|
}
|
|
return h.numPairs
|
|
}
|
|
|
|
func isTwoPair(h *hand) bool {
|
|
return getNumPairs(h) == 2
|
|
}
|
|
|
|
func isOnePair(h *hand) bool {
|
|
return !isTwoPair(h) && h.numPairs == 1
|
|
}
|