├── LICENSE ├── flake.lock ├── flake.nix ├── puzzle1.txt ├── puzzle2.txt ├── readme.md └── sudoku.nix /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright © 2022 Siraphob (Ben) Phipathananunth 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /flake.lock: -------------------------------------------------------------------------------- 1 | { 2 | "nodes": { 3 | "root": { 4 | "inputs": { 5 | "stdlib": "stdlib" 6 | } 7 | }, 8 | "stdlib": { 9 | "locked": { 10 | "lastModified": 1635176326, 11 | "narHash": "sha256-EI9lCID4NyItaPWC140G4va9+UFfJ56LvawIPsHgf58=", 12 | "owner": "manveru", 13 | "repo": "nix-lib", 14 | "rev": "d81d9411ade8f5cc6cfc08977a1f1ff6f64e49b1", 15 | "type": "github" 16 | }, 17 | "original": { 18 | "owner": "manveru", 19 | "repo": "nix-lib", 20 | "type": "github" 21 | } 22 | } 23 | }, 24 | "root": "root", 25 | "version": 7 26 | } 27 | -------------------------------------------------------------------------------- /flake.nix: -------------------------------------------------------------------------------- 1 | { 2 | description = "Sudoku solver in Nix"; 3 | inputs.stdlib.url = "github:manveru/nix-lib"; 4 | 5 | outputs = { self, stdlib }: 6 | let f = (import ./sudoku.nix { lib = stdlib.lib; }); in 7 | { lib = { inherit (f) solve readPuzzle solveFile demo; }; }; 8 | } 9 | -------------------------------------------------------------------------------- /puzzle1.txt: -------------------------------------------------------------------------------- 1 | 0 4 3 0 8 0 2 5 0 2 | 6 0 0 0 0 0 0 0 0 3 | 0 0 0 0 0 1 0 9 4 4 | 9 0 0 0 0 4 0 7 0 5 | 0 0 0 6 0 8 0 0 0 6 | 0 1 0 2 0 0 0 0 3 7 | 8 2 0 5 0 0 0 0 0 8 | 0 0 0 0 0 0 0 0 5 9 | 0 3 4 0 9 0 7 1 0 10 | -------------------------------------------------------------------------------- /puzzle2.txt: -------------------------------------------------------------------------------- 1 | 7 3 9 0 0 1 5 0 0 2 | 0 8 0 0 0 0 0 2 0 3 | 0 0 0 0 0 0 0 0 0 4 | 5 0 0 6 1 9 0 7 0 5 | 3 0 1 5 2 8 4 0 0 6 | 0 0 0 4 3 7 0 0 0 7 | 0 6 0 2 7 0 0 0 0 8 | 4 0 0 0 0 0 1 0 3 9 | 0 0 0 0 8 0 0 0 0 10 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Sudoku solver in Nix 2 | 3 | Sudoku solver in Nix using a simple backtracking algorithm. 4 | 5 | Run the following command, replacing `./puzzle1.txt` with the path of a puzzle of your choice (with zeroes representing blank squares). No need to check out the repo. 6 | 7 | ```ShellSession 8 | $ nix eval --impure --raw github:siraben/nix-sudoku#lib.demo --apply 'f: f ./puzzle1.txt' 9 | ``` 10 | 11 | Otherwise, `cd` into a checkout of repo and run the following command 12 | ```ShellSession 13 | $ nix-instantiate --eval -E '(import ./sudoku.nix {}).demo ./puzzle1.txt' | jq -r 14 | # or using newer Nix command 15 | $ nix eval --raw --impure --expr '(import ./sudoku.nix {}).demo ./puzzle1.txt' 16 | 1 4 3 9 8 6 2 5 7 17 | 6 7 9 4 2 5 3 8 1 18 | 2 8 5 7 3 1 6 9 4 19 | 9 6 2 3 5 4 1 7 8 20 | 3 5 7 6 1 8 9 4 2 21 | 4 1 8 2 7 9 5 6 3 22 | 8 2 1 5 6 7 4 3 9 23 | 7 9 6 1 4 3 8 2 5 24 | 5 3 4 8 9 2 7 1 6 25 | $ nix eval --raw --impure --expr '(import ./sudoku.nix {}).demo ./puzzle2.txt' 26 | No solution! 27 | ``` 28 | 29 | ## How fast is it? 30 | Beats solving by hand. If you know how to make it faster, please submit PRs! 31 | 32 | ``` 33 | $ nix run nixpkgs#hyperfine -- "nix-instantiate --eval -E '(import ./sudoku.nix {}).demo ./puzzle1.txt'" 34 | Benchmark 1: nix-instantiate --eval -E '(import ./sudoku.nix {}).demo ./puzzle1.txt' 35 | Time (mean ± σ): 809.3 ms ± 19.5 ms [User: 690.2 ms, System: 100.8 ms] 36 | Range (min … max): 782.9 ms … 846.5 ms 10 runs 37 | ``` 38 | 39 | ## Why did you write this? 40 | Because I could. 41 | -------------------------------------------------------------------------------- /sudoku.nix: -------------------------------------------------------------------------------- 1 | { lib ? (import {}).lib }: 2 | with lib; 3 | 4 | let 5 | BOARD-SIZE = 9; ROWS = 9; COLS = 9; GRID-SIZE = 3; 6 | get-row = r: board: elemAt board r; 7 | get-column = c: board: map (row: elemAt row c) board; 8 | get-block = br: bc: puzzle: 9 | concatMap (r: (take GRID-SIZE (drop (GRID-SIZE * bc) r))) 10 | (take GRID-SIZE (drop (GRID-SIZE * br) puzzle)); 11 | readPuzzle = f: map (x: map toInt (splitString " " x)) 12 | (init (splitString "\n" (readFile f))); 13 | get-value = board: row: col: elemAt (elemAt board row) col; 14 | safe-to-put = r: c: n: board: 15 | let v = get-value board r c; in 16 | if n == v then true else 17 | if v == 0 then !(elem n (get-row r board) || 18 | elem n (get-column c board) || 19 | elem n (get-block (r / GRID-SIZE) 20 | (c / GRID-SIZE) 21 | board)) 22 | else false; 23 | print-row = r: concatStringsSep " " (map toString r) + "\n"; 24 | print-board = b: concatStrings (map print-row b); 25 | list-set = l: n: x: take n l ++ [x] ++ tail (drop n l); 26 | set-value = board: row: col: num: 27 | list-set board row (list-set (elemAt board row) col num); 28 | and' = x: y: if x == false then false else y; 29 | or' = x: y: if x == false then y else x; 30 | try-placing = r: c: n: board: 31 | and' (safe-to-put r c n board) (set-value board r c n); 32 | go = n: r: c: board: 33 | if r == BOARD-SIZE then board else 34 | if n == 10 then false else 35 | let nb = try-placing r c n board; 36 | nr = r + (if c == COLS - 1 then 1 else 0); 37 | nc = mod (c + 1) COLS; 38 | in or' (and' nb (go 1 nr nc nb)) 39 | (go (n + 1) r c board); 40 | solve = go 1 0 0; 41 | in 42 | { 43 | inherit solve readPuzzle; 44 | solveFile = f: solve (readPuzzle f); 45 | demo = f: let res = solve (readPuzzle f); in 46 | if res == false then "No solution!" else print-board res; 47 | } 48 | --------------------------------------------------------------------------------