summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobby Zambito <contact@robbyzambito.me>2025-05-16 07:45:24 -0400
committerRobby Zambito <contact@robbyzambito.me>2025-05-19 08:16:10 -0400
commitb61b7b8a122f3efc9a573777300648ab0eb2a50b (patch)
treeacaf8a0e0512053eb0ae89aaa2714927dc4e52a2
parent720465d3bca582ffa8baccfef05e8707cd74f8d6 (diff)
check for equality instead of counting > 0
might be faster on certain cpus
-rw-r--r--src/main.zig9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/main.zig b/src/main.zig
index 0e8da4e..e6975be 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -1,5 +1,4 @@
const SudokuSolver = struct {
- const countEql = std.simd.countElementsWithValue;
const join = std.simd.join;
// Each cell exists in memory three times.
// This makes it easy to compare a guess against all relevant cells simultaneously.
@@ -29,7 +28,9 @@ const SudokuSolver = struct {
const constraints = join(self.rows[row], join(self.cols[col], self.boxes[boxOf(row, col)]));
inline for (1..10) |n| {
// A number is a possible move iff it does not already exist in the current row, col, or box.
- if (countEql(constraints, @intCast(n)) == 0) {
+ const n_vec: @Vector(@typeInfo(@TypeOf(constraints)).vector.len, u8) = @splat(n);
+ const equals = constraints == n_vec;
+ if (!@reduce(.Or, equals)) {
cell_guesses.set(n - 1);
}
}
@@ -95,7 +96,9 @@ const SudokuSolver = struct {
fn isSolved(self: SudokuSolver) bool {
for (self.rows) |row| {
- if (countEql(row, 0) > 0) return false;
+ const z_vec: @Vector(@typeInfo(@TypeOf(row)).vector.len, u8) = @splat(0);
+ const equals = row == z_vec;
+ if (@reduce(.Or, equals)) return false;
}
return true;
}