-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructs.jl
More file actions
265 lines (227 loc) · 6.91 KB
/
structs.jl
File metadata and controls
265 lines (227 loc) · 6.91 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
const Compartment = Vector{Tuple{Int, Int}}
"""
SimpleStr8ts
Lightweight datastructure used by backtracking and SAT solving.
# Fields
- `numbers`: numbers[r, c] is the number at (r, c), or 0 if empty
- `isBlack`: isBlack[r, c] is true iff the tile at (r, c) is a black cell
"""
mutable struct SimpleStr8ts
numbers::Array{Int, 2}
isBlack::Array{Bool, 2}
end
"""
Str8ts
Richer datastructure used by the human-like solver.
"""
mutable struct Str8ts
solved::Array{Bool, 2}
numbers::Array{Int, 2}
isBlack::Array{Bool, 2}
candidates::Array{BitSet, 2}
rowCompartments::Vector{Compartment}
colCompartments::Vector{Compartment}
cellToRowCompartment::Array{Int, 2}
cellToColCompartment::Array{Int, 2}
occRow::Array{Set{Tuple{Int,Int}}, 2}
occCol::Array{Set{Tuple{Int,Int}}, 2}
end
"""
Construct a SimpleStr8ts from the string representation.
"""
function SimpleStr8ts(board::AbstractString)
board = replace(board, r"\s+" => "")
if length(board) != 81
throw(ArgumentError("Board string must be exactly 81 characters long"))
end
numbers = zeros(Int, 9, 9)
isBlack = falses(9, 9)
for (idx, char) in enumerate(board)
# convert 1D index to 2D indices
row = div(idx - 1, 9) + 1
col = mod(idx - 1, 9) + 1
if char == '#'
isBlack[row, col] = true
elseif char == '.'
continue
elseif islowercase(char)
isBlack[row, col] = true
numbers[row, col] = Int(char - 'a' + 1)
elseif isdigit(char)
numbers[row, col] = parse(Int, char)
else
throw(ArgumentError("Invalid character: $char"))
end
end
return SimpleStr8ts(numbers, isBlack)
end
"""
Construct a Str8ts from the string representation.
"""
function Str8ts(board::AbstractString)
board = replace(board, r"\s+" => "")
if length(board) != 81
throw(ArgumentError("Board string must be exactly 81 characters long"))
end
numbers = zeros(Int, 9, 9)
isBlack = falses(9, 9)
solved = falses(9, 9)
candidates = [BitSet() for _ in 1:9, _ in 1:9]
for (idx, char) in enumerate(board)
row = div(idx - 1, 9) + 1
col = mod(idx - 1, 9) + 1
if char == '#'
isBlack[row, col] = true
elseif char == '.'
continue
elseif islowercase(char)
isBlack[row, col] = true
numbers[row, col] = Int(char - 'a' + 1)
elseif isdigit(char)
numbers[row, col] = parse(Int, char)
solved[row, col] = true
else
throw(ArgumentError("Invalid character: $char"))
end
end
rowCompartments = Compartment[]
colCompartments = Compartment[]
cellToRowCompartment = zeros(Int, 9, 9)
cellToColCompartment = zeros(Int, 9, 9)
for r in 1:9
c = 1
while c <= 9
if !isBlack[r, c]
comp = Compartment()
while c <= 9 && !isBlack[r, c]
push!(comp, (r, c))
c += 1
end
if length(comp) > 0
push!(rowCompartments, comp)
idx = length(rowCompartments)
for (rr, cc) in comp
cellToRowCompartment[rr, cc] = idx
end
end
else
c += 1
end
end
end
for c in 1:9
r = 1
while r <= 9
if !isBlack[r, c]
comp = Compartment()
while r <= 9 && !isBlack[r, c]
push!(comp, (r, c))
r += 1
end
if length(comp) > 0
push!(colCompartments, comp)
idx = length(colCompartments)
for (rr, cc) in comp
cellToColCompartment[rr, cc] = idx
end
end
else
r += 1
end
end
end
occRow = [Set{Tuple{Int,Int}}() for _ in 1:9, _ in 1:9]
occCol = [Set{Tuple{Int,Int}}() for _ in 1:9, _ in 1:9]
s = Str8ts(solved, numbers, isBlack, candidates, rowCompartments, colCompartments,
cellToRowCompartment, cellToColCompartment, occRow, occCol)
for r in 1:9
for c in 1:9
if !isBlack[r, c]
if solved[r, c]
s.candidates[r, c] = BitSet(numbers[r, c])
else
s.candidates[r, c] = BitSet(1:9)
end
end
end
end
for r in 1:9
for c in 1:9
if isBlack[r, c] && numbers[r, c] != 0
num = numbers[r, c]
for cc in 1:9
if !isBlack[r, cc]
delete!(s.candidates[r, cc], num)
end
end
for rr in 1:9
if !isBlack[rr, c]
delete!(s.candidates[rr, c], num)
end
end
end
end
end
for r in 1:9
for c in 1:9
if solved[r, c]
num = numbers[r, c]
for cc in 1:9
if cc != c && !isBlack[r, cc]
delete!(s.candidates[r, cc], num)
end
end
for rr in 1:9
if rr != r && !isBlack[rr, c]
delete!(s.candidates[rr, c], num)
end
end
end
end
end
for r in 1:9
for c in 1:9
if !isBlack[r, c]
for n in s.candidates[r, c]
push!(s.occRow[r, n], (r, c))
push!(s.occCol[c, n], (r, c))
end
end
end
end
return s
end
function showBoard(io::IO, numbers::Array{Int,2}, isBlack::Array{Bool,2})
BLACK_BG = "\e[40m" # Black background
WHITE_BG = "\e[47m" # White background
RESET = "\e[0m" # Reset formatting
WHITE_FG = "\e[37m" # White foreground for black tiles
BLACK_FG = "\e[30m" # Black foreground for white tiles
for row in 1:9
for col in 1:9
bg = isBlack[row, col] ? BLACK_BG : WHITE_BG
num = numbers[row, col]
if num == 0
print(io, bg * " " * RESET) # Empty square
else
# White text on black tiles, black text on white tiles
fg = isBlack[row, col] ? WHITE_FG : BLACK_FG
print(io, bg * fg * " " * string(num) * " " * RESET)
end
end
println(io)
end
end
"""
Pretty print SimpleStr8ts
"""
function Base.show(io::IO, s::SimpleStr8ts)
showBoard(io, s.numbers, s.isBlack)
end
"""
Pretty print Str8ts
"""
function Base.show(io::IO, s::Str8ts)
showBoard(io, s.numbers, s.isBlack)
end
# (c) Mia Muessig