aoc2023/day-7/two.go
2023-12-07 23:16:47 +00:00

75 lines
1.6 KiB
Go

package main
import log "github.com/sirupsen/logrus"
// promote alters a hands kind based on joker rules
func promote(hand *hand) {
m := matches(hand.cards)
log.Debug(m)
switch hand.kind() {
case fiveofakind:
break
case fourofakind:
hand._kind = fiveofakind
case fullhouse:
if m['J'] == 2 || m['J'] == 3 {
hand._kind = fiveofakind
}
log.Debug(hand.cards, " was promoted to ", hand.kind())
case threeofakind:
if m['J'] == 1 || m['J'] == 3 {
hand._kind = fourofakind
}
log.Debug(hand.cards, " was promoted to ", hand.kind())
case twopair:
if m['J'] == 1 {
hand._kind = fullhouse
} else if m['J'] == 2 {
hand._kind = fourofakind
}
log.Debug(hand.cards, " was promoted to ", hand.kind())
case onepair:
if m['J'] == 1 || m['J'] == 2 {
hand._kind = threeofakind
}
log.Debug(hand.cards, " was promoted to ", hand.kind())
case highcard:
hand._kind = onepair
log.Debug(hand.cards, " was promoted to ", hand.kind())
}
}
// two returns the sum of products of hand values by bids
// it uses new joker rules
func two(lines []string) (int, error) {
for _, hand := range hands {
if containsChar(hand.cards, "J") {
promote(hand)
}
}
strength := []rune{'A', 'K', 'Q', 'T', '9', '8', '7', '6', '5', '4', '3', '2', 'J'}
log.Debug("strength: ", strength)
sortByKindAndStrength(strength)
sum := 0
var debugNames = map[int]string{
0: "highcard",
1: "onepair",
2: "twopair",
3: "threeofakind",
4: "fullhouse",
5: "fourofakind",
6: "fiveofakind",
}
for i, hand := range hands {
log.Debug(hand.cards, ": ", hand.bid, " by ", i+1, " || ", debugNames[hand.kind()])
sum += (i + 1) * hand.bid
}
return sum, nil
}