texlive[67279] Master/texmf-dist: context-sudoku (4jun23)

commits+karl at tug.org commits+karl at tug.org
Sun Jun 4 21:57:06 CEST 2023


Revision: 67279
          http://tug.org/svn/texlive?view=revision&revision=67279
Author:   karl
Date:     2023-06-04 21:57:06 +0200 (Sun, 04 Jun 2023)
Log Message:
-----------
context-sudoku (4jun23)

Modified Paths:
--------------
    trunk/Master/texmf-dist/doc/context/third/sudoku/README.md
    trunk/Master/texmf-dist/doc/context/third/sudoku/VERSION
    trunk/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.lua
    trunk/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.mkvi

Added Paths:
-----------
    trunk/Master/texmf-dist/tex/context/third/sudoku/VERSION

Modified: trunk/Master/texmf-dist/doc/context/third/sudoku/README.md
===================================================================
--- trunk/Master/texmf-dist/doc/context/third/sudoku/README.md	2023-06-04 19:56:52 UTC (rev 67278)
+++ trunk/Master/texmf-dist/doc/context/third/sudoku/README.md	2023-06-04 19:57:06 UTC (rev 67279)
@@ -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 @@
      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 @@
     [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`.

Modified: trunk/Master/texmf-dist/doc/context/third/sudoku/VERSION
===================================================================
--- trunk/Master/texmf-dist/doc/context/third/sudoku/VERSION	2023-06-04 19:56:52 UTC (rev 67278)
+++ trunk/Master/texmf-dist/doc/context/third/sudoku/VERSION	2023-06-04 19:57:06 UTC (rev 67279)
@@ -1 +1 @@
-2023-05-15
+2023-06-03

Added: trunk/Master/texmf-dist/tex/context/third/sudoku/VERSION
===================================================================
--- trunk/Master/texmf-dist/tex/context/third/sudoku/VERSION	                        (rev 0)
+++ trunk/Master/texmf-dist/tex/context/third/sudoku/VERSION	2023-06-04 19:57:06 UTC (rev 67279)
@@ -0,0 +1 @@
+2023-06-03


Property changes on: trunk/Master/texmf-dist/tex/context/third/sudoku/VERSION
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
Modified: trunk/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.lua
===================================================================
--- trunk/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.lua	2023-06-04 19:56:52 UTC (rev 67278)
+++ trunk/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.lua	2023-06-04 19:57:06 UTC (rev 67279)
@@ -16,13 +16,13 @@
 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 @@
   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 @@
     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 @@
 
 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 @@
   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,10 +1225,12 @@
 
 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)
@@ -1207,6 +1237,16 @@
   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 @@
   arguments = {"hash"},
   actions   = ctx_sudokufunction
 }
+
+implement{
+  name      = "randomsudoku",
+  arguments = {"hash"},
+  actions   = ctx_randomsudoku
+}

Modified: trunk/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.mkvi
===================================================================
--- trunk/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.mkvi	2023-06-04 19:56:52 UTC (rev 67278)
+++ trunk/Master/texmf-dist/tex/context/third/sudoku/t-sudoku.mkvi	2023-06-04 19:57:06 UTC (rev 67279)
@@ -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



More information about the tex-live-commits mailing list.