--!name Nonograms
--!icon puzzle
--!category Puzzles
local aO="!$%&()+,-0123456789:;<=>?ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnpqrstuvwxyz|~"local function D(b)return aO:sub(b+1,b+1)end local au,aQ,av,aS,aN,aR,aP,aW=16,20,24,25,28,31,34,35 local P={5,10,15}local at={"h+lm0zw4808xv708wf200sxG0v-l+01o-G0TNv70Z+lt08xwd08xxY08pWFW8vto0TyX60L-xY0CKdE09zoeW","G9Xd+tkjTxSv-bwBG30U3yFm-7+Vx-m030007W-hEhKfRDtl-007X-ktpF-o48GX3y003yFmX248GXETxtlCOG9Xd+--U--yzpZy7W20C0u7m-7y23-t+Fm30I0mFmC0m37Ct+7W7W-7+RPpZy7W01u30GAXK2BH-l-VwXQ50000E3zT----Vu-Gu0030U3yVx-q2MfObYVu9182G03+Fw-d+dm-y30U184WU1uVnjao307W-7+---t+FmC0mFm70+3S-0-7z-z-Zy001087S-x----z-d+Cm30-F+-+QGW2084WC0FWu72uVWk0u1mZy7W","e2d0Swhd-q-+7ku-+7-my7d0Su3d--000000000G0701K0Vm7-1-yV-p-+003-+BMXQq5j0-u0000G0300y0Fm3+0-mF+0200G7--V-n-y7-000000821WmEE1-mV-3EOV-3xuFU0-WF+J-wV-Z-uDs01y0-uDzZlkTzplkDzW-u3+08W140FW1y0FW000O0tWF+3-w-xk-w-VNnwy7N1wyVNprUDLW200000000e020-WV-7Vz--l-+V-nWE73WVu1+0000000m0F07+1-ul-TsvTxhlTEtH-w7-0Vm7-0000006C0nW6C0nW6C1-uV-ZViV-ZvyFF0-mF-3-ySpW0G0701y0DW140DW1y0FW3+0tOExXdC0e0Q02I00u0FW1y1FWTyZlkTzp-kFzmF-1-mFW1y0-u3+00001y0Vm772tQcZKqvdRCSnnyF00--F-x--0000G070nKPginyOVm1y08W140Vm3+0GG630-u--u1y0-uE-Zx+Vjt-t--x-+FNWIG1K0700u0FW7-0000-0F+3-uT-Z-yU-Zd-S-pbwSkJbICgGcs0001y0Vm7N0tO7N1uyC1ZWEO0p06O0nWC7-1mS000O0pWEF-Z-+uu+23oIU23uuxw+Vln-yF-W-u630"}local T={}for a in("Crown|Flag|Pine tree|Arrow|Fish|Mug|Key|Mushroom|Cat|House|Diamond|Sailboat|Bell|Umbrella|Heart|Note|Star|Owl|Bell|Snail|Mushroom|Notes|Cat|Sailboat|Anchor|Light bulb|Crab|Fish|House|Coffee|Rocket|Tree|Duck|Apple|Umbrella|Moon|Castle|Cupcake|Ship|Cat|Balloon|Butterfly|Whale|Teapot|Rabbit|Rocket|Cactus|Snail|Lighthouse|Tree|Elephant|Penguin|Owl"):gmatch("[^|]+")do T[#T+1]=a end local ax={"Small","Medium","Large"}local aT={3,5,5}local aV={0,36,0}local aU={1,1,1}local ae="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+-"local W={}for b=1,64 do W[ae:byte(b)]=b-1 end local x,G,J={},{},0 for e=1,3 do local k=P[e]x[e]=#at[e]//((k*k+5)//6)G[e]=J J=J+x[e]end local function aI(e,m)local k=P[e]local aj=(k*k+5)//6 local bi=at[e]:sub((m-1)*aj+1,m*aj)local d,g={},0 for b=1,aj do local i=W[bi:byte(b)]or 0 for aX=5,0,-1 do g=g+1 if g<=k*k then d[g]=i>>aX&1 end end end return d end local function ar(j)for e=3,1,-1 do if j>G[e]then return e,j-G[e]end end end local f,E,u,O=5,3,1,1 local C,o,_,X={},{},{},{}local c local z={}for b=1,J do z[b]=0 end local t={}local F,r,U=false,nil,false local I={}local q=1 local n,w,az="menu",1,"menu"local s,y=1,1 local H,Y=nil,nil local function S(a)a=math.floor(a or 0)if a>=3600 then return string.format("%d:%02d:%02d",a//3600,a//60%60,a%60)end return string.format("%d:%02d",a//60,a%60)end local function V(e)return P[e].."x"..P[e]end local function ak(p,a,Q,i)local d,h={},0 for b=0,f-1 do if p[a+b*Q]==i then h=h+1 elseif h>0 then d[#d+1]=h;h=0 end end if h>0 then d[#d+1]=h end return d end local function al(e,m)u,O=e,m f,E=P[e],aT[e]C,o,_,X=aI(e,m),{},{},{}for b=1,f*f do o[b]=0 end for h=0,f-1 do _[h+1]=ak(C,h*f+1,1,1)end for l=0,f-1 do X[l+1]=ak(C,l+1,f,1)end c={id=G[e]+m,el=0,hints=0,done=false}q,I,r,U,H=1,{},nil,false,nil end local function bf()local d={}for b=1,f*f,3 do local i=(o[b]<<4)|((o[b+1]or 0)<<2)|(o[b+2]or 0)d[#d+1]=ae:sub(i+1,i+1)end return table.concat(d)end local function aM(a,k)local d={}for b=1,k*k do local i=W[a:byte((b+2)//3)]or 0 local ad=i>>(4-2*((b-1)%3))&3 d[b]=ad<=2 and ad or 0 end return d end local function an(j)for ay,v in ipairs(t)do if v.id==j then return v end end end local function aJ(v)local j,a3,a6,l,a=v.s:match("^(%d+),(%d+),(%d+),(%d+),(.*)$")local e,m=ar(tonumber(j))al(e,m)c.el,c.hints=tonumber(a3),tonumber(a6)q=math.max(1,math.min(f*f,tonumber(l)))o=aM(a,f)return true end local function a4(i)i=math.max(0,math.min(262143,math.floor(i)))local d={}for b=2,0,-1 do local Q=i>>(6*b)&63 d[#d+1]=ae:sub(Q+1,Q+1)end return table.concat(d)end local function bl()for b=1,f*f do if o[b]~=0 then return true end end end local function L()if c then for b=#t,1,-1 do if t[b].id==c.id then table.remove(t,b)end end if not c.done and bl()then table.insert(t,1,{id=c.id,s=table.concat({c.id,c.el,c.hints,q,bf()},",")})end end while#t>8 do table.remove(t)end local d={}for j=1,J do d[j]=a4(z[j])end local a7="NG1|"..table.concat(d).."|"..(F and 1 or 0)local a repeat local ag={a7}for ay,v in ipairs(t)do ag[#ag+1]=v.s end a=table.concat(ag,"|")if#a>1020 then table.remove(t)end until#a<=1020 snail.save(a)end local function bc()local a=snail.load()if type(a)~="string"or a:sub(1,4)~="NG1|"then return end local M={}for ad in(a.."|"):gmatch("([^|]*)|")do M[#M+1]=ad end local bk=M[2]or""for j=1,J do local i=0 for g=j*3-2,j*3 do i=(i<<6)|(W[bk:byte(g)or 48]or 0)end z[j]=i end F=M[3]=="1"for b=4,#M do local j=tonumber(M[b]:match("^(%d+),"))if j and j>=1 and j<=J then t[#t+1]={id=j,s=M[b]}end end end local function aE(a,Q,aB)local d=ak(o,a,Q,1)if#d~=#aB then return false end for b=1,#d do if d[b]~=aB[b]then return false end end return true end local function as(b)return(o[b]==1 and C[b]==0)or(o[b]==2 and C[b]==1)end local function bd()local k=0 for b=1,f*f do if as(b)then k=k+1 end end return k end local function aa(b,i)if o[b]==i then return end I[#I+1]=b*4+o[b]if#I>200 then table.remove(I,1)end o[b]=i end local function ah()for b=1,f*f do if(o[b]==1 and 1 or 0)~=C[b]then return end end c.done,r=true,nil local d=z[c.id]if d==0 or c.el<d then z[c.id]=math.max(1,c.el)end end local function R(e)local k=0 for g=1,x[e]do if z[G[e]+g]>0 then k=k+1 end end return k end local function Z(e,a5)for g=1,x[e]do local m=(a5+g-1)%x[e]+1 if z[G[e]+m]==0 then return m end end end local function a8()for g=0,f*f-1 do local b=(q-1+g)%(f*f)+1 if as(b)or(C[b]==1 and o[b]~=1)then aa(b,C[b]==1 and 1 or 2)q=b c.hints=c.hints+1 H=C[b]==1 and"That square is filled"or"That square is empty"ah()return end end end local function N()n,Y="play",os.time()end local function ao(e,m)if c then L()end c=nil local v=an(G[e]+m)if v and not pcall(aJ,v)then c=nil end if not c then al(e,m)end N()end local function ab(e)s=e y=(c and u==e and O)or Z(e,0)or 1 n="list"end local function aA()local aG=os.time()if c and n=="play"and Y and not c.done then local aD=aG-Y if aD>0 then c.el=c.el+math.min(aD,600)end end Y=aG end local function ai()return V(u).." #"..O end local function aF()local d={}if c and not c.done then d[1]={"Continue: "..ai().." ("..S(c.el)..")","cont"}elseif t[1]then local e,m=ar(t[1].id)d[1]={"Continue: "..V(e).." #"..m,"resume"}end for e=1,3 do d[#d+1]={ax[e].." "..V(e)..": "..R(e).." of "..x[e].." solved",e}end d[#d+1]={"Drag painting: "..(F and"on"or"off"),"drag"}d[#d+1]={"How to play","help"}return d end local function aH()return{{"Back to the puzzle","cont"},{#I>0 and("Undo ("..#I..(#I==1 and" change)"or" changes)"))or"Undo (nothing to undo)","undo"},{"Check for mistakes","check"},{"Hint: fix a square","hint"},{"Drag painting: "..(F and"on"or"off"),"drag"},{"Restart this puzzle","restart"},{"Puzzle list","list"},{"Main menu","menu"},{"How to play","help"},}end local function a_(p)if p=="cont"then N()elseif p=="resume"then local e,m=ar(t[1].id)ao(e,m)elseif p=="undo"then local ac=table.remove(I)if ac then o[ac//4],q=ac%4,ac//4 N()L()else H="Nothing to undo"end elseif p=="check"then N()local k=bd()U=k>0 H=k==0 and"No mistakes so far"or(k==1 and"1 mistake, marked"or k.." mistakes, marked")elseif p=="hint"then N()if not c.done then a8();L()end elseif p=="drag"then F,r=not F,nil L()elseif p=="restart"then al(u,O)N()L()elseif p=="list"then ab(u)elseif p=="menu"then n,w="menu",1 elseif type(p)=="number"then ab(p)else az,n=n,p end end local function be(g)local h,l=(q-1)//f,(q-1)%f if g=="up"then h=h>0 and h-1 or(r and 0 or f-1)elseif g=="down"then h=h<f-1 and h+1 or(r and h or 0)elseif g=="left"then l=l>0 and l-1 or(r and 0 or f-1)elseif g=="right"then l=l<f-1 and l+1 or(r and l or 0)end q=h*f+l+1 end local function bh(g)if c.done then if g=="top"then ab(u)elseif g=="ok"then local m=Z(u,O)if m then ao(u,m)else ab(u)end end return end if g=="top"then r=nil n,w="opts",1 L()return end if g=="ok"then if F and r then r=nil return end local i=(o[q]+1)%3 aa(q,i)if F then r=i end else be(g)if r then aa(q,r)else return end end ah()L()end local function bb(g)if g=="up"then y=(y-2)%x[s]+1 elseif g=="down"then y=y%x[s]+1 elseif g=="left"or g=="right"then s=(s+(g=="left"and 1 or 0))%3+1 y=Z(s,0)or 1 elseif g=="ok"then ao(s,y)elseif g=="top"then n,w="menu",1 end end function key(g)aA()H,U=nil,false if n=="play"and c then bh(g)elseif n=="list"then bb(g)elseif n=="menu"or n=="opts"then local K=n=="menu"and aF()or aH()if g=="up"then w=(w-2)%#K+1 elseif g=="down"then w=w%#K+1 elseif g=="ok"then a_(K[math.min(w,#K)][2])end elseif g=="top"or g=="ok"then n=az end end function touch(ba,l,h)if ba~="board"or n~="play"or not c or c.done then return end l,h=l-E,h-E if l<1 or l>f or h<1 or h>f then return end local b=(h-1)*f+l if r and b~=q then aA()H,U,q=nil,false,b aa(b,r)ah()L()else q=b key("ok")end end local function aC(af,d,A)local i if#af==0 then if d==E then i=0 end else i=af[#af-(E-d)]end return i and D(A+i)or"."end local function aY()local A=aV[u]local B={}local a0,aZ=(q-1)//f,(q-1)%f for d=1,E do local a={string.rep(".",E)}for l=1,f do a[#a+1]=aC(X[l],d,A)end a[#a+1]="."B[d]=table.concat(a)end for h=0,f-1 do local a={}for d=1,E do a[d]=aC(_[h+1],d,A)end for l=0,f-1 do local b=h*f+l+1 local i=o[b]if U and as(b)then a[#a+1]="@"elseif b==q then a[#a+1]=D(A+aS+i)elseif i==1 then a[#a+1]=D(A+av)else a[#a+1]=D(A+(i==2 and aQ or au)+(l%5==0 and 1 or 0)+(h%5==0 and 2 or 0))end end a[#a+1]=D(A+aN+((h==a0 and 2)or(aE(h*f+1,1,_[h+1])and 1)or 0))B[#B+1]=table.concat(a)end local a={string.rep(".",E)}for l=0,f-1 do a[#a+1]=D(A+aR+((l==aZ and 2)or(aE(l+1,f,X[l+1])and 1)or 0))end a[#a+1]=D(A+aP)B[#B+1]=table.concat(a)return table.concat(B,"/")end local function am(k,p,bj,bg)local B={}local aK=string.rep(".",bg)for h=0,k-1 do local a={aK}for l=0,k-1 do local i=p[h*k+l+1]if bj then a[#a+1]=i==1 and D(aW)or"."else a[#a+1]=i==1 and D(av)or D(au+(l%5==0 and 1 or 0)+(h%5==0 and 2 or 0))end end a[#a+1]=aK B[#B+1]=table.concat(a)end return table.concat(B,"/")end local function a2()snail.title("Nonograms - "..ai())if c.done then local j=c.id snail.status((T[j].."! Solved in "..S(c.el)..(c.hints>0 and(", "..c.hints..(c.hints==1 and" hint"or" hints"))or"")):sub(1,47))snail.board(am(f,C,true,aU[u]))snail.center(true)snail.text(T[j])snail.small("Best time "..S(z[j])..". "..R(u).." of "..x[u].." "..V(u).." pictures solved.")snail.hint(Z(u,O)and"BACK puzzle list OK next puzzle"or"BACK puzzle list OK puzzle list")return end local a=H if not a then a=S(c.el)if c.hints>0 then a=a.."  Hints "..c.hints end if r then a=a.."  Painting: "..(r==1 and"fill"or r==2 and"cross"or"clear")end end snail.status(a:sub(1,47))snail.board(aY())local aq if F then aq=r and"lift pen"or({"paint fill","paint cross","paint clear"})[(o[q]+1)%3+1]else aq=({"fill","cross","clear"})[(o[q]+1)%3+1]end snail.hint("BACK options OK "..aq.." L/R move UP/DN move")end local function a1()snail.title("Nonograms - "..ax[s].." "..V(s))snail.status(R(s).." of "..x[s].." solved")local k=x[s]local aL=math.max(1,math.min(y-3,k-6))for m=aL,math.min(k,aL+6)do local j=G[s]+m local a if z[j]>0 then a=m..". "..T[j].." ("..S(z[j])..")"elseif an(j)or(c and c.id==j and not c.done)then a=m..". In progress"else a=m..". Not solved yet"end snail.row(a,m==y)end snail.gap()local j,ap=G[s]+y,P[s]if z[j]>0 then snail.board(am(ap,aI(s,y),true,0),"small")else local p if c and c.id==j then p=o else local v=an(j)p=v and aM(v.s:match("([^,]*)$"),ap)or{}end snail.board(am(ap,p,false,0),"small")end snail.hint("BACK menu OK play L/R size UP/DN move")end local aw={"Each number is a run of filled squares in that row or column, in order, with gaps between.","Arrows move. OK cycles a square: filled, crossed out, empty. Crosses are just your notes.","A tick at the edge means that line matches its clue. Finish to see what the picture is.","BACK opens options: undo, check for mistakes, a hint, and drag painting.","Drag painting: OK puts the pen down, the arrows paint as you move, OK lifts it.",}function draw()if n=="play"and c then a2()return end if n=="list"then a1()return end if n=="help"then snail.title("How to play")if snail.touchscreen then aw[2]="Tap a square to cycle it: filled, crossed out, empty. Crosses are just your notes."end for ay,a in ipairs(aw)do snail.text(a)end snail.hint("BACK back OK back")return end local K=n=="menu"and aF()or aH()if w>#K then w=#K end snail.title(n=="menu"and"Nonograms"or("Nonograms - "..ai()))if H then snail.status(H)end for b,a9 in ipairs(K)do snail.row(a9[1],b==w)end snail.gap()if n=="menu"then local k=R(1)+R(2)+R(3)snail.small(k.." of "..J.." pictures solved.")else snail.small("Time "..S(c.el)..(c.hints>0 and(", hints "..c.hints)or""))end snail.hint("BACK leave OK choose UP/DN move")end function start()snail.ink("fast")bc()if t[1]and pcall(aJ,t[1])and c then N()else c,n,w=nil,"menu",1 end end
