-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution1275.go
More file actions
70 lines (65 loc) · 1.34 KB
/
Copy pathsolution1275.go
File metadata and controls
70 lines (65 loc) · 1.34 KB
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
package solution1275
// ============================================================================
// 1275. Find Winner on a Tic Tac Toe Game
// URL: https://leetcode.com/problems/find-winner-on-a-tic-tac-toe-game/
// ============================================================================
/*
go test -bench=. -benchmem
goos: linux
goarch: amd64
pkg: GoLeetCode/solutions/1275---Find-Winner-on-a-Tic-Tac-Toe-Game
cpu: 13th Gen Intel(R) Core(TM) i7-13700K
Benchmark_tictactoe-24 19551152 61.16 ns/op 0 B/op 0 allocs/op
PASS
*/
func tictactoe(moves [][]int) string {
boardA := uint(0)
boardB := uint(0)
victories := [8]uint{
0b_111000000,
0b_000111000,
0b_000000111,
0b_100100100,
0b_010010010,
0b_001001001,
0b_100010001,
0b_001010100,
}
a, b, x, y := 0, 0, 0, 0
idx := 0
for i, pair := range moves {
x = pair[0]
y = pair[1]
idx = y*3 + x
if i%2 == 0 {
boardA |= 1 << idx
} else {
boardB |= 1 << idx
}
}
for _, mask := range victories {
a = 0
b = 0
for i := 0; i < 9; i++ {
v := mask & (1 << i)
va := boardA & (1 << i)
vb := boardB & (1 << i)
if v > 0 && va > 0 && v == va {
a++
}
if v > 0 && vb > 0 && v == vb {
b++
}
}
if a == 3 {
return "A"
}
if b == 3 {
return "B"
}
}
if len(moves) < 9 {
return "Pending"
}
return "Draw"
}