WR

WR(w,c,[r],[i])
w
word
c
constant arrays (in our example cs for snake lookup, cd for diagonal lookup)
[r]
always omitted, carries the final result
[i]
always omitted, carries nr. iterations, depending of LEN(w)

Set of functions and study for solving the Word Maze Excel Challenge

Xlambda

Well-known Member
Joined
Mar 8, 2021
Messages
868
Office Version
  1. 365
Platform
  1. Windows
WR: Word maze Results challenge. Please do see latest (26-Oct-21) YT video of MrExcel Solving Word Search With Excel (FMWC) - 2439
This is a compact set of functions that can solve all lookup scenarios.
Defined names: ar, sample array ( A1:T15 ) ; rw, nr. rows of sample array (15) (width is fixed to 20) ; cs, constant array values for "snake" lookup {-1,1, 20,-20} ; cd, constant array values for "diagonal" lookup {-21,-19,19,21}

WL(l)=LAMBDA(l,LET(x,IF(ar=l,1),y,IF(ar="?",1),z,IF(x+y,1),v,IF(z,SEQUENCE(rw,20)),SMALL(v,SEQUENCE(COUNT(v)))))
WA(a,c)=LAMBDA(a,c,LET(x,a-c,y,IF(x>0,x),UNIQUE(SMALL(y,SEQUENCE(COUNT(y ))))))
WB(a,b)=LAMBDA(a,b,FILTER(a,IFNA(XMATCH(a,b,,2),0)))

WR(w,c,[r],[i ])=LAMBDA(w,c,[r],[i ],LET(n,IF(i,i,LEN(w)),x,MID(w,n,1),y,MID(w,n-1,1),z,IF(AND(r=0),WA(WL(x),c),WA(r,c)),IF(n=1,r,WR(w,c,WB(z,WL(y )),n-1))))
Last one WR is !! recursive !! Define the names, then the functions and we are good to go. (Gaps in [i ] are to keep the text no to turn italics)
Will come back with the "concept" explanation asap.
Excel Formula:
=LAMBDA(w,c,[r],[i ],LET(n,IF(i,i,LEN(w)),x,MID(w,n,1),y,MID(w,n-1,1),z,
     IF(AND(r=0),WA(WL(x),c),WA(r,c)),IF(n=1,r,WR(w,c,WB(z,WL(y)),n-1))
    )
)
WORD MAZE.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAK
1Z1SGPE2THEDIZBANTSNSdefined names
2SP?AKIWK1FHVZG2IEKAXar: A1:T15 (sample array)
3?ITQE?LASIXQJNSX4TJKrw: 15 (width is always 20)
4LENJFXA4TIKNSBX1DJCPcs: {-1,1,20,-20} (constant array for "snake" type lookup
5GDL33DMVLTGK?CU3OQOOcd: {-21,-19,19,21} (constant array for "diagonal" type lookup
6IAIUVGYLU?IEP3WGKBTP
74APAU4CZVFFFAFIPAXTT"snake" lookup"diagonal" lookup
8HN?4GCKS4IDRHNNAJDNS
9EC3XCODBVQOYJERGSKAH=WR("snake",cs)=WR("diagonal",cd)
10EX1Z?PNI3OWCCN2UREGJ382
11EONMEVKABEPFLQNZ?EW318
12KAN?AIBKLG24?N?ZBNQE20=WR("advance",cd)
13SMO4YFBBRE?DNE1ADJM416067
14CFX43TY1ZJNIWUZ3OWHM224
15TYD2YZSUDVVF4OZEKA?S300
16
17=SEQUENCE(rw,20)=WR("difficult",cs)
181234567891011121314151617181920151
192122232425262728293031323334353637383940
204142434445464748495051525354555657585960=WR("winner",cs)
216162636465666768697071727374757677787980115
2281828384858687888990919293949596979899100273
23101102103104105106107108109110111112113114115116117118119120
24121122123124125126127128129130131132133134135136137138139140
25141142143144145146147148149150151152153154155156157158159160Note: I left the results in "sequence numbers" representation on purpose,
26161162163164165166167168169170171172173174175176177178179180to be much easier to visualize the results.
27181182183184185186187188189190191192193194195196197198199200To get them to "range" representation we can use PR(a) (print results)
28201202203204205206207208209210211212213214215216217218219220PR(a)=LAMBDA(a,LET(x,QUOTIENT(a-1,20)+1,y,MOD(a-1,20)+1,ADDRESS(x,y,4)))
29221222223224225226227228229230231232233234235236237238239240
30241242243244245246247248249250251252253254255256257258259260
31261262263264265266267268269270271272273274275276277278279280C1B5
32281282283284285286287288289290291292293294295296297298299300R1
33T1G4
34Important Note: Covered here only snake and diagonal search, for being the more complicated ones.T8
35Anyhow, to cover the rest of them the only thing we have to do is defining new constant arraysD12
36cs (constant snake) {-1,1,20,-20}T15
37cd (constant diagonal) {-21,-19,19,21}
38For example, for horizontal lookup we have to defineK8
39ch {-1,1} and for vertical cv: {-20,20}
40O6
41M14
42
43
WM snake diagonal
Cell Formulas
RangeFormula
W9,W20,W17,A17,AC12,AC9W9=FORMULATEXT(W10)
W10:W15W10=WR("snake",cs)
AC10AC10=WR("diagonal",cd)
AC13AC13=WR("advance",cd)
A18:T32A18=SEQUENCE(rw,20)
W18W18=WR("difficult",cs)
W21:W22W21=WR("winner",cs)
W31:W36W31=PR(W10#)
AC31AC31=PR(AC10)
AC33,W38AC33=PR(AC13)
W40:W41W40=PR(W21#)
Dynamic array formulas.
Named Ranges
NameRefers ToCells
ar='WM snake diagonal'!$A$1:$T$15AC13, AC10, W21, W18, W10
 
Upvote 0
Few examples before revealing the concept.
SDK array.xlsx
ABCDEFGHIJKLMNOPQRSTUVWX
1extreme sudokus from sudoku.com
2=SK.2IJ(B3:J11)
3755 218284753196
42845396218745
552157496832
671672531984
7814927831649257
8926549827613
9418415962378
1034763185429
118456928374561
12
13 SK.2IJ function solves using recursion and also shows info about recursive loops.
142nd value, total loops, 1st value, nr. of iterations of last loop that solved the puzzle.
15
Sheet3
Cell Formulas
RangeFormula
L2L2=FORMULATEXT(L3)
L3:U11L3=SK.2IJ(B3:J11)
Dynamic array formulas.

SDK array.xlsx
ABCDEFGHIJKLMNOPQRSTUV
15
16=SK.2IJ(B17:J25)
1771654 76427319865
18972915786243
198683452791
20917598261437
21491764835912
223132974586
231429871543629
2425349627158
2553256198374
26
Sheet3
Cell Formulas
RangeFormula
L16L16=FORMULATEXT(L17)
L17:U25L17=SK.2IJ(B17:J25)
Dynamic array formulas.

SDK array.xlsx
ABCDEFGHIJKLMNOPQRSTUV
26
27how many loops for "creating" a solved puzzle from scratch
28=SK.2IJ(B29:J37)
2929 403653849712
30891327645
31274165389
32428736951
33165298473
34937514268
35349651827
36712483596
37586972134
38
Sheet3
Cell Formulas
RangeFormula
L28L28=FORMULATEXT(L29)
L29:U37L29=SK.2IJ(B29:J37)
Dynamic array formulas.
 
A simpler solution, for a single function, but this time with 5x5 of 9x9 arrays on which every row and every column of any next 9x9 array has no dups (SUDOKUs have also every next 3x3 arrays with no dups)
It's iterative, 5 in 5 loops of 3M iterations => total 75M iterations
NK(n,k) delivers kxk of nxn arrays Each nxn array has no dups byrow or bycol
Excel Formula:
=LAMBDA(n, k,
    LET(
        s, SEQUENCE(n),
        q, SEQUENCE(k),
        r, LAMBDA(n, TOROW(SORTBY(s, RANDARRAY(n)))),
        c, LAMBDA(x, AND(BYCOL(x, LAMBDA(y, ROWS(UNIQUE(y)) = ROWS(x))))),
        b, LAMBDA(x,
            REDUCE(
                r(n),
                SEQUENCE(10 ^ 6, 3),
                LAMBDA(v, i, IF(ROWS(v) = x, v, LET(a, VSTACK(v, r(x)), IF(c(a), a, v))))
            )
        ),
        DROP(REDUCE("", q, LAMBDA(v, i, VSTACK(v, DROP(REDUCE("", q, LAMBDA(v, i, HSTACK(v, b(n)))), , 1)))), 1)
    )
)
NNK.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAU
1
2=NK(9,5)
3618345972179863425312954678672518394346925718
4926851743795148632549378126561273849752849361
5435789621586427391734826951735691482168593247
6361478259627519843267519384189346275289167453
7754192368234795186123497865924835167514782639
8283964517953674218496785213358469721693274185
9179523486341286957875631492296784513937451826
10847236195418352769981263547843127956825316974
11592617834862931574658142739417952638471638592
12539724816462793815786534129495821736857961423
13217835649213956784654192387528764319934156278
14756148923658142937195827436271543698518294637
15971653284574368129427915863736982451179823564
16395287461389517246831759642953417862621437985
17864379152125674398972346518649258173386749152
18628491375741289653368271954182635947742518396
19143562798897431562549683271364179285465372819
20482916537936825471213468795817396524293685741
21857163924697314582371294865945238167735891246
22932846157348796125182675934764915328176534928
23269715483254867913219457386571624983612973485
24624359718921685374924186573183496275398462751
25795432861173249856897362451629843751961248537
26178294635869153247738521649432781596289756314
27581927346782531469645738192817569432453617892
28346581279415972638456913728298357614524189673
29413678592536428791563849217356172849847325169
30592436718214958763374215689546128379856192347
31135648279395482176731596248725681934394586721
32647921853876521349563128974352719846621479538
33876159324453869217297684351861294753435618972
34213584697782613594942863517479832561283754619
35729863145537294681158342796217453698567941283
36354217986148736925815937462683947125178325496
37981375462629175438486759123194365287719263854
38468792531961347852629471835938576412942837165
39159472386532869471647812593286745931347865291
40612345798928514736183295467372469518513689724
41265781943416297385439568271924518673789543612
42371824659379628154528971346153274896874312956
43896253174165973248892137654745186329462157389
44748196235853741692761453982419653287231496578
45523918467794156823214689735638927154958271463
46437569812241385967356724819597831462625938147
47984637521687432519975346128861392745196724835
48
Sheet2
Cell Formulas
RangeFormula
B2B2=FORMULATEXT(B3)
B3:AT47B3=NK(9,5)
Dynamic array formulas.

A lambda that has number of iterations "ni" as separate parameter
(I found the solution with real SUDOKU's far more challenging, this is only if someone prefers a sigle cell formula)
NK(n,k,ni) delivers kxk of nxn arrays using a "k" in "k" loops of "ni"x3 iterations for each loop => k*k*ni*3 iterations. (ni grows "mega" exponential with the size of "n" (for n=5, ni=1000 is enough)
Excel Formula:
=LAMBDA(n, k, ni,
    LET(
        s, SEQUENCE(n),
        q, SEQUENCE(k),
        r, LAMBDA(n, TOROW(SORTBY(s, RANDARRAY(n)))),
        c, LAMBDA(x, AND(BYCOL(x, LAMBDA(y, ROWS(UNIQUE(y)) = ROWS(x))))),
        b, LAMBDA(x,
            REDUCE(
                r(n),
                SEQUENCE(ni, 3),
                LAMBDA(v, i, IF(ROWS(v) = x, v, LET(a, VSTACK(v, r(x)), IF(c(a), a, v))))
            )
        ),
        DROP(
            REDUCE(
                "",
                q,
                LAMBDA(v, i, VSTACK(v, DROP(REDUCE("", q, LAMBDA(v, i, HSTACK(v, b(n)))), , 1)))
            ),
            1
        )
    )
)
Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAA
1
2=NK(4,3,100)
3341241324321
4213424133412
5124313241234
6432132412143
7432112342314
8341241231423
9123423414132
10214334123241
11132414232431
12324142313124
13413223144312
14241331421243
15
16=NK(5,5,1000)
174231552341324155231424351
182315423415231541324551243
195124331254513422415335124
201453245132452314153212435
213542114523145233542143512
223251443125123451325424315
231534251432541234513212534
242145312543314523254131452
255423135214235145142345123
264312524351452312431553241
272145314235523142143543512
284253123154214353451235124
293521445321431524235114235
301432531542145231524321453
315314252413352415312452341
323125451342214355341234512
331534223154543213524141325
345241342531125432135413254
352453115423352141452352143
364312534215431524213525431
374532125431213455142353124
385241353214541323214541532
391354214325352141523432451
403125442153124534351224315
412413531542435212435115243
42
Sheet1
Cell Formulas
RangeFormula
B2,B16B2=FORMULATEXT(B3)
B3:M14B3=NK(4,3,100)
B17:Z41B17=NK(5,5,1000)
Dynamic array formulas.
Lambda Functions
NameFormula
NK=LAMBDA(n,k,ni,LET(s,SEQUENCE(n),q,SEQUENCE(k),r,LAMBDA(n,TOROW(SORTBY(s,RANDARRAY(n)))),c,LAMBDA(x,AND(BYCOL(x,LAMBDA(y,ROWS(UNIQUE(y))=ROWS(x))))),b,LAMBDA(x,REDUCE(r(n),SEQUENCE(ni,3),LAMBDA(v,i,IF(ROWS(v)=x,v,LET(a,VSTACK(v,r(x)),IF(c(a),a,v)))))),DROP(REDUCE("",q,LAMBDA(v,i,VSTACK(v,DROP(REDUCE("",q,LAMBDA(v,i,HSTACK(v,b(n)))),,1)))),1)))
 
NKni.xlsx
ABCDEFGHIJKLMNO
1
2
3checking: no dups byrow or by col for a single 8x8 array
4
5=NK(8,1,100000)=BYROW(B6#,LAMBDA(x,COUNT(UNIQUE(x,1))))
6714836258
7536784128
8347162588
9852341768
10461257838
11278513648
12625478318
13183625478
14
15=BYCOL(B6#,LAMBDA(x,COUNT(UNIQUE(x))))
1688888888
17
Sheet2
Cell Formulas
RangeFormula
B5,B15,K5B5=FORMULATEXT(B6)
B6:I13B6=NK(8,1,100000)
K6:K13K6=BYROW(B6#,LAMBDA(x,COUNT(UNIQUE(x,1))))
B16:I16B16=BYCOL(B6#,LAMBDA(x,COUNT(UNIQUE(x))))
Dynamic array formulas.
 
This is amazing - a collection of valid Sudoku grids!
Glad you liked it. ✌
Remember we were talking about recursion limitations? Turns out it was some kind of a glitch.
Check this out, made a post about it here:
 

Forum statistics

Threads
1,225,131
Messages
6,183,036
Members
453,146
Latest member
scarabeovini

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top