Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ solve

grid.counts
grid.index

node_modules
16 changes: 16 additions & 0 deletions BUILD-wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

targets="(all|run_benchmark|run_tests|generate|tdoku|grid_lib)"
if [[ "${1}" =~ ${targets} ]]; then
target=${1}
shift
fi

mkdir -p build
(
cd build
rm -f CMakeCache.txt
rm -rf CMakeFiles
emcmake cmake -DWASM=ON .. $*
emmake make ${target}
)
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ option(FSSS2 "Include fsss2" OFF)
option(JCZSOLVE "Include JCZSolve" OFF)
option(SK_BFORCE2 "Include SK_BFORCE2" OFF)
option(RUST_SUDOKU "Include rust_sudoku" OFF)
option(WASM "Build for WebAssembly" OFF)

if (ALL)
set(MINISAT ON)
Expand Down Expand Up @@ -63,7 +64,11 @@ endif()
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -${OPT} ${ARGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -${OPT} ${ARGS}")

if(AVX512)
if(WASM)
# https://emscripten.org/docs/porting/simd.html
set(ArchFlags "-msimd128 -msse2")
# set(ArchFlags "-mrelaxed-simd")
elseif(AVX512)
set(ArchFlags "-mavx512vl -mavx512bitalg")
elseif(AVX2)
set(ArchFlags "-mavx2")
Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
## Tdoku: A fast Sudoku Solver and Generator

### WebAssembly

#### Prerequisites
1. Setup https://github.com/emscripten-core/emsdk
2. On mac: brew install cmake

#### Running example in browser with WebAssembly
1. npm run build:wasm
2. npm run compile:wasm
3. npm run start
4. Open http://localhost:8080/example/solve.html

#### Running original with C
1. npm run build:c
2. npm run compile:c
3. ./solve


#### Overview
This project contains an optimized Sudoku solver and puzzle generator for conventional 9x9 puzzles (as well as Sukaku
"pencilmark" puzzles with clues given as negative instead of positive literals). It also contains two
Expand Down
1 change: 1 addition & 0 deletions compile-wasm.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
emcc example/solve.c build/libtdoku_static.a -O3 -o solve -lstdc++ -lm -o example/solve.js -sEXPORTED_FUNCTIONS=_solve -sEXPORTED_RUNTIME_METHODS=ccall
1 change: 1 addition & 0 deletions compile.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gcc example/solve.c build/libtdoku_static.a -O3 -o solve -lstdc++ -lm
160 changes: 160 additions & 0 deletions example/generate.html

Large diffs are not rendered by default.

28 changes: 16 additions & 12 deletions example/solve.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, const char **argv) {
size_t limit = argc > 1 ? atoll(argv[1]) : 10000;
#ifdef __cplusplus
extern "C" {
#endif

char *puzzle = NULL;

char* solve(char *puzzle) {
char *result;
char solution[81];
size_t size, guesses;

while (getline(&puzzle, &size, stdin) != -1) {
if (strlen(puzzle) < 81 || puzzle[0] == '#') continue;
solution[0] = '\0';
size_t count = SolveSudoku(puzzle, limit, 0, solution, &guesses);
if (limit > 1 && count == 1) {
SolveSudoku(puzzle, 1, 0, solution, &guesses);
}
printf("%.81s:%ld:%.81s\n", puzzle, count, solution);
size_t count = SolveSudoku(puzzle, 2, 0, solution, &guesses);
if (count == 1) {
SolveSudoku(puzzle, 1, 0, solution, &guesses);
sprintf(result, "%.81s", solution);
}
return result;
}

#ifdef __cplusplus
}
#endif
19 changes: 19 additions & 0 deletions example/solve.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!doctypehtml>
<html lang=en-us>

<head>
<meta charset=utf-8>
<meta content="text/html; charset=utf-8" http-equiv=Content-Type>
<title>Emscripten Solve</title>
</head>

<body>
<script src=solve.js></script>
<script>
Module.onRuntimeInitialized = function () {
console.info(Module.ccall('solve', 'string', ['string'], ['.1...3.942....5...7....82...67......1..4....6.4..81..5....72.....3....8.......1.3']));
}
</script>
</body>

</html>
1 change: 1 addition & 0 deletions example/solve.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added example/solve.wasm
Binary file not shown.
Loading