Skip to content

Commit 2a297cb

Browse files
committed
1275_Find_Winner_on_a_Tic_Tac_Toe_Game
1 parent b314a08 commit 2a297cb

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ I have solved quite a number of problems from several topics. See the below tabl
196196
|84| **[1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit](https://tinyurl.com/yysmvobr)** | [Python](https://tinyurl.com/wu6rdaw/1438_Longest_Continuous_Subarray_With_Absolute_Diff_Less_Than_or_Equal_to_Limit.py), [Swift](https://tinyurl.com/wuja3c4/1438_Longest_Continuous_Subarray_With_Absolute_Diff_Less_Than_or_Equal_to_Limit.swift) | (Art 1)[https://tinyurl.com/y69hjhjp], (Art 2)[https://tinyurl.com/y3h2fj2m] | Medium | Sliding window |
197197
|85| **[1428. Leftmost Column with at Least a One](https://tinyurl.com/y3reu3y2)** | [Python](https://tinyurl.com/wu6rdaw/1428_Leftmost_Column_with_at_Least_a_One.py), [Swift](https://tinyurl.com/wuja3c4/1428_Leftmost_Column_with_at_Least_a_One.swift) | --- | Medium | Binary Search |
198198
|86| **[914. X of a Kind in a Deck of Cards](https://tinyurl.com/y39y2k32)** | [Python](https://tinyurl.com/wu6rdaw/914_X_of_a_Kind_in_a_Deck_of_Cards.py), [Swift](https://tinyurl.com/wuja3c4/914_X_of_a_Kind_in_a_Deck_of_Cards.swift) | --- | Easy (Not So) | |
199+
|87| **[1275. Find Winner on a Tic Tac Toe Game](https://tinyurl.com/y323arde)** | [Python](https://tinyurl.com/wu6rdaw/1275_Find_Winner_on_a_Tic_Tac_Toe_Game.py), [Swift](https://tinyurl.com/wuja3c4/1275_Find_Winner_on_a_Tic_Tac_Toe_Game.swift) | Art 1](https://tinyurl.com/yyc54qj5) | Easy (Not So) | |
199200

200201

201202
</p>
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
// Optimized solution
2+
import Foundation
3+
class Solution {
4+
func tictactoe(_ moves: [[Int]]) -> String {
5+
let size = 3
6+
var (rows, cols) = (Array(repeating: 0, count: size), Array(repeating: 0, count: size))
7+
var (diagonal, skew_diagonal) = (0, 0)
8+
for (index, move) in moves.enumerated() {
9+
let (i, j) = (move[0], move[1])
10+
let sign = index % 2 == 0 ? 1 : -1
11+
rows[i] += sign
12+
cols[j] += sign
13+
if i == j {
14+
diagonal += sign
15+
}
16+
if i + j == size - 1 {
17+
skew_diagonal += sign
18+
}
19+
if abs(rows[i]) == size || abs(cols[j]) == size || abs(diagonal) == size || abs(skew_diagonal) == size {
20+
return sign == 1 ? "A" : "B"
21+
}
22+
}
23+
return moves.count == (size*size) ? "Draw" : "Pending"
24+
}
25+
}
26+
27+
28+
29+
// Naive stupid solution
30+
import Foundation
31+
class Solution {
32+
enum Result: String, RawRepresentable {
33+
case Pending
34+
case Draw
35+
case A
36+
case B
37+
}
38+
39+
func tictactoe(_ moves: [[Int]]) -> String {
40+
guard moves.count > 0, moves.count <= 9 else {
41+
return Result.Pending.rawValue
42+
}
43+
44+
var grid = Array(repeating: Array(repeating: "#", count: 3), count: 3)
45+
var aMove = true
46+
for move in moves {
47+
let (i, j) = (move[0], move[1])
48+
if aMove {
49+
grid[i][j] = "X"
50+
} else {
51+
grid[i][j] = "O"
52+
}
53+
aMove.toggle()
54+
}
55+
var result: Result = Result.Pending
56+
57+
// Checks first row
58+
if grid[0][0] == grid[0][1] && grid[0][1] == grid[0][2] {
59+
if grid[0][0] == "X" {
60+
if result == .Pending || result == .A {
61+
result = .A
62+
} else if result == .B || result == .Draw {
63+
return Result.Draw.rawValue
64+
}
65+
} else if grid[0][0] == "O" {
66+
if result == .Pending || result == .B {
67+
result = .B
68+
} else if result == .A || result == .Draw {
69+
return Result.Draw.rawValue
70+
}
71+
}
72+
}
73+
74+
// Checks first col
75+
if grid[0][0] == grid[1][0] && grid[1][0] == grid[2][0] {
76+
if grid[0][0] == "X" {
77+
if result == .Pending || result == .A {
78+
result = .A
79+
} else if result == .B || result == .Draw {
80+
return Result.Draw.rawValue
81+
}
82+
} else if grid[0][0] == "O" {
83+
if result == .Pending || result == .B {
84+
result = .B
85+
} else if result == .A || result == .Draw {
86+
return Result.Draw.rawValue
87+
}
88+
}
89+
}
90+
91+
// Checks diagonal
92+
if grid[0][0] == grid[1][1] && grid[1][1] == grid[2][2] {
93+
if grid[0][0] == "X" {
94+
if result == .Pending || result == .A {
95+
result = .A
96+
} else if result == .B || result == .Draw {
97+
return Result.Draw.rawValue
98+
}
99+
} else if grid[0][0] == "O" {
100+
if result == .Pending || result == .B {
101+
result = .B
102+
} else if result == .A || result == .Draw {
103+
return Result.Draw.rawValue
104+
}
105+
}
106+
}
107+
108+
// Checks second col
109+
if grid[0][1] == grid[1][1] && grid[1][1] == grid[2][1] {
110+
if grid[1][1] == "X" {
111+
if result == .Pending || result == .A {
112+
result = .A
113+
} else if result == .B || result == .Draw {
114+
return Result.Draw.rawValue
115+
}
116+
} else if grid[1][1] == "O" {
117+
if result == .Pending || result == .B {
118+
result = .B
119+
} else if result == .A || result == .Draw {
120+
return Result.Draw.rawValue
121+
}
122+
}
123+
}
124+
125+
// Checks third col
126+
if grid[0][2] == grid[1][2] && grid[1][2] == grid[2][2] {
127+
if grid[0][2] == "X" {
128+
if result == .Pending || result == .A {
129+
result = .A
130+
} else if result == .B || result == .Draw {
131+
return Result.Draw.rawValue
132+
}
133+
} else if grid[0][2] == "O" {
134+
if result == .Pending || result == .B {
135+
result = .B
136+
} else if result == .A || result == .Draw {
137+
return Result.Draw.rawValue
138+
}
139+
}
140+
}
141+
142+
// Checks anti diagonal
143+
if grid[0][2] == grid[1][1] && grid[1][1] == grid[2][0] {
144+
if grid[0][2] == "X" {
145+
if result == .Pending || result == .A {
146+
result = .A
147+
} else if result == .B || result == .Draw {
148+
return Result.Draw.rawValue
149+
}
150+
} else if grid[0][2] == "O" {
151+
if result == .Pending || result == .B {
152+
result = .B
153+
} else if result == .A || result == .Draw {
154+
return Result.Draw.rawValue
155+
}
156+
}
157+
}
158+
159+
// Checks second row
160+
if grid[1][0] == grid[1][1] && grid[1][1] == grid[1][2] {
161+
if grid[1][0] == "X" {
162+
if result == .Pending || result == .A {
163+
result = .A
164+
} else if result == .B || result == .Draw {
165+
return Result.Draw.rawValue
166+
}
167+
} else if grid[1][0] == "O" {
168+
if result == .Pending || result == .B {
169+
result = .B
170+
} else if result == .A || result == .Draw {
171+
return Result.Draw.rawValue
172+
}
173+
}
174+
}
175+
176+
// Checks third row
177+
if grid[2][0] == grid[2][1] && grid[2][1] == grid[2][2] {
178+
if grid[2][0] == "X" {
179+
if result == .Pending || result == .A {
180+
result = .A
181+
} else if result == .B || result == .Draw {
182+
return Result.Draw.rawValue
183+
}
184+
} else if grid[2][0] == "O" {
185+
if result == .Pending || result == .B {
186+
result = .B
187+
} else if result == .A || result == .Draw {
188+
return Result.Draw.rawValue
189+
}
190+
}
191+
}
192+
193+
if result == .A || result == .B {
194+
return result.rawValue
195+
} else {
196+
if moves.count == 9 {
197+
return Result.Draw.rawValue
198+
} else {
199+
return Result.Pending.rawValue
200+
}
201+
}
202+
}
203+
}

0 commit comments

Comments
 (0)