The answer 66 seems correct. A brute force python search also resulted in 66
import itertools as it
import numpy as np
H=4
V=2
detours=["LLRR", "LDRU", "DDUU"]
combi=[list("RRRRUU")+list(detour) for detour in detours]
# so we create all permuations of the 3 combinations of normal moves and de-tours
# this would create many duplicates but since we use set comprehension
# each possible move will be there only once.
# certainly not the most efficient way but
# only takes fractions of a seconds anyways
moves={move for move in it.chain(*[it.permutations(p) for p in combi])}
print("total number of moves to consider",len(moves))
def pos(pin,move):
"""
pin position in (x,y) pair
move a letter indicating the move direction
returns a tuple of (x,y,hidx,vidx,mv)
where x y is the position after move
hidx and vidx is the index of the horizontal and
vertical nodes that is touched and
mv is either 'H' or 'V'
"""
x,y=pin
hindex,vindex=x,y
mv='H'
match move:
case 'R':
hindex=x
x+=1
case 'L':
x-=1
hindex=x
case 'U':
vindex=y
y+=1
mv='V'
case 'D':
y-=1
vindex=y
mv='V'
case _: assert False,"illegal direction"
pout=(x,y,hindex,vindex,mv)
return pout
def domoves(m):
"""
do the moves in list m
returns a tuple (xmax,xmin,ymax,ymin,edgemax,m)
with the minimum and maximum x and y positions to see if
we left the field
and the maximum number edges we traversed
so should be 1 in order not to move twice on an edge
"""
hedge=np.zeros((H,V+1))
vedge=np.zeros((H+1,V))
x,y,hidx,vidx=0,0,0,0
xmax=0
xmin=0
ymax=0
ymin=0
for move in m:
x,y,hidx,vidx,mv=pos((x,y),move)
xmax=max(x,xmax)
xmin=min(x,xmin)
ymax=max(y,ymax)
ymin=min(y,ymin)
if xmax <= H and xmin >= 0 and ymax <= V and ymin >=0:
if mv=='H':
hedge[hidx,vidx] += 1
else:
vedge[hidx,vidx] += 1
hmax=np.max(hedge)
vmax=np.max(vedge)
edgemax=max(hmax,vmax)
assert x==H and y==V
return (xmax,xmin,ymax,ymin,edgemax,m)
mres=[domoves(mv) for mv in moves]
mvalid=[m for xmax,xmin,ymax,ymin,emax,m in mres if xmax <= H and xmin >= 0 and ymax <= V and ymin >= 0 and emax == 1]
print(len(mvalid))