-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (95 loc) · 2.07 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
_ "embed"
"fmt"
"slices"
"strconv"
"strings"
)
//go:embed input.txt
var input string
var kind = []string{"H", "1P", "2P", "3", "F", "4", "5"}
var cards = []string{"2", "3", "4", "5", "6", "7", "8", "9", "T", "J", "Q", "K", "A"}
type Hand struct {
cards []string
bid int
kind int
cardValues []int
}
/**
* Day 7: Camel Cards - Part 1
* url: https://adventofcode.com/2023/day/7
*/
func main() {
hands := sortHand(parseHands())
sum := 0
for i, hand := range hands {
sum += (i + 1) * hand.bid
fmt.Println("-", i, hand.bid, hand.kind, hand.cards)
}
fmt.Println("Part 1 =", sum)
}
func parseHands() []Hand {
hands := []Hand{}
for _, s := range strings.Split(strings.TrimSpace(input), "\n") {
str := strings.Split(s, " ")
num, _ := strconv.Atoi(strings.TrimSpace(str[1]))
hand := Hand{cards: strings.Split(str[0], ""), bid: num}
evaluateHand(&hand)
hands = append(hands, hand)
}
return hands
}
func sortHand(hands []Hand) []Hand {
slices.SortFunc(hands, func(a, b Hand) int {
if a.kind == b.kind {
for i := 0; i < len(a.cardValues); i++ {
if a.cardValues[i] == b.cardValues[i] {
continue
}
return a.cardValues[i] - b.cardValues[i]
}
}
return a.kind - b.kind
})
return hands
}
func evaluateHand(hand *Hand) {
count := map[int]int{}
values := []int{}
for _, card := range hand.cards {
cardValue := slices.Index(cards, card)
count[cardValue]++
values = append(values, cardValue)
}
kinds := map[string]int{"5": 0, "4": 0, "F": 0, "3": 0, "2P": 0, "1P": 0, "H": 0}
for _, cpt := range count {
if cpt == 5 {
kinds["5"] += 1
} else if cpt == 4 {
kinds["4"] += 1
} else if cpt == 3 {
kinds["3"] += 1
} else if cpt == 2 {
kinds["1P"] += 1
}
}
if kinds["3"] == 1 && kinds["1P"] == 1 {
kinds["3"] -= 1
kinds["1P"] -= 1
kinds["F"] += 1
}
if kinds["1P"] > 1 {
kinds["1P"] -= 2
kinds["2P"] += 1
}
maxKind := 0
for idx, val := range kinds {
if val == 0 {
continue
}
maxKind = slices.Index(kind, idx)
}
hand.kind = maxKind
hand.cardValues = values
}