Browse Source

complete day 7

master
Alexander Avery 5 months ago
parent
commit
436baf433e
  1. 148
      day07.go
  2. 1000
      input/7-2
  3. 6
      main_test.go

148
day07.go

@ -141,7 +141,7 @@ func camelrank(hand hand) (rank, []int) {
}
if seenPair {
return onePair, hand.values
return onePair, hand.values
} else if seenThreeOfAKind {
return threeOfAKind, hand.values
}
@ -149,6 +149,123 @@ func camelrank(hand hand) (rank, []int) {
return highCard, hand.values
}
func camelrankjoker(hand hand) (rank, []int, int) {
counts := make([]int, 14)
jokers := 0
hand.values = make([]int, len(hand.cards))
for i, r := range hand.cards {
switch r {
case '1':
counts[0] = counts[0] + 1
hand.values[i] = 1
case '2':
counts[1] = counts[1] + 1
hand.values[i] = 2
case '3':
counts[2] = counts[2] + 1
hand.values[i] = 3
case '4':
counts[3] = counts[3] + 1
hand.values[i] = 4
case '5':
counts[4] = counts[4] + 1
hand.values[i] = 5
case '6':
counts[5] = counts[5] + 1
hand.values[i] = 6
case '7':
counts[6] = counts[6] + 1
hand.values[i] = 7
case '8':
counts[7] = counts[7] + 1
hand.values[i] = 8
case '9':
counts[8] = counts[8] + 1
hand.values[i] = 9
case 'T':
counts[9] = counts[9] + 1
hand.values[i] = 10
case 'J':
jokers++
hand.values[i] = 1
case 'Q':
counts[11] = counts[11] + 1
hand.values[i] = 12
case 'K':
counts[12] = counts[12] + 1
hand.values[i] = 13
case 'A':
counts[13] = counts[13] + 1
hand.values[i] = 14
}
}
var (
seenPair bool
seenThreeOfAKind bool
)
for _, count := range counts {
switch count {
case 0:
case 1:
continue
case 2:
if seenPair {
return twoPair, hand.values, jokers
} else if seenThreeOfAKind {
return fullHouse, hand.values, jokers
}
seenPair = true
case 3:
if seenPair {
return fullHouse, hand.values, jokers
}
seenThreeOfAKind = true
case 4:
return fourOfAKind, hand.values, jokers
case 5:
return fiveOfAKind, hand.values, jokers
}
}
if seenPair {
return onePair, hand.values , jokers
} else if seenThreeOfAKind {
return threeOfAKind, hand.values, jokers
}
return highCard, hand.values, jokers
}
func upgraderank(hand hand, jokers int) rank {
var upgrades [4]rank
switch hand.rank {
case highCard:
if jokers == 5 {
return fiveOfAKind
}
upgrades = [4]rank{onePair, threeOfAKind, fourOfAKind, fiveOfAKind}
case onePair:
upgrades = [4]rank{threeOfAKind, fourOfAKind, fiveOfAKind, fiveOfAKind}
case twoPair:
upgrades = [4]rank{fullHouse, fullHouse, fullHouse, fullHouse}
case threeOfAKind:
upgrades = [4]rank{fourOfAKind, fiveOfAKind, fiveOfAKind, fiveOfAKind}
case fullHouse:
upgrades = [4]rank{fullHouse, fullHouse, fullHouse, fullHouse}
case fourOfAKind:
upgrades = [4]rank{fiveOfAKind, fiveOfAKind, fiveOfAKind, fiveOfAKind}
case fiveOfAKind:
upgrades = [4]rank{fiveOfAKind, fiveOfAKind, fiveOfAKind, fiveOfAKind}
}
if jokers > 0 {
return upgrades[jokers-1]
}
return hand.rank
}
func (d day07) solve1(r io.Reader) string {
total := 0
var hands hands = make([]hand, 0)
@ -179,5 +296,32 @@ func (d day07) solve1(r io.Reader) string {
}
func (d day07) solve2(r io.Reader) string {
return "incomplete"
total := 0
var hands hands = make([]hand, 0)
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanWords)
for i, w := 0, 0; scanner.Scan(); w++ {
word := scanner.Text()
if w%2 == 0 {
hand := hand{cards: []rune(word)}
var jokers int
hand.rank, hand.values, jokers = camelrankjoker(hand)
hand.rank = upgraderank(hand, jokers)
hands = append(hands, hand)
} else {
val, err := strconv.ParseInt(word, 10, 64)
if err != nil {
panic(err)
}
hands[i].bid = int(val)
i++
}
}
sort.Sort(hands)
for i, hand := range hands {
total += hand.bid * (i + 1)
}
return fmt.Sprintf("%d", total)
}

1000
input/7-2

File diff suppressed because it is too large

6
main_test.go

@ -189,4 +189,10 @@ QQQJA 483`
if ans != res {
t.Errorf("solve1() = %s; wanted %s", res, ans)
}
ans = "5905"
res = puzzle.solve2(strings.NewReader(input))
if ans != res {
t.Errorf("solve2() = %s; wanted %s", res, ans)
}
}

Loading…
Cancel
Save