-
Notifications
You must be signed in to change notification settings - Fork 0
/
p54.go
185 lines (154 loc) · 2.74 KB
/
p54.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
package main
import (
"bufio"
"fmt"
"os"
"time"
)
func Rank(card []rune, val map[rune]int) (int, int) {
straight := true
flush := true
for i := 2; i < 10; i += 2 {
if val[card[i]]-1 != val[card[i-2]] {
straight = false
break
}
}
for i := 3; i < 10; i += 2 {
if card[i] != card[i-2] {
flush = false
break
}
}
if straight && flush {
// Royal Flush
if card[0] == rune('T') {
return 9, val[card[8]]
}
// Straight Flush
return 8, val[card[8]]
}
cnt := make([]int, len(val))
for i := 0; i < 10; i += 2 {
cnt[val[card[i]]]++
}
four := 0
three := 0
two := 0
fourVal := 0
threeVal := 0
twoVal := 0
for i, c := range cnt {
if c == 4 {
four++
fourVal = i
} else if c == 3 {
three++
threeVal = i
} else if c == 2 {
two++
twoVal = i
}
}
// Four of a Kind
if four > 0 {
return 7, fourVal
}
// Full House
if three > 0 && two > 0 {
return 6, threeVal
}
// Flush
if flush {
return 5, val[card[8]]
}
// Straight
if straight {
return 4, val[card[8]]
}
// Three of a Kind
if three > 0 {
return 3, threeVal
}
// Two Pair
if two > 1 {
return 2, 0
}
// Pair
if two > 0 {
return 1, twoVal
}
return 0, val[card[8]]
}
func main() {
start := time.Now()
f, _ := os.Open("./data/d54")
scanner := bufio.NewScanner(f)
scanner.Split(bufio.ScanWords)
val := make(map[rune]int)
val[rune('2')] = 0
val[rune('3')] = 1
val[rune('4')] = 2
val[rune('5')] = 3
val[rune('6')] = 4
val[rune('7')] = 5
val[rune('8')] = 6
val[rune('9')] = 7
val[rune('T')] = 8
val[rune('J')] = 9
val[rune('Q')] = 10
val[rune('K')] = 11
val[rune('A')] = 12
A := make([]rune, 10)
B := make([]rune, 10)
ans := 0
for i := 0; i < 1000; i++ {
for j := 0; j < 5; j++ {
scanner.Scan()
text := scanner.Text()
A[j*2] = rune(text[0])
A[j*2+1] = rune(text[1])
}
for j := 0; j < 5; j++ {
scanner.Scan()
text := scanner.Text()
B[j*2] = rune(text[0])
B[j*2+1] = rune(text[1])
}
for i := 0; i < 5; i++ {
for j := i + 1; j < 5; j++ {
if val[A[i*2]] > val[A[j*2]] {
A[i*2], A[j*2] = A[j*2], A[i*2]
A[i*2+1], A[j*2+1] = A[j*2+1], A[i*2+1]
}
if val[B[i*2]] > val[B[j*2]] {
B[i*2], B[j*2] = B[j*2], B[i*2]
B[i*2+1], B[j*2+1] = B[j*2+1], B[i*2+1]
}
}
}
rankA, valA := Rank(A, val)
rankB, valB := Rank(B, val)
if rankA > rankB {
ans++
} else if rankA == rankB {
if valA > valB {
ans++
} else if valA == valB {
for i := 8; i > -1; i -= 2 {
if val[A[i]] > val[B[i]] {
ans++
fmt.Println(string(A[i]), string(B[i]))
break
} else if val[A[i]] < val[B[i]] {
break
}
}
}
}
}
fmt.Println(ans)
f.Close()
elapsed := time.Since(start)
fmt.Println("Elasped:", elapsed)
}