Part 1
Description
A robot has to go through a maze of walls (#) and open spaces (.). Some open spaces have a number, 0 to 7 in my maze. The robot starts at 0, and Part 1 asks for the fewest steps that take it to every number at least once, in any order.
Solution
I split this into two smaller problems. First, find the shortest walking distance between every two numbered locations. There are only 28 pairs of the 8 locations. After that, the maze isn't needed any more, and what remains is a small "which order should I visit them in" problem: 7 locations after the start can be visited in 7! = 5,040 orders. Excel can just try all of them.
The distances. Using the name manager, I created a BFS lambda function to count the distance between two places. It looks at all the cells one step away from the start, then two steps away, and so on, and never walks into #. It remembers where each cell was reached from, so when it finds the goal it can walk back to count the steps:
=LAMBDA(grid,sx,sy,tx,ty,LET(w,COLUMNS(grid),h,ROWS(grid),dirs,{0,-1;-1,0;1,0;0,1},tg_keys,ty*1000+tx,start,HSTACK(sx,sy,0,-1),result,REDUCE(start,SEQUENCE(w*h),LAMBDA(state,lev,LET(keys,INDEX(state,,2)*1000+INDEX(state,,1),IF(SUM(--ISNUMBER(XMATCH(keys,tg_keys)))>0,state,LET(frontier,FILTER(state,INDEX(state,,3)=lev-1,HSTACK(-1,-1,-1,-1)),IF(INDEX(frontier,1,1)=-1,state,LET(candidates,DROP(REDUCE(HSTACK(0,0,0,0),SEQUENCE(ROWS(frontier)),LAMBDA(acc,i,LET(x,INDEX(frontier,i,1),y,INDEX(frontier,i,2),pk,y*1000+x,nx,x+CHOOSECOLS(dirs,1),ny,y+CHOOSECOLS(dirs,2),VSTACK(acc,HSTACK(nx,ny,EXPAND(lev,4,1,lev),EXPAND(pk,4,1,pk)))))),1),cand_keys,INDEX(candidates,,2)*1000+INDEX(candidates,,1),valid,MAP(SEQUENCE(ROWS(candidates)),LAMBDA(i,LET(x,INDEX(candidates,i,1),y,INDEX(candidates,i,2),k,INDEX(cand_keys,i),IF(OR(x<0,x>=w,y<0,y>=h),FALSE,AND(INDEX(grid,y+1,x+1)<>"#",ISNA(XMATCH(k,keys)),XMATCH(k,cand_keys)=i))))),fresh,FILTER(candidates,valid,HSTACK(-1,-1,-1,-1)),IF(INDEX(fresh,1,1)=-1,state,VSTACK(state,fresh))))))))),final_keys,INDEX(result,,2)*1000+INDEX(result,,1),hits,FILTER(result,ISNUMBER(XMATCH(final_keys,tg_keys)),HSTACK(-1,-1,-1,-1)),IF(INDEX(hits,1,1)=-1,"Nopath",LET(steps,INDEX(hits,1,3),path,REDUCE(INDEX(hits,1,{1,2,3,4}),SEQUENCE(steps),LAMBDA(acc,s,LET(pk,INDEX(acc,1,4),prev,FILTER(result,final_keys=pk),VSTACK(prev,acc)))),steps))))
grid is the excel representation of the input, using:
=LET(
input, A5:A45,
grid, MID(input, SEQUENCE(, MAX(LEN(input))), 1),
sx, sy, tx and ty are starting points and target points.
All the orders. For the 5,040 orders I used a formula, like below. The perms parts puts 0 in front of 1234567, etc.
=LET(
input, A5:A45,
grid, MID(input, SEQUENCE(, MAX(LEN(input))), 1),
numb, IFERROR(--grid, grid),
highest, MAX(numb),
rrr, SEQUENCE(ROWS(grid), , 0),
ccc, SEQUENCE(, COLUMNS(grid), 0),
pts, SORT(HSTACK(
TOCOL(IF(ISNUMBER(numb), numb, NA()), 2),
TOCOL(IF(ISNUMBER(numb), ccc, NA()), 2),
TOCOL(IF(ISNUMBER(numb), rrr, NA()), 2)
)),
nnn, SEQUENCE(highest + 1, , 0),
comb, FILTER(TOCOL(nnn & TRANSPOSE(nnn)), TOCOL(nnn < TRANSPOSE(nnn))),
dist, MAP(comb, LAMBDA(pair,
LET(
aaa, --LEFT(pair, 1) + 1,
bbb, --RIGHT(pair, 1) + 1,
BFSa(numb, INDEX(pts, aaa, 2), INDEX(pts, aaa, 3), INDEX(pts, bbb, 2), INDEX(pts, bbb, 3))
)
)),
perms, "0" & REDUCE("", SEQUENCE(highest), LAMBDA(acc,k,
TOCOL(IFS(ISERROR(FIND(SEQUENCE(, highest), acc)), acc & SEQUENCE(, highest)), 2)
)),
steps, MID(perms, SEQUENCE(, highest), 2),
norm, IF(LEFT(steps, 1) < RIGHT(steps, 1), steps, RIGHT(steps, 1) & LEFT(steps, 1)),
totals, BYROW(INDEX(dist, XMATCH(norm, comb)), LAMBDA(row, SUM(row))),
MIN(totals)
)Part 2
Description
Now the robot has to go back to location 0 at the end. The question is the fewest steps for the whole trip.
Solution
For each route I also need the distance from its last location back to 0. In V46 I typed 0, and W46 looks up the distance between the last location and V46. The total then includes this extra step, and the answer is the shortest of all the totals, or by adapting my formula, adding 0 in the perms part.
