-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpart2.go
64 lines (62 loc) · 1.09 KB
/
part2.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
package main
import (
"reflect"
"slices"
)
func doPartTwo(input string) int {
hands := getHands(input, true)
Counter := func(H Hand) int {
C := map[int]int{}
for _, c := range H.hand {
C[c]++
}
T := reflect.ValueOf(C).MapKeys()[0].Interface()
for k := range C {
if k != 1 {
if C[k] > C[T.(int)] || T == 1 {
T = k
}
}
}
if _, ok := C[1]; ok && T.(int) != 1 {
C[T.(int)] += C[1]
delete(C, 1)
}
res := []int{}
for _, v := range C {
res = append(res, v)
}
slices.Sort(res)
for _, S := range handscores {
if reflect.DeepEqual(res, S.hand) {
return S.strength
}
}
return 0
}
slices.SortFunc(hands, func(a, b Hand) int {
Ca := Counter(a)
Cb := Counter(b)
if Ca < Cb {
return -1
}
if Ca > Cb {
return 1
}
// If we get here, they have the same score, and so need to iterate through the cards
for i := 0; i < len(a.hand); i++ {
if a.hand[i] < b.hand[i] {
return -1
}
if a.hand[i] > b.hand[i] {
return 1
}
}
return 0
})
res := 0
for i, H := range hands {
res += (i + 1) * H.bid
}
return res
}