--!name Killer Sudoku
--!icon puzzle
--!category Puzzles
local bk="!$%&()+,-0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnpqrstuvwxyz|~"local function aw(a)return bk:sub(a+1,a+1)end local bq,br,bj,bm,aR,aQ,aV,aU,aT,bp,bn,aX=8,9,12,14,24,33,43,44,45,46,48,49 local bi={"QXdZCr4HJCG9jIrI6i94VIuy14lKSnG1JW0JA6PQCbCCJ7KGLBaHJ4b5COnmHZhAK95dO0IHDZYAcof7uUI2YN4u1Jr5YyDIqGYrh6000MK4DKDeGWYTpGWgoh7uSeDr49Yj9z4iN800LQ5SWq0E0oJBoISbh80AsABP41BphOm9MW0VPl4cWIm2H3IkfEoApH0cnYob12ziKWX3y059dfR8k8XeOXgInr6CMG9p6BWK5ROrGk280BnjACECfQPeqBQf9CemR6Mip4P8L3Pb8nDWCOd969jCG0IurSPZHSTZXJcRWe3LaaIqXS0YKHOP9DfnAiqHHIJ8CNGfq6BY45COmKjeG0XbGIaz1heHA3S3IjROh5Ws3AN6beiMGODG0B24eIsYa1HCnvaEcKoI8EsKP5gw4nL2B+G0Zq83D1j0I88fcKnjhnIXhLJHG5kB8iCGv00R224I5MDb0B3542utaa6EkBCAaIhZbAqGW0ZGGbN68IWPGFJ4WHOM15MiOmh2IqTA0KSW0Iw7SaIR9e2PHTKWaMoA4uLp61CDcV698G-0PqYPKoT2P+ClO2omoaOhH4jj4Bed5SLeM80A6FK289cfO8L2Lgf1p5f1PinPvXp686hu00Di2i98HbqokeHG89aYraLCJTH2SonNE84MW4Eo4ATegK3DkYaAhjG8Oqb8o6nzCerRG5W0HaaL7cIKjF3LQG74fbSG3qWxR4si94RCIW0RgmQLcqc9JIn5051T60Mp6DDmXdrfIAn4G04sdf3bgDWhIBRWLsHOLR8Q4KrGPiaOLfAm0Wgl822MnIFmhQBLsK8ICVuJYj11uiNk8A001LegGujGe2na9ajvcWoRJqXZ51OwlZ546u0DWmJICK1I08eb5nhd428x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medium","Hard"}local aP="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+-"local aS={}for a=1,64 do aS[aP:byte(a)]=a-1 end local function be(f)local aJ,G,v=1,0,0 return function(i)while v<i do G=(G<<6)|(aS[f:byte(aJ)]or 0)aJ,v=aJ+1,v+6 end v=v-i local r=G>>v G=G&((1<<v)-1)return r end end local function aA(a)return(a//27)*3+(a%9)//3 end local function bB(bL)local ab=be(bL)local C,al,ak,an={},{},{},{}for c=0,8 do al[c],ak[c],an[c]=0,0,0 end for a=0,80 do local q,p,d=a//9,a%9,aA(a)local N=~(al[q]|ak[p]|an[d])&511 local X,ay=0,0 for c=0,8 do X=X+(N>>c&1)end while(1<<ay)<X do ay=ay+1 end local aD=ab(ay)for c=0,8 do if N>>c&1==1 then if aD==0 then C[a]=c+1;break end aD=aD-1 end end local Z=1<<(C[a]-1)al[q],ak[p],an[d]=al[q]|Z,ak[p]|Z,an[d]|Z end local ax={}for a=0,80 do ax[a]=a end local function aB(D)while ax[D]~=D do D=ax[D]end return D end local function a7(j,d)j,d=aB(j),aB(d)if j~=d then ax[math.max(j,d)]=math.min(j,d)end end for a=0,80 do if a%3<2 and ab(1)==1 then a7(a,a+1)end if a//9%3<2 and ab(1)==1 then a7(a,a+9)end end local y={}for a=0,80 do y[a]=aB(a)end return C,y end local function bA(e)local g=os.date("*t",e)return os.time({year=g.year,month=g.month,day=g.day,hour=12})//86400 end local function ah()local e=os.time()if not e or e<1700000000 then return nil end return bA(e)end local bs={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"}local bo={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}local bl={3,1,1,2,2,2,3}local function a3(ad)return os.date("*t",ad*86400+43200)end local function a4(ad)local g=a3(ad)return bs[g.wday].." "..bo[g.month].." "..g.day end local function a2(ad)return bl[a3(ad).wday]end local C,m,u,J,E={},{},{},{},{}local H={}local b local P={}local h={}for a=1,13 do h[a]=0 end local af={1,1,1}local Y,ae=false,false local Q={}local k,z,s,a8,S=0,"grid",1,1,false local n,x,a_="menu",1,"menu"local B,ao,as=nil,nil,nil local function V(f)f=math.floor(f or 0)if f>=3600 then return string.format("%d:%02d:%02d",f//3600,f//60%60,f%60)end return string.format("%d:%02d",f//60,f%60)end local function ar(K)if K.kind=="d"then return"Daily, "..a4(K.num)end return W[K.diff].." #"..K.num end local function at(aq,I,T)local a0=bi[I]local X=#a0//am local aa,e if aq=="d"then aa,e=(T*37+11)%X,T%8 else aa=(T-1)%X e=((T-1)//X+aa*5)%8 end local bM,bv=bB(a0:sub(aa*am+1,aa*am+am))local ap={}H={}for q=0,8 do for p=0,8 do local ag,ac=q,p if e&4~=0 then ac=8-ac end if e&2~=0 then ag=8-ag end if e&1~=0 then ag,ac=ac,ag end local bc,a=ag*9+ac,q*9+p C[a]=bM[bc]local c=bv[bc]if not ap[c]then H[#H+1]={cells={},sum=0}ap[c]=#H end local y=H[ap[c]]E[a]=ap[c]y.cells[#y.cells+1]=a y.sum=y.sum+C[a]end end for a=0,80 do J[a]=#H[E[a]].cells==1 m[a],u[a]=J[a]and C[a]or 0,0 end b={kind=aq,diff=I,num=T,el=0,hints=0,done=false}Q,z,S,ae,B={},"grid",false,false,nil k=0 for a=0,80 do if not J[a]then k=a;break end end end local function bI()local e,G,v,aZ={},0,0,false for a=0,81 do local N=u[a]or 0 if N~=0 then aZ=true end G,v=(G<<9)|N,v+9 while v>=6 do v=v-6 local r=G>>v e[#e+1]=aP:sub(r+1,r+1)G=G&((1<<v)-1)end end return aZ and table.concat(e)or""end local function bO()local r={}for a=0,80 do r[a+1]=(J[a]and#H[E[a]].cells>1)and string.char(96+m[a])or tostring(m[a])end return table.concat({b.kind,b.diff,b.num,b.el,b.hints,b.done and 1 or 0,table.concat(r),bI()},",")end local function bf(f)local l={}for D in(f..","):gmatch("([^,]*),")do l[#l+1]=D end local I,T=tonumber(l[2]),tonumber(l[3])if(l[1]~="d"and l[1]~="b")or not I or I<1 or I>3 or not T then return end at(l[1],I,T)b.el,b.hints,b.done=tonumber(l[4])or 0,tonumber(l[5])or 0,l[6]=="1"local r=l[7]or""for a=0,80 do local F=r:byte(a+1)or 48 if not J[a]then if F>=97 and F<=105 then m[a],J[a]=F-96,true elseif F>=49 and F<=57 then m[a]=F-48 end end end if l[8]and l[8]~=""then local ab=be(l[8])for a=0,80 do u[a]=ab(9)end end return true end local function M()if b then P[b.kind]=bO()end snail.save(table.concat({"KS1",table.concat(h,","),table.concat(af,","),Y and 1 or 0,b and b.kind or"",P.d or"",P.b or""},";"))end local function bG()local f=snail.load()if type(f)~="string"or f:sub(1,4)~="KS1;"then return end local l={}for D in(f..";"):gmatch("([^;]*);")do l[#l+1]=D end local c=0 for D in(l[2]or""):gmatch("[^,]+")do c=c+1 if c<=13 then h[c]=tonumber(D)or 0 end end c=0 for D in(l[3]or""):gmatch("[^,]+")do c=c+1 if c<=3 then af[c]=math.max(1,tonumber(D)or 1)end end Y=l[4]=="1"if l[6]and l[6]~=""then P.d=l[6]end if l[7]and l[7]~=""then P.b=l[7]end local az=l[5]if az and P[az]then local aH,aL=pcall(bf,P[az])if not(aH and aL)then b=nil end end end local function bN(a,o)return a//9==o//9 or a%9==o%9 or aA(a)==aA(o)end local function au(t)Q[#Q+1]=t if#Q>60 then table.remove(Q,1)end end local function aM(a,r,t)t=t or{}t[#t+1],t[#t+2],t[#t+3]=a,m[a],u[a]m[a],u[a]=r,0 if r>0 then local Z=1<<(r-1)for o=0,80 do if u[o]&Z~=0 and bN(a,o)then t[#t+1],t[#t+2],t[#t+3]=o,m[o],u[o]u[o]=u[o]&~Z end end end return t end local function aF()local i=0 for a=0,80 do if m[a]>0 and m[a]~=C[a]then i=i+1 end end return i end local function a6()local aC=true for a=0,80 do if m[a]==0 then aC=false end end if aF()>0 or not aC then if aC then B="The grid is full, but not right yet"end return end b.done,z,ae=true,"grid",false local d=(b.diff-1)*3 h[d+1]=h[d+1]+1 if h[d+2]==0 or b.el<h[d+2]then h[d+2]=b.el end h[d+3]=h[d+3]+b.el if b.kind=="d"and h[13]~=b.num then h[10]=h[10]+1 h[11]=h[13]==b.num-1 and h[11]+1 or 1 if h[11]>h[12]then h[12]=h[11]end h[13]=b.num end end local function bR()local K=table.remove(Q)if not K then return false end for c=#K-2,1,-3 do local o=K[c]if not J[o]then m[o],u[o]=K[c+1],K[c+2]end end k,z=K[1],"grid"return true end local function bE()local a=k if J[a]or m[a]==C[a]then a=nil for o=0,80 do if m[o]>0 and m[o]~=C[o]then a=o;break end end if not a then local a1=99 for aY,y in ipairs(H)do local _={}for aY,o in ipairs(y.cells)do if m[o]==0 then _[#_+1]=o end end if#_>0 and#_<a1 then a,a1=_[1],#_ end end end end if not a then return end au(aM(a,C[a]))J[a],k=true,a b.hints=b.hints+1 B="Revealed a "..C[a]a6()end local function bz()local i={}for g=1,9 do i[g]=0 end for a=0,80 do if m[a]>0 then i[m[a]]=i[m[a]]+1 end end return i end local function av()local g=ah()if g and h[13]<g-1 then return 0 end return h[11]end local function O()n,as,x="play",os.time(),1 end local function bh(I)if b then M()end local i=af[I]af[I]=i+1 at("b",I,i)O()M()end local function bP(g)if b then M()end b=nil if P.d then local aH,aL=pcall(bf,P.d)if not(aH and aL)or b.num~=g then b=nil end end if not b then at("d",a2(g),g)end O()M()end local function by()local bb=os.time()if b and n=="play"and as and not b.done then local a5=bb-as if a5>0 then b.el=b.el+math.min(a5,600)end end as=bb end local function a9()local e={}if b and not b.done then e[#e+1]={"Continue: "..ar(b).." ("..V(b.el)..")","cont"}end local g=ah()if g then local f="Daily puzzle: "..a4(g)..", "..W[a2(g)]if h[13]==g then f=f.." (solved)"end e[#e+1]={f,"daily"}else e[#e+1]={"Daily puzzle: needs the clock","noclock"}end for c=1,3 do e[#e+1]={"New "..W[c].." puzzle (#"..af[c]..")",c}end e[#e+1]={"Statistics","stats"}e[#e+1]={"How to play","help"}return e end local function bd()return{{"Back to the puzzle","cont"},{#Q>0 and("Undo ("..#Q..(#Q==1 and" move)"or" moves)"))or"Undo (nothing to undo)","undo"},{"Hint: reveal a cell","hint"},{"Check for mistakes","check"},{"Always show mistakes: "..(Y and"on"or"off"),"wrong"},{"Restart this puzzle","restart"},{"New puzzle","menu"},{"Statistics","stats"},{"How to play","help"},}end local function bx(j)if j=="cont"then O()elseif j=="undo"then if bR()then O()else B="Nothing to undo"end M()elseif j=="hint"then if b.done then O()else O();bE();M()end elseif j=="check"then O()local i=aF()ae=i>0 B=i==0 and"No mistakes so far"or(i==1 and"1 mistake, struck through"or i.." mistakes, struck through")elseif j=="wrong"then Y=not Y;M()elseif j=="restart"then at(b.kind,b.diff,b.num)O()M()elseif j=="menu"then n,x="menu",1 elseif j=="daily"then bP(ah())elseif type(j)=="number"then if b and b.kind=="b"and not b.done and ao~=j then ao,B=j,"Press OK again to leave "..ar(b)else ao=nil bh(j)end else a_,n=n,j end end local function ba(c)local q,p=k//9,k%9 if c=="up"then q=(q+8)%9 elseif c=="down"then q=(q+1)%9 elseif c=="left"then p=(p+8)%9 elseif c=="right"then p=(p+1)%9 end k=q*9+p end local function bJ()if s==10 then S=not S return end if s==11 then if m[k]~=0 or u[k]~=0 then au(aM(k,0))end z="grid"elseif S then au({k,m[k],u[k]})m[k]=0 u[k]=u[k]~(1<<(s-1))else a8=s au(aM(k,m[k]==s and 0 or s))z="grid"a6()end M()end local function bK(c)if b.done then if c=="top"then n,x="opts",1 elseif c=="ok"then if b.kind=="b"then bh(b.diff)else n,x="menu",1 end end return end if z=="pad"then if c=="top"then z="grid"elseif c=="left"then s=(s+9)%11+1 elseif c=="right"then s=s%11+1 elseif c=="ok"then bJ()else z="grid";ba(c)end return end if c=="top"then n,x="opts",1 M()elseif c=="ok"then if J[k]then B=#H[E[k]].cells==1 and"A one-cell cage: its number is given"or"That number was revealed"else z="pad"s=m[k]>0 and m[k]or a8 end else ba(c)end end function key(c)by()B,ae=nil,false if n=="play"then bK(c)return end if n=="menu"or n=="opts"then local R=n=="menu"and a9()or bd()if c~="ok"then ao=nil end if c=="up"then x=(x-2)%#R+1 elseif c=="down"then x=x%#R+1 elseif c=="ok"then bx(R[math.min(x,#R)][2])elseif c=="top"and n=="menu"and b and not b.done then n,x="opts",1 end return end if c=="top"or c=="ok"then n=a_ end end function touch(aq,p,q)if aq~="board"or n~="play"or b.done or p<1 or q<1 then return end if q==21 then if p<5 or p>15 then return end if z~="pad"then key("ok")end if z=="pad"then s=p-4;key("ok")end elseif q<19 and p<19 then local a=(q-1)//2*9+(p-1)//2 if a==k then key("ok")else k,z,B=a,"grid",nil end end end local function bH(N)local e={}for g=1,9 do if N>>(g-1)&1==1 then e[#e+1]=g end end return e end local function bu()local aK=b.done local aO,aG={},0 local function A(a)local F=aw(a)if not aO[F]then aO[F],aG=true,aG+1 end return F end local w,L,bg={},{},{}for q=0,8 do local j,d={},{}for p=0,8 do local a=q*9+p local c=E[a]local aN=q%3==0 and 2 or(E[a-9]~=c and 1 or 0)local aE=p%3==0 and 2 or(E[a-1]~=c and 1 or 0)if not bg[c]then bg[c]=true j[#j+1]="{"..H[c].sum.."}"elseif aN+aE>0 then j[#j+1]=A(aN*3+aE)else local bQ=E[a-10]~=E[a-9]or E[a-10]~=E[a-1]j[#j+1]=A(bQ and bq or 0)end j[#j+1]=A(br+aN)d[#d+1]=A(bj+aE)local r=m[a]local bt=not aK and not J[a]and r>0 and r~=C[a]and(Y or ae)if a==k and not aK then d[#d+1]=A(aR+r)elseif bt then d[#d+1]="?";L[#L+1]=#w+2;L[#L+1]=#d;L[#L+1]=r elseif r>0 then d[#d+1]=A(bm+r)elseif u[a]~=0 then local e=bH(u[a])d[#d+1]="{"..(#e<=2 and table.concat(e)or(e[1].."+")).."}"else d[#d+1]="."end end j[#j+1]=A(aV)d[#d+1]=A(aV)w[#w+1]=j w[#w+1]=d end A(aU)A(aT)local U if not aK then U={}local aI,i=z=="pad",bz()for g=1,9 do if aI and s==g then U[g]=A(aR+g)elseif i[g]>=9 then U[g]="{}"else U[g]="{"..g.."}"end end U[10]=A(((aI and s==10)and aX or bp)+(S and 1 or 0))U[11]=A((aI and s==11)and aX+2 or bn)end for c=1,#L,3 do local F=aw(aQ+L[c+2])w[L[c]][L[c+1]]=(aO[F]or aG<32)and A(aQ+L[c+2])or"@"end for c,e in ipairs(w)do w[c]=table.concat(e)end w[#w+1]=string.rep(aw(aU),18)..aw(aT)if U then w[#w+1]="."w[#w+1]="...."..table.concat(U)end return table.concat(w,"/")end local function bw(a)local y=H[E[a]]local i=#y.cells if i==1 then return"Given "..y.sum end local ai={}for N=1,511 do local f,c,e=0,0,{}for g=1,9 do if N>>(g-1)&1==1 then f,c,e[#e+1]=f+g,c+1,g end end if c==i and f==y.sum then ai[#ai+1]=table.concat(e,"+")end end local f="Cage "..y.sum.." in "..i if#ai<=2 then return f..": "..table.concat(ai," or ")end return f..", "..#ai.." ways"end local function bC()snail.title("Killer Sudoku - "..ar(b))local f local d=(b.diff-1)*3 if b.done then f="Solved! Best "..W[b.diff].." time "..V(h[d+2])if b.hints>0 then f=f..", "..b.hints..(b.hints==1 and" hint"or" hints")end elseif B then f=B else f=V(b.el).."  "..bw(k)if Y then local i=aF()if i>0 then f=f.."  "..i.." wrong"end end if S then f=f.."  Notes on"end end snail.status(f:sub(1,47))snail.board(bu(),nil,b.done and("Solved in "..V(b.el))or nil)if b.done then snail.text(W[b.diff].." puzzles solved: "..h[d+1]..", average "..V(h[d+3]/math.max(1,h[d+1])))if b.kind=="d"then snail.text("Daily streak: "..av()..(av()==1 and" day"or" days"))end snail.hint(b.kind=="b"and"BACK options OK next puzzle"or"BACK options OK new puzzle")elseif z=="pad"then local aj="place"if s==10 then aj=S and"notes off"or"notes on"elseif s==11 then aj="erase"elseif S then aj="mark"elseif m[k]==s then aj="clear"end snail.hint("BACK grid OK "..aj.." L/R number UP/DN grid")else snail.hint("BACK options OK enter L/R move UP/DN move")end end local function bD()snail.title("Killer Sudoku statistics")for c=1,3 do local d=(c-1)*3 local i=h[d+1]if i>0 then snail.text(W[c]..": "..i.." solved, best "..V(h[d+2])..", average "..V(h[d+3]/i))else snail.text(W[c]..": none solved yet")end end snail.gap()snail.text("Daily puzzles solved: "..h[10])snail.text("Daily streak "..av()..", best "..h[12])snail.hint("BACK back OK back")end local aW={"Fill the grid so every row, column and 3x3 box holds 1 to 9 once each.","Each outlined cage adds up to the sum in its corner, with no number twice inside it.","Heavy lines mark the 3x3 boxes.","Arrows move. OK opens the number pad: pick with LEFT/RIGHT, then OK.","The pencil key turns notes on; the last key erases.","The status line shows the cage you're on and, when there are only one or two, its sums.","BACK opens options: undo, a hint, checking for mistakes and new puzzles.",}function draw()if n=="play"and b then bC()return end if n=="stats"then bD()return end if n=="help"then snail.title("How to play")if snail.touchscreen then aW[4]="Tap a cell, then a number on the pad."end for aY,f in ipairs(aW)do snail.text(f)end snail.hint("BACK back OK back")return end if n=="noclock"then snail.title("Daily puzzle")snail.text("The daily puzzle needs the date. Connect to Wi-Fi once so the clock gets set, then come back.")snail.text("The Easy to Hard puzzles work any time.")snail.hint("BACK back OK back")return end local R=n=="menu"and a9()or bd()if x>#R then x=#R end snail.title(n=="menu"and"Killer Sudoku"or("Killer Sudoku - "..ar(b)))if B then snail.status(B)end for a,bF in ipairs(R)do snail.row(bF[1],a==x)end snail.gap()if n=="menu"then local i=h[1]+h[4]+h[7]snail.small("Daily streak "..av()..", "..i..(i==1 and" puzzle"or" puzzles").." solved.")else snail.small("Time "..V(b.el)..(b.hints>0 and(", hints "..b.hints)or""))end snail.hint("BACK leave OK choose UP/DN move")end function start()snail.ink("fast")bG()if b and not b.done and not(b.kind=="d"and ah()and b.num~=ah())then O()else n,x="menu",1 end end
