summaryrefslogtreecommitdiff
path: root/src/main.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.zig')
-rw-r--r--src/main.zig23
1 files changed, 10 insertions, 13 deletions
diff --git a/src/main.zig b/src/main.zig
index 265425a..d5830b7 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -52,11 +52,7 @@ const SudokuSolver = struct {
fn solve(self: *SudokuSolver) bool {
if (self.isSolved()) return true;
- // Variables to track the most constrained cell
- var min_row: usize = 0;
- var min_col: usize = 0;
- var min_moves: ?std.StaticBitSet(9) = null;
- var min_count: usize = 10; // More than maximum possible (9)
+ var most_constrained_moves: ?PossibleMoves = null;
for (self.rows, 0..) |r, row| {
for (@as([9]u8, r), 0..) |cell, col| {
@@ -72,24 +68,25 @@ const SudokuSolver = struct {
return self.solve();
},
0 => return false,
- else => |c| if (c < min_count) {
- min_row = row;
- min_col = col;
- min_moves = current_possible_moves;
- min_count = c;
+ else => |c| if (most_constrained_moves == null or c < most_constrained_moves.?.moves.count()) {
+ most_constrained_moves = .{
+ .row = row,
+ .col = col,
+ .moves = current_possible_moves,
+ };
},
}
}
}
}
- if (min_moves) |pm| {
- var moves_iter = pm.iterator(.{});
+ if (most_constrained_moves) |pm| {
+ var moves_iter = pm.moves.iterator(.{});
while (moves_iter.next()) |current_move| {
const backup = self.*;
- self.setCell(min_row, min_col, @intCast(current_move + 1));
+ self.setCell(pm.row, pm.col, @intCast(current_move + 1));
if (self.solve()) {
return true;