-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.go
109 lines (94 loc) · 1.52 KB
/
common.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
package main
import (
"bufio"
"fmt"
"log"
"os"
"sort"
"strconv"
"strings"
)
type Game struct {
Hand string
Bid int
Type int
}
const (
HighCard = iota
OnePair
TwoPair
ThreeOfAKind
FullHouse
FourOfAKind
FiveOfAKind
)
func main() {
games := parse()
for i := 0; i < len(games); i++ {
games[i].Type = Type(games[i].Hand)
}
sort.Slice(games, func(i, j int) bool {
if games[i].Type < games[j].Type {
return true
}
if games[i].Type > games[j].Type {
return false
}
for c := 0; c < 5; c++ {
card1, card2 := value[games[i].Hand[c]], value[games[j].Hand[c]]
if card1 < card2 {
return true
}
if card1 > card2 {
return false
}
}
return false
})
winning := 0
for i, g := range games {
winning += g.Bid * (i + 1)
}
fmt.Println(winning)
}
func parse() []Game {
scanner := bufio.NewScanner(os.Stdin)
var games []Game
for scanner.Scan() {
fields := strings.Fields(scanner.Text())
hand := fields[0]
bid, _ := strconv.Atoi(fields[1])
games = append(games, Game{
Hand: hand,
Bid: bid,
})
}
return games
}
func getTypeByCount(count map[rune]int) int {
values := sort.IntSlice{}
for _, v := range count {
values = append(values, v)
}
sort.Sort(sort.Reverse(values))
switch values[0] {
case 5:
return FiveOfAKind
case 4:
return FourOfAKind
case 3:
if values[1] == 2 {
return FullHouse
}
return ThreeOfAKind
case 2:
if values[1] == 2 {
return TwoPair
}
return OnePair
case 1:
return HighCard
}
log.Fatal("impossible")
return 0
}