From e2cbc9dd61a4b72e2f110cac5613a4d8efe1802d Mon Sep 17 00:00:00 2001 From: Karl Berry Date: Sun, 4 Jun 2023 19:57:06 +0000 Subject: context-sudoku (4jun23) git-svn-id: svn://tug.org/texlive/trunk@67279 c570f23f-e606-0410-a88d-b1316a301751 --- .../texmf-dist/doc/context/third/sudoku/README.md | 15 +++-- Master/texmf-dist/doc/context/third/sudoku/VERSION | 2 +- Master/texmf-dist/tex/context/third/sudoku/VERSION | 1 + .../tex/context/third/sudoku/t-sudoku.lua | 70 ++++++++++++++++++---- .../tex/context/third/sudoku/t-sudoku.mkvi | 56 ++++++++++++++++- 5 files changed, 123 insertions(+), 21 deletions(-) create mode 100755 Master/texmf-dist/tex/context/third/sudoku/VERSION (limited to 'Master') diff --git a/Master/texmf-dist/doc/context/third/sudoku/README.md b/Master/texmf-dist/doc/context/third/sudoku/README.md index f9ffa7b04e0..fee6d7f25bb 100644 --- a/Master/texmf-dist/doc/context/third/sudoku/README.md +++ b/Master/texmf-dist/doc/context/third/sudoku/README.md @@ -1,13 +1,16 @@ # sudoku This is a ConTeXt port of a famous sudoku solver by Peter Norvig. It -provides four commands, as well as a command handler: +provides five commands, as well as a command handler: 1. `\sudoku` typesets a sudoku if valid. 2. `\sudokufile` typesets a sudoku from a file if valid. 3. `\solvesudoku` solves a sudoku if valid. -3. `\solvesudokufile` solves a sudoku from a file if valid. -5. `\setupsudoku` is the command handler for commands above. +4. `\solvesudokufile` solves a sudoku from a file if valid. +5. `\randomsudoku` creates a random, unsolved sudoku. +6. `\sudokubuffer` typesets a sudoku from a buffer. +7. `\solvesudokubuffer` solves a sudoku from a buffer if valid. +8. `\setupsudoku` is the command handler for commands above. `\setupsudoku` understands the following parameters: @@ -18,7 +21,8 @@ provides four commands, as well as a command handler: evenbackground=color, oddbackground=color, evenbackgroundcolor=darkred, - oddbackgroundcolor=darkblue] + oddbackgroundcolor=darkblue, + n=17] % for random sudokus ``` As you might notice, sudokus are just `TABLE`'s in disguise, but only @@ -34,7 +38,8 @@ something like this: [placeholdercommand=\inframed, placeholderlabela=First error, placeholderlabelb=Second error, - placeholderlabelc=Third error] + placeholderlabelc=Third error, + placeholderlabeld=Number too low] ``` For actual examples, check `t-sudoku.mkvi`. diff --git a/Master/texmf-dist/doc/context/third/sudoku/VERSION b/Master/texmf-dist/doc/context/third/sudoku/VERSION index 01291292263..a92966fd4ce 100644 --- a/Master/texmf-dist/doc/context/third/sudoku/VERSION +++ b/Master/texmf-dist/doc/context/third/sudoku/VERSION @@ -1 +1 @@ -2023-05-15 +2023-06-03 diff --git a/Master/texmf-dist/tex/context/third/sudoku/VERSION b/Master/texmf-dist/tex/context/third/sudoku/VERSION new file mode 100755 index 00000000000..a92966fd4ce --- /dev/null +++ b/Master/texmf-dist/tex/context/third/sudoku/VERSION @@ -0,0 +1 @@ +2023-06-03 diff --git a/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.lua b/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.lua index 4e6e12578b6..15ab9c7e2f7 100644 --- a/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.lua +++ b/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.lua @@ -16,13 +16,13 @@ if not modules then modules = { } end modules ['t-sudoku'] = local table, math, io = table, math, io local ipairs, pairs, tostring = ipairs, pairs, tostring -local floor, ceil = math.floor, math.ceil +local floor, ceil, random = math.floor, math.ceil, math.random local insert, concat, sort = table.insert, table.concat, table.sort -- ConTeXt goodies -local context = context -local interfaces = interfaces +local context, buffers, inferfaces = context, buffers, interfaces +local getcontent = buffers.getcontent local implement = interfaces.implement -- Take a look for definitions @@ -1110,11 +1110,12 @@ random_sudoku = function(N) local N = N or 17 local result = {} local values = {} - for _, v in ipairs(squares) do + for _, s in ipairs(squares) do values[s] = digits end - for _, v in ipairs(shuffle(squares)) do - if not assign(values, s, values[rows[random(9)]..columns[random(9)]]) then + for _, v in ipairs(shuffle(copy(squares))) do + local r = random(#values[v]) + if not assign(values, v, values[v]:sub(r, r)) then break end local ds = {} @@ -1126,7 +1127,7 @@ random_sudoku = function(N) if #ds >= N and #unique(ds) >= 8 then for _, i in ipairs(rows) do for _, j in ipairs(columns) do - insert(result, #values[i..j]>0 and values[i..j] or "0") + insert(result, #values[i..j] == 1 and values[i..j] or "0") end end return concat(result) @@ -1139,7 +1140,8 @@ end local ctx_sudoku, ctx_solvesudoku, ctx_sudokufile, ctx_solvesudokufile, - ctx_randomsudoku, ctx_sudokufunction, + ctx_sudokubuffer, ctx_sudokusolvebuffer, + ctx_randomsudoku, ctx_sudokufunction, ctx_typeset, ctx_error ctx_sudoku = function(grid, data) @@ -1173,6 +1175,32 @@ ctx_solvesudoku = function(grid, data) end end +ctx_sudokubuffer = function(buffer, data) + local ok, result = pcall(grid_values, getcontent(buffer)) + if ok then + if result then + ctx_typeset(result, data) + else + ctx_error("c") + end + else + ctx_error("b") + end +end + +ctx_sudokusolvebuffer = function(buffer, data) + local ok, result = pcall(solve, getcontent(buffer)) + if ok then + if result then + ctx_typeset(result, data) + else + ctx_error("c") + end + else + ctx_error("b") + end +end + ctx_solvesudokufile = function(file, data) local ok, result = pcall(solve, loaddata(file)) if ok then @@ -1197,16 +1225,28 @@ end local ctx_sudokufunctions = { - sudoku = ctx_sudoku, - sudokufile = ctx_sudokufile, - solvesudoku = ctx_solvesudoku, - solvesudokufile = ctx_solvesudokufile + sudoku = ctx_sudoku, + sudokufile = ctx_sudokufile, + sudokubuffer = ctx_sudokubuffer, + sudokusolvebuffer = ctx_sudokusolvebuffer, + solvesudoku = ctx_solvesudoku, + solvesudokufile = ctx_solvesudokufile } ctx_sudokufunction = function(t) ctx_sudokufunctions[t.name](t.content, t) end +ctx_randomsudoku = function(data) + local n = tonumber(data.n) + if n < 17 then + ctx_error("d") + return + end + local result = grid_values(random_sudoku(tonumber(n))) + ctx_typeset(result, data) +end + ctx_typeset = function(grid, data) local alternatives = { @@ -1235,3 +1275,9 @@ implement{ arguments = {"hash"}, actions = ctx_sudokufunction } + +implement{ + name = "randomsudoku", + arguments = {"hash"}, + actions = ctx_randomsudoku +} diff --git a/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.mkvi b/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.mkvi index 45ee942bf6d..03883a1ef4f 100644 --- a/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.mkvi +++ b/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.mkvi @@ -1,6 +1,6 @@ %D \module %D [ file=t-sudoku, -%D version=2023-05-15, +%D version=2023-06-03, %D title=\CONTEXT\ User Module, %D subtitle=Sudokus for ConTeXt, %D author=Jairo A. del Rio, @@ -68,6 +68,7 @@ \v!odd\c!background=\v!color, \v!even\c!backgroundcolor=white, \v!odd\c!backgroundcolor=gray, + \c!n=42, \c!placeholder\c!label a=Invalid sudoku, \c!placeholder\c!label b=Invalid sudoku file, \c!placeholder\c!label c=Impossible to find a solution, @@ -91,7 +92,7 @@ \bTABLE [\c!align=\sudokuparameter\c!align, \c!width=\sudokuparameter\c!size, - \c!height=\sudokuparameter\c!size] + \c!height=\sudokuparameter\c!size]% \clf_sudokufunction [name =#name, content ={#content}, @@ -102,10 +103,35 @@ \eTABLE% \endgroup} +%D Random sudokus + +\unexpanded\def\randomsudoku{\dosingleempty\randomsudoku_direct} + +\def\randomsudoku_direct[#parameters]% + {\begingroup% + \iffirstargument + \setupTABLE[#parameters]% + \setupsudokus[#parameters]% + \fi + \bTABLE + [\c!align=\sudokuparameter\c!align, + \c!width=\sudokuparameter\c!size, + \c!height=\sudokuparameter\c!size]% + \clf_randomsudoku + [n =\sudokuparameter\c!n, + evenbackground =\sudokuparameter{\v!even\c!background}, + oddbackground =\sudokuparameter{\v!odd\c!background}, + evenbackgroundcolor =\sudokuparameter{\v!even\c!backgroundcolor}, + oddbackgroundcolor =\sudokuparameter{\v!odd\c!backgroundcolor}] + \eTABLE% + \endgroup} + %D Sudoku macros \def\sudoku{\sudokufunction_name[sudoku]} \def\sudokufile{\sudokufunction_name[sudokufile]} +\def\sudokubuffer{\sudokufunction_name[sudokubuffer]} +\def\sudokusolvebuffer{\sudokufunction_name[sudokusolvebuffer]} \def\solvesudoku{\sudokufunction_name[solvesudoku]} \def\solvesudokufile{\sudokufunction_name[solvesudokufile]} @@ -185,6 +211,24 @@ \stopsection +\startbuffer[lmao] +.......... .......1 .....2.3. +....1...4. .3.5.... .26....7. +14...8...5 ........ 7....6.2. +\stopbuffer + +\startsection[title={\type{\sudokubuffer}}] + +\sudokubuffer{lmao} + +\stopsection + +\startsection[title={\type{\sudokusolvebuffer}}] + +\sudokusolvebuffer{lmao} + +\stopsection + \startsection[title={\type{\sudokufile}}] \sudokufile[evenbackgroundcolor=cornsilk,oddbackgroundcolor=pink]{t-sudoku-test-01.txt} @@ -214,4 +258,10 @@ \stopsection -\stoptext +\startsection[title={\type{\randomsudoku}}] + +\randomsudoku[n=45] + +\stopsection + +\stoptext \ No newline at end of file -- cgit v1.2.3