-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolver.zig
171 lines (149 loc) · 4.06 KB
/
Solver.zig
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////// TODO ////////////////////////////////////
// read template from file
// adapt to hex (16x16)
// create user interface
//////////////////////////////////////////////////////////////////////////////
const std = @import("std");
const print = std.debug.print;
const Allocator = std.mem.Allocator;
const ORDER: u8 = 3;
const SIZE: u8 = ORDER * ORDER;
pub fn main() !void
{
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const gpallocator = gpa.allocator();
defer { _ = gpa.deinit();}
var board: [SIZE][]u8 = try make_board(&gpallocator);
defer {
for (board) |row| {
gpallocator.free(row);
}
}
const template = [_][3]u8{
[3]u8{ 0, 4, 3 },
[3]u8{ 0, 5, 7 },
[3]u8{ 0, 7, 9 },
[3]u8{ 0, 8, 2 },
[3]u8{ 1, 0, 6 },
[3]u8{ 1, 1, 3 },
[3]u8{ 2, 1, 9 },
[3]u8{ 2, 5, 2 },
[3]u8{ 2, 6, 3 },
[3]u8{ 2, 8, 5 },
[3]u8{ 3, 0, 8 },
[3]u8{ 3, 1, 7 },
[3]u8{ 3, 8, 1 },
[3]u8{ 4, 1, 2 },
[3]u8{ 4, 3, 9 },
[3]u8{ 4, 5, 1 },
[3]u8{ 4, 7, 4 },
[3]u8{ 5, 0, 9 },
[3]u8{ 5, 7, 2 },
[3]u8{ 5, 8, 7 },
[3]u8{ 6, 0, 1 },
[3]u8{ 6, 2, 9 },
[3]u8{ 6, 3, 5 },
[3]u8{ 6, 7, 7 },
[3]u8{ 7, 7, 8 },
[3]u8{ 7, 8, 6 },
[3]u8{ 8, 0, 3 },
[3]u8{ 8, 1, 6 },
[3]u8{ 8, 3, 4 },
[3]u8{ 8, 4, 1 },
};
for (template) |assignment| {
assign(&board, &assignment);
}
display_board(&board); // board as template with zeros for unfilled cells
// var res: bool = solver(&board);
_ = solver(&board);
display_board(&board); // solved board
}
fn make_board(allocator: *const Allocator) ![SIZE][]u8
{
var matrix: [SIZE][]u8 = undefined;
for (&matrix) |*row| {
row.* = try allocator.alloc(u8, SIZE);
for (row.*, 0..) |val, i| {
_ = val;
row.*[i] = 0;
}
}
return matrix;
}
fn assign(board: *[SIZE][]u8, val: *const [3]u8) void
{
board[val[0]][val[1]] = val[2];
}
fn display_board(board: *[SIZE][]u8) void
{
for (board) |row| {
for (row) |item| {
print("{} ", .{item});
}
print("\n", .{});
}
print("\n", .{});
}
fn check_candidate(x:u8, y:u8, candidate:u8, board:*[SIZE][]u8) bool
{
// test row
for (0..SIZE-1) |i| {
if (board.*[x][i] == candidate) {
return false;
}
}
// test column
for (0..SIZE-1) |j| {
if (board.*[j][y] == candidate) {
return false;
}
}
// test square
const row_start = x - (x % ORDER);
const col_start = y - (y % ORDER);
for (0..ORDER) |row| {
for (0..ORDER) |col| {
if (candidate == board.*[row + row_start][col + col_start]) {
return false;
}
}
}
return true;
}
fn solver(board:*[SIZE][]u8) bool
{
// check for empty cells; if there are none, return true
var x: u8 = undefined;
var y: u8 = undefined;
var z: bool = true;
outer: for (0..SIZE) |i| {
for (0..SIZE) |j| {
if (board.*[i][j] == 0) {
x = @intCast(i);
y = @intCast(j);
z = false;
break :outer;
}
}
}
if (z == true) {
return true;
}
// recursive solve: iterate through integers, validate the candidate, update the board,
// and recurse; ensure cell value is zero if candidate is not available.
const candidates: [9]u8 = [9]u8{1,2,3,4,5,6,7,8,9};
for (candidates) |candidate| {
var is_valid: bool = check_candidate(x, y, candidate, board);
if (is_valid == true) {
board.*[x][y] = candidate;
var complete: bool = solver(board);
if (complete == true) {
return true;
}
board.*[x][y] = 0;
}
}
return false;
}