summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2025-05-14 08:17:21 -0400
committerRobby Zambito <contact@robbyzambito.me>2025-05-19 08:16:10 -0400
commit8a1b62e4361e7cc64910fb37cd7cf68720760592 (patch)
tree446d996b8d48fb73ade43469055bf19692aba661
parent521a2f3c707522b575e9fdb497469f7806aefb45 (diff)
Only store the most constrained move
-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;