-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy path10315.go
189 lines (166 loc) · 3.81 KB
/
10315.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
// UVa 10315 - Poker Hands
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
)
type card struct {
value int
suit byte
}
var valueMap = map[byte]int{'T': 10, 'J': 11, 'Q': 12, 'K': 13, 'A': 14}
func straightFlush(c []card) bool { return straight(c) && flush(c) }
func fourOfAKind(c []card) bool { return c[0].value == c[3].value || c[1].value == c[4].value }
func fullHouse(c []card) bool { return threeOfAKind(c) && getValues(c) == 2 }
func flush(c []card) bool {
for i := 1; i < 5; i++ {
if c[i].suit != c[0].suit {
return false
}
}
return true
}
func straight(c []card) bool {
for i := 1; i < 5; i++ {
if c[i].value != c[0].value-i {
return false
}
}
return true
}
func threeOfAKind(c []card) bool {
return c[0].value == c[2].value || c[1].value == c[3].value || c[2].value == c[4].value
}
func twoPairs(c []card) bool { return getValues(c) == 3 }
func pair(c []card) bool { return getValues(c) == 4 }
func getValues(c []card) int {
valueMap := make(map[int]bool)
for _, vi := range c {
valueMap[vi.value] = true
}
return len(valueMap)
}
func buildCard(c string) card {
if v, ok := valueMap[c[0]]; ok {
return card{v, c[1]}
}
return card{int(c[0] - '0'), c[1]}
}
func score(c []card) int {
sort.Slice(c, func(i, j int) bool { return c[i].value > c[j].value })
// order matters
switch {
case straightFlush(c):
return 9
case fourOfAKind(c):
return 8
case fullHouse(c):
return 7
case flush(c):
return 6
case straight(c):
return 5
case threeOfAKind(c):
return 4
case twoPairs(c):
return 3
case pair(c):
return 2
default:
return 1
}
}
func highCard(blacks, whites []card) (int, int) {
for i := range blacks {
if blacks[i].value != whites[i].value {
return blacks[i].value, whites[i].value
}
}
return 0, 0
}
func scoreForPair(c []card) []int {
switch {
case c[0].value == c[1].value:
return []int{c[0].value, c[2].value, c[3].value, c[4].value}
case c[1].value == c[2].value:
return []int{c[1].value, c[0].value, c[3].value, c[4].value}
case c[2].value == c[3].value:
return []int{c[2].value, c[0].value, c[1].value, c[4].value}
default:
return []int{c[3].value, c[0].value, c[1].value, c[2].value}
}
}
func scoreForTwoPairs(c []card) []int {
switch {
case c[0].value != c[1].value:
return []int{c[1].value, c[3].value, c[0].value}
case c[1].value != c[2].value:
return []int{c[0].value, c[3].value, c[2].value}
default:
return []int{c[0].value, c[2].value, c[4].value}
}
}
func compareSameScore(score int, blacks, whites []card) (int, int) {
switch score {
case 9, 5:
return blacks[0].value, whites[0].value
case 8:
return blacks[1].value, whites[1].value
case 7, 4:
return blacks[2].value, whites[2].value
case 6, 1:
return highCard(blacks, whites)
case 3, 2:
var bs, ws []int
if score == 3 {
bs, ws = scoreForTwoPairs(blacks), scoreForTwoPairs(whites)
} else {
bs, ws = scoreForPair(blacks), scoreForPair(whites)
}
for i := range bs {
if bs[i] != ws[i] {
return bs[i], ws[i]
}
}
fallthrough
default:
return 0, 0
}
}
func compare(line string) string {
c := strings.Fields(line)
blacks, whites := make([]card, 5), make([]card, 5)
for i := range blacks {
blacks[i], whites[i] = buildCard(c[i]), buildCard(c[i+5])
}
blackScore, whiteScore := score(blacks), score(whites)
if blackScore == whiteScore {
blackScore, whiteScore = compareSameScore(whiteScore, blacks, whites)
}
switch {
case blackScore > whiteScore:
return "Black wins."
case whiteScore > blackScore:
return "White wins."
default:
return "Tie."
}
}
func main() {
in, _ := os.Open("10315.in")
defer in.Close()
out, _ := os.Create("10315.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
var line string
for s.Scan() {
if line = s.Text(); line == "" {
break
}
fmt.Fprintln(out, compare(line))
}
}