-
Notifications
You must be signed in to change notification settings - Fork 0
/
day07.go
248 lines (203 loc) · 3.76 KB
/
day07.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
_ "embed"
"fmt"
"sort"
"strconv"
"strings"
)
//go:embed input.txt
var input string
type handType int
const (
highCard handType = iota
onePair
twoPair
threeKind
fullHouse
fourKind
fiveKind
)
type hand struct {
cards [5]int
hType handType
bid int
}
func cardValue(card byte) int {
if card >= '2' && card <= '9' {
return int(card - 50)
}
switch card {
case 'T':
return 8
case 'J':
return 9
case 'Q':
return 10
case 'K':
return 11
case 'A':
return 12
}
return 0
}
func cardValueJoker(card byte) int {
if card >= '2' && card <= '9' {
return int(card - 49)
}
switch card {
case 'J':
return 0
case 'T':
return 9
case 'Q':
return 10
case 'K':
return 11
case 'A':
return 12
}
return 0
}
func getHandType(hand [5]int) handType {
amount := make(map[int]int)
for _, v := range hand {
amount[v]++
}
var firstMax, secondMax int
for _, v := range amount {
if v > firstMax {
secondMax = firstMax
firstMax = v
} else if v > secondMax {
secondMax = v
}
}
switch {
case firstMax == 5:
return fiveKind
case firstMax == 4:
return fourKind
case firstMax == 3 && secondMax == 2:
return fullHouse
case firstMax == 3:
return threeKind
case firstMax == 2 && secondMax == 2:
return twoPair
case firstMax == 2:
return onePair
default:
return highCard
}
}
func getHandTypeJoker(hand [5]int) handType {
amount := make(map[int]int)
for _, v := range hand {
amount[v]++
}
var firstMax, secondMax int
for k, v := range amount {
if k != 0 {
if v > firstMax {
secondMax = firstMax
firstMax = v
} else if v > secondMax {
secondMax = v
}
}
}
firstMax += amount[0]
switch {
case firstMax == 5:
return fiveKind
case firstMax == 4:
return fourKind
case firstMax == 3 && secondMax == 2:
return fullHouse
case firstMax == 3:
return threeKind
case firstMax == 2 && secondMax == 2:
return twoPair
case firstMax == 2:
return onePair
default:
return highCard
}
}
func parseSet(input string) []hand {
lines := strings.Split(input, "\n")
set := make([]hand, len(lines))
for i, line := range lines {
set[i].cards = [5]int{
cardValue(line[0]),
cardValue(line[1]),
cardValue(line[2]),
cardValue(line[3]),
cardValue(line[4]),
}
bid, _ := strconv.Atoi(line[6:])
set[i].hType = getHandType(set[i].cards)
set[i].bid = bid
}
return set
}
func parseSetJoker(input string) []hand {
lines := strings.Split(input, "\n")
set := make([]hand, len(lines))
for i, line := range lines {
set[i].cards = [5]int{
cardValueJoker(line[0]),
cardValueJoker(line[1]),
cardValueJoker(line[2]),
cardValueJoker(line[3]),
cardValueJoker(line[4]),
}
bid, _ := strconv.Atoi(line[6:])
set[i].hType = getHandTypeJoker(set[i].cards)
set[i].bid = bid
}
return set
}
func part1(input string) int {
set := parseSet(input)
sort.Slice(set, func(i, j int) bool {
if set[i].hType != set[j].hType {
return set[i].hType < set[j].hType
}
for l := 0; l < len(set[i].cards); l++ {
if set[i].cards[l] != set[j].cards[l] {
return set[i].cards[l] < set[j].cards[l]
}
}
return false
})
var sum int
for i, part := range set {
sum += part.bid * (i + 1)
}
return sum
}
func part2(input string) int {
set := parseSetJoker(input)
sort.Slice(set, func(i, j int) bool {
if set[i].hType != set[j].hType {
return set[i].hType < set[j].hType
}
for l := 0; l < len(set[i].cards); l++ {
if set[i].cards[l] != set[j].cards[l] {
return set[i].cards[l] < set[j].cards[l]
}
}
return false
})
var sum int
for i, part := range set {
sum += part.bid * (i + 1)
}
return sum
}
func main() {
fmt.Println("--- 2023 day 07 answer ---")
fmt.Println("part 1:\t", part1(input))
fmt.Println("part 2:\t", part2(input))
}