Skip to main content
edited body
Source Link
Ajax1234
  • 7.7k
  • 1
  • 15
  • 28

Python3, 285 bytes

Similar to the other solutions, the code below builds haystacks by first anchoring the search on each needle present in the input.

E=enumerate
def G(x,y,d):
 q,s=[(x,y)],[(x,y)]
 for x,y in q:
  t=[u for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]if d.get(u:=(x+X,y+Y))and u not in s and'#'==d[u]]
  q+=t;s+=t
 return s
def f(b):
 d={(x,y):v for x,r in E(b)for y,v in E(r)}
 return max([G(*i,d)for i in d if'N'==d[i]],key=len)

Try it online!

Python3, 559 bytes

Out of academic curiosity, the solution below finds all possible haystacks with a needle, and then selects the largest. This approach is exactly the opposite of the code belowabove and the other answers.

E=enumerate
def f(b):
 d,P={(x,y):v for x,r in E(b)for y,v in E(r)},[]
 q=[([t:=[i for i in d if'#'==d[i]][0]],[t],[],0)]
 for s,S,g,c in q:
  if[]==s:
   if[]==(k:=[i for i in d if i not in[J for K in g+[S]for J in K]and'#'==d[i]]):P+=g+[S]
   if k:q+=[(k[:1],k[:1],g+[S],0)]
   continue
  (x,y),*s=s
  L=[[],[]]
  for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]:
   if d.get(u:=(x+X,y+Y))and u not in S:L[d[u]=='#']+=[u]
  for i in L[0]*(0==c):q+=[(s+L[1]+[i],S+L[1]+[i],g,1)]
  q+=[(s+L[1],S+L[1],g,c)]
 return max([i for i in P if any(d[j]=='N'for j in i)],key=len)

Try it online!

Python3, 285 bytes

Similar to the other solutions, the code below builds haystacks by first anchoring the search on each needle present in the input.

E=enumerate
def G(x,y,d):
 q,s=[(x,y)],[(x,y)]
 for x,y in q:
  t=[u for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]if d.get(u:=(x+X,y+Y))and u not in s and'#'==d[u]]
  q+=t;s+=t
 return s
def f(b):
 d={(x,y):v for x,r in E(b)for y,v in E(r)}
 return max([G(*i,d)for i in d if'N'==d[i]],key=len)

Try it online!

Python3, 559 bytes

Out of academic curiosity, the solution below finds all possible haystacks with a needle, and then selects the largest. This approach is exactly the opposite of the code below and the other answers.

E=enumerate
def f(b):
 d,P={(x,y):v for x,r in E(b)for y,v in E(r)},[]
 q=[([t:=[i for i in d if'#'==d[i]][0]],[t],[],0)]
 for s,S,g,c in q:
  if[]==s:
   if[]==(k:=[i for i in d if i not in[J for K in g+[S]for J in K]and'#'==d[i]]):P+=g+[S]
   if k:q+=[(k[:1],k[:1],g+[S],0)]
   continue
  (x,y),*s=s
  L=[[],[]]
  for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]:
   if d.get(u:=(x+X,y+Y))and u not in S:L[d[u]=='#']+=[u]
  for i in L[0]*(0==c):q+=[(s+L[1]+[i],S+L[1]+[i],g,1)]
  q+=[(s+L[1],S+L[1],g,c)]
 return max([i for i in P if any(d[j]=='N'for j in i)],key=len)

Try it online!

Python3, 285 bytes

Similar to the other solutions, the code below builds haystacks by first anchoring the search on each needle present in the input.

E=enumerate
def G(x,y,d):
 q,s=[(x,y)],[(x,y)]
 for x,y in q:
  t=[u for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]if d.get(u:=(x+X,y+Y))and u not in s and'#'==d[u]]
  q+=t;s+=t
 return s
def f(b):
 d={(x,y):v for x,r in E(b)for y,v in E(r)}
 return max([G(*i,d)for i in d if'N'==d[i]],key=len)

Try it online!

Python3, 559 bytes

Out of academic curiosity, the solution below finds all possible haystacks with a needle, and then selects the largest. This approach is exactly the opposite of the code above and the other answers.

E=enumerate
def f(b):
 d,P={(x,y):v for x,r in E(b)for y,v in E(r)},[]
 q=[([t:=[i for i in d if'#'==d[i]][0]],[t],[],0)]
 for s,S,g,c in q:
  if[]==s:
   if[]==(k:=[i for i in d if i not in[J for K in g+[S]for J in K]and'#'==d[i]]):P+=g+[S]
   if k:q+=[(k[:1],k[:1],g+[S],0)]
   continue
  (x,y),*s=s
  L=[[],[]]
  for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]:
   if d.get(u:=(x+X,y+Y))and u not in S:L[d[u]=='#']+=[u]
  for i in L[0]*(0==c):q+=[(s+L[1]+[i],S+L[1]+[i],g,1)]
  q+=[(s+L[1],S+L[1],g,c)]
 return max([i for i in P if any(d[j]=='N'for j in i)],key=len)

Try it online!

added 1281 characters in body
Source Link
Ajax1234
  • 7.7k
  • 1
  • 15
  • 28

Python3, 559 bytes

Python3, 285 bytes

Similar to the other solutions, the code below builds haystacks by first anchoring the search on each needle present in the input.

E=enumerate
def G(x,y,d):
 q,s=[(x,y)],[(x,y)]
 for x,y in q:
  t=[u for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]if d.get(u:=(x+X,y+Y))and u not in s and'#'==d[u]]
  q+=t;s+=t
 return s
def f(b):
 d={(x,y):v for x,r in E(b)for y,v in E(r)}
 return max([G(*i,d)for i in d if'N'==d[i]],key=len)

Try it online!

Python3, 559 bytes

Out of academic curiosity, the solution below finds all possible haystacks with a needle, and then selects the largest. This approach is exactly the opposite of the code below and the other answers.

E=enumerate
def f(b):
 d,P={(x,y):v for x,r in E(b)for y,v in E(r)},[]
 q=[([t:=[i for i in d if'#'==d[i]][0]],[t],[],0)]
 for s,S,g,c in q:
  if[]==s:
   if[]==(k:=[i for i in d if i not in[J for K in g+[S]for J in K]and'#'==d[i]]):P+=g+[S]
   if k:q+=[(k[:1],k[:1],g+[S],0)]
   continue
  (x,y),*s=s
  L=[[],[]]
  for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]:
   if d.get(u:=(x+X,y+Y))and u not in S:L[d[u]=='#']+=[u]
  for i in L[0]*(0==c):q+=[(s+L[1]+[i],S+L[1]+[i],g,1)]
  q+=[(s+L[1],S+L[1],g,c)]
 return max([i for i in P if any(d[j]=='N'for j in i)],key=len)

Try it online!Try it online!

Python3, 559 bytes

E=enumerate
def f(b):
 d,P={(x,y):v for x,r in E(b)for y,v in E(r)},[]
 q=[([t:=[i for i in d if'#'==d[i]][0]],[t],[],0)]
 for s,S,g,c in q:
  if[]==s:
   if[]==(k:=[i for i in d if i not in[J for K in g+[S]for J in K]and'#'==d[i]]):P+=g+[S]
   if k:q+=[(k[:1],k[:1],g+[S],0)]
   continue
  (x,y),*s=s
  L=[[],[]]
  for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]:
   if d.get(u:=(x+X,y+Y))and u not in S:L[d[u]=='#']+=[u]
  for i in L[0]*(0==c):q+=[(s+L[1]+[i],S+L[1]+[i],g,1)]
  q+=[(s+L[1],S+L[1],g,c)]
 return max([i for i in P if any(d[j]=='N'for j in i)],key=len)

Try it online!

Python3, 285 bytes

Similar to the other solutions, the code below builds haystacks by first anchoring the search on each needle present in the input.

E=enumerate
def G(x,y,d):
 q,s=[(x,y)],[(x,y)]
 for x,y in q:
  t=[u for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]if d.get(u:=(x+X,y+Y))and u not in s and'#'==d[u]]
  q+=t;s+=t
 return s
def f(b):
 d={(x,y):v for x,r in E(b)for y,v in E(r)}
 return max([G(*i,d)for i in d if'N'==d[i]],key=len)

Try it online!

Python3, 559 bytes

Out of academic curiosity, the solution below finds all possible haystacks with a needle, and then selects the largest. This approach is exactly the opposite of the code below and the other answers.

E=enumerate
def f(b):
 d,P={(x,y):v for x,r in E(b)for y,v in E(r)},[]
 q=[([t:=[i for i in d if'#'==d[i]][0]],[t],[],0)]
 for s,S,g,c in q:
  if[]==s:
   if[]==(k:=[i for i in d if i not in[J for K in g+[S]for J in K]and'#'==d[i]]):P+=g+[S]
   if k:q+=[(k[:1],k[:1],g+[S],0)]
   continue
  (x,y),*s=s
  L=[[],[]]
  for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]:
   if d.get(u:=(x+X,y+Y))and u not in S:L[d[u]=='#']+=[u]
  for i in L[0]*(0==c):q+=[(s+L[1]+[i],S+L[1]+[i],g,1)]
  q+=[(s+L[1],S+L[1],g,c)]
 return max([i for i in P if any(d[j]=='N'for j in i)],key=len)

Try it online!

Source Link
Ajax1234
  • 7.7k
  • 1
  • 15
  • 28

Python3, 559 bytes

E=enumerate
def f(b):
 d,P={(x,y):v for x,r in E(b)for y,v in E(r)},[]
 q=[([t:=[i for i in d if'#'==d[i]][0]],[t],[],0)]
 for s,S,g,c in q:
  if[]==s:
   if[]==(k:=[i for i in d if i not in[J for K in g+[S]for J in K]and'#'==d[i]]):P+=g+[S]
   if k:q+=[(k[:1],k[:1],g+[S],0)]
   continue
  (x,y),*s=s
  L=[[],[]]
  for X,Y in[(1,0),(-1,0),(0,1),(0,-1)]:
   if d.get(u:=(x+X,y+Y))and u not in S:L[d[u]=='#']+=[u]
  for i in L[0]*(0==c):q+=[(s+L[1]+[i],S+L[1]+[i],g,1)]
  q+=[(s+L[1],S+L[1],g,c)]
 return max([i for i in P if any(d[j]=='N'for j in i)],key=len)

Try it online!