#Python, 990990 943 bytes
EDIT: Golfed adjacency list making. I now use a slightly different formula
from math import*
Y=range
A=input()
B=input()
C=ceil(sqrt((len(A)-.25)/3)+.5)
D=3*C*~-C+1
E=2*C-1
F=C-1
A+='.'*(D-len(A))
F=[0]*E
G=[0]*DG=[0]*E
H=[0]*D
I=[0,I=[0]*D
J=[(1,0,-1),(0,1,-1),(-1]
J=[11,1,0),(-1,-0,1),(0,1]
K=[set(-1,1)for x in Y,(D)]
L=[D*[1,-1]for d in Y(61,0)]
M=lambdaK=lambda q,r:q+F[r]
N=C-1q+G[r+F]
O=lambdaL=lambda a,b,q,r,s:max(q+N*map(J[a%6]+J[b%6])abs,r+N*[q,r,s]))
M=[set(I[a%6]+I[b%6])for x in Y(D)]
P=0N=0
for r in Y(E-1):F[r+1]=intG[r+1]=int(F[r]+E+G[r]+E+.5-abs(r-C+1.5))
for r in Y(E):
Q=maxO=max(-r,1-C)
for q in Y(E-abs(r-C+1)):G[P]=Q+q;H[P]=r;P+=1H[N]=O+q;I[N]=r-F;N+=1
forP=[-H[x]-I[x]for dx in Y(6D):
q,r=J[~-d%6]*N,N+I[~-d%6]*N]
for x in Y(ND):R
q,S=O(d+2r,d+3s=H[x],qI[x],r);L[d][MP[x];p=F and(q/F,r)]=M(R/F,Ss/F);q+=J[-~d%6];r+=I[-~d%6]
L[d][M;m=L(q,r,s)]=-2;a=M[x].add
forif xp in Y(N)J:q+=J[(d+2)%6];r+=I[a(d+2)%6];R,S=OK(d+4,d+3s,q,));a(K(r,s);L[d][M)
elif L(q,r)]=M(R,Ss)==F:
for x inif Ym==r or-m==r:a(DK(-s,-r):)
if m==q or-m==q:a(K(-q,r=G[x]-s))
if m==s or-m==s:a(K(-r,H[x]-q))
for d in Y(6):
if-1==L[d][x]:Q,R,S=q+J[d]S=q+J[d][0],r+I[d];K[x].add(M(Rr+J[d][1],S))s+J[d][2]
elif-2==L[d][x]:pass
if elseL(Q,R,S)<C:K[x].adda(L[d][x]K(Q,R))
def T(i,U,V):
W=U[0]==A[i]or'.'==A[i];X=0
if 1==len(2>len(U)and W)*W:X=1
elif W:X=max([T(x,U[1:],V+[i])if x not in V else 0 for x in K[i]]+[0]M[i]]+[0])
return X
print(max([T(x,B,[])for x in Y(D)])and len(B)<=D)
from math import*
#Rundown of the formula:
# * Get inputdata about the size of the hexagon
prog=input('Enter# * Create lookup tables for index <-> coordinate conversion
# * I chose to measure in cubic coordinates, as that allows
# for easy oob checks
# * Create the adjacency list using the lookup tables, while
# checking for wrapping
# * Heuristically check if a path in the hexagon: ')matches the
expr=input('Enter# expression:
# 'Get input
prog=input()
expr=input()
# sdln = Side length
# hxln = Closest hexagonal number
# nmrw = Number of rows in the hexagon
# usdl = one less than the side length. I use it a lot later
sdln=ceil(sqrt((len(prog)-.25)/3)+.5)
hxln=3*sdln*~-sdln+1
nmrw=2*sdln-1
usdl=sdln-1
# Pad prog with dots
prog+='.'*(hxln-len(prog))
# nmbf = Number of elements before in each row
# in2q = index to collum
# in2r = index to row
nmbf=[0]*nmrw
in2q=[0]*hxln
in2r=[0]*hxln
# 4 5
# \ /
# 3 -- -- 0
# / \
# 2 1
# qdirdirs contains the q,r and rdirs representvalues theneeded aboveto directionsmove a point
# in axialthe form
direction refrenced by the index
rdir=[0,dirs=[(1,0,-1),(0,1,-1),(-1]
qdir=[11,1,0),(-1,-0,1),(0,1]-1,1),(1,-1,0)]
# generate nmbf using a summation formula I made
for r in range(nmrw-1):
nmbf[r+1]=int(nmbf[r]+nmrw+.5-abs(r-sdln+1.5))
# generate in2q and in2r using more formulas
# cntr = running counter
cntr=0
for r in range(nmrw):
bgnq=max(-r,1-sdln)
for q in range(nmrw-abs(r-sdln+1)):
in2q[cntr]=bgnq+q
in2r[cntr]=r-usdl
cntr+=1
# wrpnin2s = Spaces that wrap, and which ones they goindex to
# mayb = corners, turns out we don't need it
wrpn=[hxln*[-1]for d ins. range(6)]
#mayb=[[0,0]forUseful dfor inbounds range(6)]
checking
# c2in = coords to index conversion
# usdlmxab = one less thanreturn theroughly sidehow length.far Iaway usea itpoint ais lotfrom later
c2in=lambdathe q,r:q+nmbf[r]
usdl=sdln-1
center
# mkptadjn = Mark a pointAdjacency usdlsets
in2s=[-in2q[x]-in2r[x]for unitsx in direction a and b starting fromrange(hxln)]
c2in=lambda q and ,r
:q+nmbf[r+usdl]
mkpt=lambdamxab=lambda a,b,q,r,s:max(q+usdl*map(qdir[a%6]+qdir[b%6])abs,r+usdl*(rdir[a%6]+rdir[b%6]))
# The below loop generates the wrapping table.
# Basically[q, for each directionr, it goes along 2 edges
# marking the points that will wrap and the point it will wrap to
s]))
adjn=[set()for dx in range(6hxln):]
# Go to the 'forward-top'Generate (d+1)adjacency cornersets
q,r=qdir[~-d%6]*usdl,usdl+rdir[~-d%6]*usdl
for x in range(usdlhxln):
# Mark a point 'backwards' (d+3) and 'backwards-down' (d+2) on#Get the otherq,r,s edgecoords
mkdqq,mkdr=mkpt(d+2r,d+3s=in2q[x],qin2r[x],r)
in2s[x]
# Add itAre towe theat wrappinga listcorner?
p=usdl wrpn[d][c2inand(q/usdl,r)]=c2in(mkdq/usdl,mkdrs/usdl)
# Move down the current edge
m = distance from q+=qdir[-~d%6]center
r+=rdir[-~d%6]
m=mxab(q,r,s)
# Nowif we are at thea corner...
# Theif belowp codein isdirs:
unnecessary, because
# corners already# wrapadd tothe other 2 corners
we can go #mkdq,mkdr=mkpt(d+2,d+3,q,r)to
#mayb[d][0]=c2in(mkdq,mkdr)
#mkdq,mkdr=mkptadjn[x].add(d+4,d+3c2in(s,q,r))
#mayb[d][1]=c2in(mkdq,mkdr)
wrpn[d][c2inadjn[x].add(q,c2in(r,s)]=-2
)
# gootherwise, alongif thewe 'bottom'are edge
on the edge...
for x inelif rangemxab(usdlq,r,s)==usdl:
# move 'backwards-down'
q+=qdir[(d+2)%6]
add the only other point it wraps r+=rdir[(d+2)%6]
to
# mark a point on the opposite edgeif bym==r goingor-m==r:
# 'backwards' (d+3) and 'backwards-up' adjn[x].add(d+4)
mkdq,mkdr=mkptc2in(d+4,d+3,q-s,-r)
)
# addif thatm==q pairor-m==q:
to the wrapping list
wrpn[d][c2inadjn[x].add(c2in(-q,r-s)]=c2in(mkdq,mkdr)
# adjn = Adjacency sets
adjn=[set()for x in range(hxln)]
# Generate theif adjacencym==s sets
or-m==s:
for x in range(hxln):
# Get the row and columnadjn[x].add(c2in(-r,-q))
of the current index
# for all the q,r=in2q[x],in2r[x]directions...
for d in range(6):
if-1==wrpn[d][x]:
# on the insidetmp{q, don't use wrapping table
r,s} = moving in direction d from mkdqq,mkdr=q+qdir[d]r,r+rdir[d]s
adjn[x].add(c2in(mkdqtmpq,mkdr))
elif-2==wrpn[d][x]:passtmpr,tmps=q+dirs[d][0],r+dirs[d][1],s+dirs[d][2]
# is a corner
#if Alreadythe takenpoint carewe ofmoved byto wrappingis in other directions
# adjn[x]bounds.add(mayb[d][0])..
# if adjn[x].addmxab(mayb[d][1]tmpq,tmpr,tmps)
else<sdln:
# going oob, use the wrappingadd tableit
adjn[x].add(wrpn[d][x]c2in(tmpq,tmpr))
# Recursive brute-forcepath checking function
def mtch(i,mtst,past):
# dmch = Does the currentplace charwe are on in the hexagon match the
# current char the place we are in the regexexpression?
dmch=mtst[0]==prog[i]or'.'==prog[i]
# out = the out=0
value to return
if 1==len(mtst)and dmch:dmch=mtst[0]==prog[i]or'.'==prog[i]
out=0
# if we are at the last characterend, and it matches,...
return true if(2>len(mtst))*dmch:
out=1
# otherwise...
elif dmch:
# otherwise, recurRecur in all directions that we haven't visited yet
out=max([mtch(x,mtst[1:],past+[i]) if x not in past else 0 for x in adjn[i]]+[0])
return out
# Start function at all the locations in the hexagon
# Automatically return false if the expression is longer
# than the entire hexagon
print(max([mtch(x,expr,[])for x in range(hxln)])and len(expr)<=hxln)