From c16a68e796bcc5968c259a2026530c1a94a895a5 Mon Sep 17 00:00:00 2001 From: Matt Low Date: Tue, 23 Feb 2021 16:50:17 -0700 Subject: [PATCH] Use 0 instead of null to represent no value --- src/sudoku/index.ts | 2 +- src/sudoku/math.ts | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/sudoku/index.ts b/src/sudoku/index.ts index 1b225bd..d18b60d 100644 --- a/src/sudoku/index.ts +++ b/src/sudoku/index.ts @@ -3,7 +3,7 @@ import { StaticPool, isTimeoutError } from "node-worker-threads-pool"; import WORKERS from "physical-cpu-count"; const TIMEOUT = 20000; -export type Cell = { value: number | null }; +export type Cell = { value: number }; export type GenerateArguments = { regionWidth: number; diff --git a/src/sudoku/math.ts b/src/sudoku/math.ts index 16e3a36..7e2746c 100644 --- a/src/sudoku/math.ts +++ b/src/sudoku/math.ts @@ -20,14 +20,20 @@ type NodeMeta = { type BoardInfo = [number, number, number, number, number]; export class SudokuMath { + /** The width of each region */ regionWidth: number; + /** The height of each region */ regionHeight: number; + /** The number of unqiue values in this sudoku */ values: number; + /** The squared number of unique values - also the total number of cells */ values2: number; + /** An array of board indexes (a range from 0 to `values2 - 1`) */ indexes: number[]; + /** A cache of the possible cadidate rows of this sudoku. */ candidates: BoardInfo[]; constructor(regionWidth: number, regionHeight: number) { @@ -130,8 +136,7 @@ export class SudokuMath { _baseBoard(): Cell[][] { // return a sudoku board with a random set of values in the first row // used in generateComplete for small speedup - const firstRow = range(1, this.values + 1); - shuffle(firstRow); + const firstRow = shuffle(range(1, this.values + 1)); return [ firstRow.map((value) => ({ value })), ...Array(this.values - 1) @@ -139,7 +144,7 @@ export class SudokuMath { .map(() => Array(this.values) .fill(0) - .map((val) => ({ value: val > 0 ? val : null })) + .map((value) => ({ value })) ), ]; } @@ -179,8 +184,8 @@ export class SudokuMath { candidates[meta.row][meta.col][meta.value - 1] = node; }); - // board positions which have been removed, in the order they've been removed - const removed: Set = new Set(); + // board positions which have been removed + const removed = new Set(); const masked: DNode[] = []; const hasOneSolution = () => { @@ -199,12 +204,8 @@ export class SudokuMath { const col = n % this.values; const existValue = completed[row][col].value; const nodes = candidates[row][col]; - // console.log(row, col); - // console.log(existValue); nodes.forEach((node) => { if (node.meta.value !== existValue) { - // console.log(node.meta); - // console.log("masking node"); masked.push(node); maskRow(node); } @@ -256,9 +257,7 @@ export class SudokuMath { } removed.forEach((index) => { - completed[Math.floor(index / this.values)][ - index % this.values - ].value = null; + completed[Math.floor(index / this.values)][index % this.values].value = 0; }); return completed; }