###################################################################### ## Kubok.txt Save this file as Kubok.txt to use it, # # stay in the # ## same directory, get into Maple (by typing: maple ) # ## and then type: read `Kubok.txt` # ## Then follow the instructions given there # ## # ## Written by Doron Zeilberger, Rutgers University , # ## DoronZeil at gmail dot com # ###################################################################### print(`Written: July 24: tested for Maple 2020 `): with(combinat): with(combinat): print(): print(`This is Kubok.txt, A Maple package`): print(`to create and solve Kubok puzzles of the style designed by Davide Coppo `): print(` that appear in the newspaper USA Today (summer 2024 and possibly before and after).`): print(): print(`The most current version is available from:`): print(` http://sites.math.rutgers.edu/~zeilberg/tokhniot/Kubok.txt .`): print(`Please report all bugs to: DoronZeil at gmail dot com .`): print(): print(`-----------------------------`): print(`For general help, and a list of the MAIN functions,`): print(` type "ezra();". For specific help type "ezra(procedure_name);" `): print(): print(`-----------------------------`): print(`For a list of the supporting procedures type ezra1()`): print(`For specific help type "ezra(procedure_name);" `): print(`For a list of the supporting functions type: ezra1();`): print(): print(`-----------------------------`): print(`-----------------------------`): ezra1:=proc() if args=NULL then print(`The SUPPORTING procedures are: ChangeE, DrawPuz, FF, Ini, IsGood, MakeHarder, Yels `): print(` `): else ezra(args): fi: end: ezra:=proc() if args=NULL then print(`The main procedures are`): print(` MakeHardest, MakePuz, MakePuzB, MakePuzHB, PlotPuz, Ptor `): elif nargs=1 and args[1]=ChangeE then print(`ChangeE(M,c,a): Given a matrix and a cell c, replaces the cell c by a. Try:`): print(`ChangeE[[1,3,4],[2,5,2],[1,3,4]],[1,3],0);`): elif nargs=1 and args[1]=DrawPuz then print(`DrawPuz(P): draws the puzzle P, Try:`): print(`DrawPuz([[[7,0,0,0],[0,15,0,8],[0,0,2,0],[0,14,0,11]],[35,40,32,29],[37,48,15,36]]);`): elif nargs=1 and args[1]=FF then print(`FF(M): The list of all pure integers in M`): elif nargs=1 and args[1]=Ini then print(`Ini(P,x): Inputs a Kubok puzzle P=[M,R,C], outputs initial triple, Inputs [SymbolicMatrix,var,AV]. Try`): print(`Ini([[[2,6,0,0],[11,0,0,0],[0,0,0,12],[0,0,7,16]],[21,29,35,51],[42,25,23,46]],x);`): elif nargs=1 and args[1]=IsGood then print(`IsGood(M): Is the partial solution M good?`): elif nargs=1 and args[1]=MakeHarder then print(`MakeHarder(P): inputs a Kubok puzzle, outputs the (possibly empty) set of puzzles, still with a unique solution obtained by removing one clue: Try`): print(`MakeHarder([[[2,6,0,0],[11,0,0,0],[0,0,0,12],[0,0,7,16]],[21,29,35,51],[42,25,23,46]]);`): elif nargs=1 and args[1]=MakeHardest then print(`MakeHardest(P): inputs a Kubok puzzle, outputs the set of hardest puzzles, still with a unique solution with removing as many as possible clues. Try:`): print(`MakeHardest([[[2,6,0,0],[11,0,0,0],[0,0,0,12],[0,0,7,16]],[21,29,35,51],[42,25,23,46]]);`): elif nargs=1 and args[1]=MakePuz then print(`MakePuz(m,n,k): makes a random m by n puzzle with k clues with a unique solution. or returns FAIL. Try:`): print(`MakePuz(4,4,6);`): elif nargs=1 and args[1]=PlotPuz then print(`PlotPuz(P): plotsthe puzzle P, Try:`): print(`PlotPuz([[[7,0,0,0],[0,15,0,8],[0,0,2,0],[0,14,0,11]],[35,40,32,29],[37,48,15,36]]);`): elif nargs=1 and args[1]=MakePuzB then print(`MakePuzB(m,n,k,K): inputs integers m, n, and k, and K and outputs K random m by n Kubok puzzles with k clues`): print(`w/o trying to make them harder, i.e. some of the clues are redundant. It outputs a list of K items of the form [puzzle, solution]. Try:`): print(`MakePuzB(3,3,2,10);`): elif nargs=1 and args[1]=MakePuzHB then print(`MakePuzHB(m,n,k,K): inputs integers m, n, and k, and K and outputs K random m by n Kubok puzzles with k clues`): print(`starting with such a puzzle it makes it as hard as possible, by removing as many as possible clues and still having a unique solution.`): print(`Using MakeHardes(P). It outputs a list of K items of the form [puzzle, solution]. Try:`): print(`MakePuzHB(3,3,2,10);`): elif nargs=1 and args[1]=Ptor then print(`Ptor(P): the set of all solutions to a Kubok problem. Try:`): print(`Ptor([[[2,6,0,0],[11,0,0,0],[0,0,0,12],[0,0,7,16]],[21,29,35,51],[42,25,23,46]]);`): elif nargs=1 and args[1]=Yels then print(`Yels(T): all the good children of the triple T.`): else print(`There is no such thing as`, args): fi: end: with(combinat): #Ini(P): Inputs a Kubok puzzle P=[M,R,C], outputs The initial triple, [SymbolicMatrix,var,AV]. Try #Ini([[[2,6,0,0],[11,0,0,0],[0,0,0,12],[0,0,7,16]],[21,29,35,51],[42.25.23.46]]); Ini:=proc(P,x) local M,R,C,m,n,var,i,j,eq,AV,var1,LE,var11,M1: M:=P[1]: R:=P[2]: C:=P[3]: m:=nops(M): n:=nops(M[1]): if not (nops(R)=m and nops(C)=n and convert(R,`+`)=(m*n)*(m*n+1)/2 and convert(C,`+`)=(m*n)*(m*n+1)/2) then RETURN(FAIL): fi: M1:=[seq([seq(x[i,j],j=1..n)],i=1..m)]: var:={seq(seq(x[i,j],j=1..n),i=1..m)}: eq:={seq(add(x[i,j],j=1..n)-R[i],i=1..nops(R)),seq(add(x[i,j],i=1..m)-C[j],j=1..nops(C))}: AV:={seq(i,i=1..m*n)}: for i from 1 to m do for j from 1 to n do if M[i][j]<>0 then eq:=eq union {x[i,j]=M[i,j]}: AV:=AV minus {M[i][j]}: fi: od: od: var1:=solve(eq,var): LE:=[]: for var11 in var1 do if op(1,var11)=op(2,var11) then var1:=var1 minus {var11}: LE:=[op(LE),op(1,var11)]: fi: od: [subs(var1,M1),LE,AV]: end: #IsGood(M): Is the partial solution M good? IsGood:=proc(M) local m,n,L,i,j: m:=nops(M): n:=nops(M[1]): L:=[]: for i from 1 to m do for j from 1 to n do if type(M[i][j],integer) then L:=[op(L),M[i][j]]: fi: od: od: if nops(L)<>nops(convert(L,set)) then RETURN(false): fi: if convert(L,set) minus {seq(i,i=1..m*n)}<>{} then RETURN(false): fi: true: end: #FF(M): The list of all pure integers in M FF:=proc(M) local m,n,L,i,j: m:=nops(M): n:=nops(M[1]): L:=[]: for i from 1 to m do for j from 1 to n do if type(M[i][j],integer) then L:=[op(L),M[i][j]]: fi: od: od: L: end: #Yels(T): all the good children of the triple T. Try: Yels:=proc(T) local M,var,AV,gu,var1,a,M1,AV1,v: M:=T[1]: var:=T[2]: AV:=T[3]: if var=[] then if IsGood(M) then RETURN(T): else RETURN(FAIL): fi: fi: v:=var[1]: var1:=[op(2..nops(var),var)]: gu:={}: for a in AV do M1:=subs(v=a,M): if IsGood(M1) then AV1:=AV minus convert(FF(M1),set): gu:=gu union {[M1,var1,AV1]}: fi: od: gu: end: #Ptor(P): the set of all solutions to a Kubok problem. Try: #Ptor([[[2,6,0,0],[11,0,0,0],[0,0,0,12],[0,0,7,16]],[21,29,35,51],[42,25,23,46]]); Ptor:=proc(P) local x,gu,gu1: if nops(P)<>3 then RETURN(FAIL): fi: gu:=Ini(P,x): if gu=FAIL then RETURN(FAIL): fi: gu:={gu}: while gu<>{} and nops(gu[1][2])>0 do gu:={seq(op(Yels(gu1)),gu1 in gu)}: od: {seq(gu1[1],gu1 in gu)}: end: #ChangeE(M,c,a): Given a matrix and a cell c, replaces the cell c by a. Try: #ChangeE[[1,3,4],[2,5,2],[1,3,4]],[1,3],0); ChangeE:=proc(M,c,a) local m,n,i,j: m:=nops(M): n:=nops(M[1]): i:=c[1]: j:=c[2]: if not (1<=i and i<=m) then RETURN(FAIL): fi: if not (1<=j and j<=n) then RETURN(FAIL): fi: [op(1..i-1,M),[op(1..j-1,M[i]),a,op(j+1..n,M[i])],op(i+1..m,M)]: end: #MakePuz(m,n,k): makes a random m by n puzzle with k clues with a unique solution. or returns FAIL. Try: #MakePuz(4,4,k); MakePuz:=proc(m,n,k) local pi,S,R,C,i,j,S1,gu,c,i1,P: pi:=randperm(m*n): S:=[seq([op(i*n+1..i*n+n,pi)],i=0..m-1)]: R:=[seq(add(S[i][j],j=1..n),i=1..m)]: C:=[seq(add(S[i][j],i=1..m),j=1..n)]: gu:={seq(seq([i,j],j=1..n),i=1..m)}: S1:=[[0$n]$m]: for i1 from 1 to k do c:=gu[rand(1..nops(gu))()]: S1:=ChangeE(S1,c,S[c[1]][c[2]]): gu:=gu minus {c}: od: P:= [S1,R,C]: if nops(Ptor(P))>1 then RETURN(FAIL): fi: P: end: #MakeHarder(P): Given a puzzle P, outputs all those that are obtained by deleting one clue and still having q unique solution MakeHarder:=proc(P) local gu,i,j,M,R,C,M1,P1: if nops(Ptor(P))<>1 then RETURN(FAIL): fi: M:=P[1]: R:=P[2]: C:=P[3]: gu:={}: for i from 1 to nops(M) do for j from 1 to nops(M[i]) do if M[i][j]<>0 then M1:=ChangeE(M,[i,j],0): P1:=[M1,R,C]: if nops(Ptor(P1))=1 then gu:=gu union {P1}: fi: fi: od: od: gu: end: #MakeHardest(P): Given a puzzle P, outputs all those that are obtained by deleting as many as possible clues and still having q unique solution MakeHardest:=proc(P) local gu,gu1,mu: gu:={P}: while gu<>{} do mu:={seq(op(MakeHarder(gu1)),gu1 in gu)}: if mu={} then RETURN(gu): fi: gu:=mu: od: FAIL: end: #DrawPuz(P): draws the puzzle P, Try: #DrawPuz([[[7,0,0,0],[0,15,0,8],[0,0,2,0],[0,14,0,11]],[35,40,32,29],[37,48,15,36]]);`): DrawPuz:=proc(P) local M,R,C,m,n,i,P1: M:=P[1]: R:=P[2]: C:=P[3]: m:=nops(M): n:=nops(M[1]): P1:= [ [`*`,`*`,op(C)], [`*`$(n+2)], seq([R[i],`*`,op(M[i])],i=1..m) ]: P1:=subs(0=` `,P1): matrix(P1): end: with(plots): #PlotPuz(P): plots the puzzle PlotPuz:=proc(P) local M,R,C,a,b,i,j,p: M:=P[1]: R:=P[2]: C:=P[3]: b:=nops(M): a:=nops(M[1]): p:=plot([[0,0],[a,0],[a,b],[0,b],[0,0]],thickness=3,axes=none,scaling=constrained): for i from 1 to b-1 do p:=p,plot([[0,i],[a,i]]): od: for j from 1 to a-1 do p:=p,plot([[j,0],[j,b]]): od: for i from 1 to b do for j from 1 to a do if M[i][j]<>0 then p:=p,textplot([j-1/2, b-i+1/2,M[i][j]]): fi: od: od: for i from 1 to b do p:=p,textplot([-1/2,b-i+1/2,R[i]]): p:=p,implicitplot((x+1/2)^2+(y-b+i-1/2)^2=(2/5)^2, x=-1/2-2/5..-1/2+2/5, y=b-i+1/2-2/5..b-i+1/2+2/5): od: for j from 1 to a do p:=p,textplot([j-1/2,b+1/2,C[j]]): p:=p,implicitplot((x-j+1/2)^2+(y-b-1/2)^2=(2/5)^2, x=-j-1/2-2/5..j-1/2+2/5, y=b+1/2-2/5..b+1/2+2/5): od: display(p): end: #PlotSol(P): plots the solution S PlotSol:=proc(S) local M,R,C,a,b,i,j,p: M:=S: b:=nops(M): a:=nops(M[1]): p:=plot([[0,0],[a,0],[a,b],[0,b],[0,0]],thickness=3,axes=none,scaling=constrained): for i from 1 to b-1 do p:=p,plot([[0,i],[a,i]]): od: for j from 1 to a-1 do p:=p,plot([[j,0],[j,b]]): od: for i from 1 to b do for j from 1 to a do if M[i][j]<>0 then p:=p,textplot([j-1/2, b-i+1/2,M[i][j]]): fi: od: od: display(p): end: #MakePuzB(m,n,k,K): inputs integers m, n, and k, and K and outputs K random m by n Kubok puzzles with k clues #w/o trying to make them harder, i.e. some of the clues are redundant. It outputs a list of K items of the form [puzzle, solution]. Try: #MakePuzB(3,3,2,10); MakePuzB:=proc(m,n,k,K) local co,gu,P,S: co:=0: gu:=[]: while coFAIL then co:=co+1: S:=Ptor(P)[1]: gu:=[op(gu),[P,S]]: fi: od: gu: end: #MakePuzHB(m,n,k,K): inputs integers m, n, and k, and K and outputs K random m by n Kubok puzzles starting with k clues #and hten make them harder by removing as many clues as possible. It outputs a list of K items of the form [puzzle, solution]. Try: #MakePuzHB(3,3,2,10); MakePuzHB:=proc(m,n,k,K) local co,gu,P,S: co:=0: gu:=[]: while coFAIL then co:=co+1: P:=MakeHardest(P)[1]: S:=Ptor(P)[1]: gu:=[op(gu),[P,S]]: fi: od: gu: end: