Skip to main content
Improve answer and simplify explanation
Source Link
faubi
  • 2.6k
  • 1
  • 13
  • 11

Haskell - 481 405405 387 bytes

import Data.List
s&t=elemIndices s t
l=last
b a=(([id,reverse]<*>[a]>>=).)
c!(x:y:z)=l$(y:c)!(x:z):((!)[].(do{[x:c++z)<$>b xp,q]<-mapM(b y[id,reverse]<*>)[[x],[y]];x&[l q];[[]!(\(x:p)q->[q++p]<*x&[l q]q++p):c++z)]}
c!(x:[])=x[x]=x:[]!c
c!z=z
main=interact(\m->let{g=' '&m;
u=(\\[k|k<-g,length(k&concat vv>>=(k&))==2])<$>[]!v;
v=[[x,y]|x<-g,y<-g,elem(y-x-1)[0,head$'\n'&m]];
}in '|':(u>>=(++"|").init.(>>=(:" ").toEnum.((+)<*>(+65).(*32).(`div`26)).l.(-1:).(&(nub$u>>=init.tail)))))

This gets thecreates a list of spaces that are in the maze (numbered, numbered by index in the string), and uses it to find all the pairs of adjacent spaces. It then stitches the pairs together into longer sequences of points based on matching first/last elements and removes the ones corresponding to corridors, so that each sequence is one room in the 1D maze. A pairing between letters andThe sequences are then translated into a string by replacing points on the interior of these roomsat least one room (the warp points) is createdinto corresponding letters and used to translate the lists of numbersrest into a string, which is the outputspaces.

The 2D maze is read from STDIN and the 1D maze is printed to STDOUT.

Edit: Reduced by 62 bytes rearranging a bunch of stuff and modifying the algorithm a bit, and another 14 by replacing chr with toEnum as suggested by Laikoni.

Edit 2: Saved 13 more bytes by simplifying the logic in (!), 3 by using the list pattern match sugar, and 2 by using >>= to concat in u.

Haskell - 481 405 bytes

import Data.List
s&t=elemIndices s t
l=last
b a=(([id,reverse]<*>[a]>>=).)
c!(x:y:z)=l$(y:c)!(x:z):((!)[].(:c++z)<$>b x(b y)(\(x:p)q->[q++p]<*x&[l q]))
c!(x:[])=x:[]!c
c!z=z
main=interact(\m->let{g=' '&m;
u=(\\[k|k<-g,length(k&concat v)==2])<$>[]!v;
v=[[x,y]|x<-g,y<-g,elem(y-x-1)[0,head$'\n'&m]];
}in '|':(u>>=(++"|").init.(>>=(:" ").toEnum.((+)<*>(+65).(*32).(`div`26)).l.(-1:).(&(nub$u>>=init.tail)))))

This gets the list of spaces that are in the maze (numbered by index in the string) uses it to find all the pairs of adjacent spaces. It then stitches the pairs together into longer sequences of points based on matching first/last elements and removes the ones corresponding to corridors so that each sequence is one room in the 1D maze. A pairing between letters and points on the interior of these rooms (the warp points) is created and used to translate the lists of numbers into a string, which is the output.

The 2D maze is read from STDIN and the 1D maze is printed to STDOUT.

Edit: Reduced by 62 bytes rearranging a bunch of stuff and modifying the algorithm a bit, and another 14 by replacing chr with toEnum as suggested by Laikoni.

Haskell - 481 405 387 bytes

import Data.List
s&t=elemIndices s t
l=last
c!(x:y:z)=l$(y:c)!(x:z):do{[x:p,q]<-mapM([id,reverse]<*>)[[x],[y]];x&[l q];[[]!((q++p):c++z)]}
c![x]=x:[]!c
c!z=z
main=interact(\m->let{g=' '&m;
u=(\\[k|k<-g,length(v>>=(k&))==2])<$>[]!v;
v=[[x,y]|x<-g,y<-g,elem(y-x-1)[0,head$'\n'&m]];
}in '|':(u>>=(++"|").init.(>>=(:" ").toEnum.((+)<*>(+65).(*32).(`div`26)).l.(-1:).(&(nub$u>>=init.tail)))))

This creates a list of spaces that are in the maze, numbered by index in the string, and uses it to find all the pairs of adjacent spaces. It then stitches the pairs together into longer sequences of points based on matching first/last elements and removes the corridors, so that each sequence is one room in the 1D maze. The sequences are then translated into a string by replacing points on the interior of at least one room (the warp points) into corresponding letters and the rest into spaces.

The 2D maze is read from STDIN and the 1D maze is printed to STDOUT.

Edit: Reduced by 62 bytes rearranging a bunch of stuff and modifying the algorithm a bit, and another 14 by replacing chr with toEnum as suggested by Laikoni.

Edit 2: Saved 13 more bytes by simplifying the logic in (!), 3 by using the list pattern match sugar, and 2 by using >>= to concat in u.

added 2 characters in body
Source Link
faubi
  • 2.6k
  • 1
  • 13
  • 11

Haskell - 481481 405 bytes

import Data.List
import Data.Char
e=elemIndices
o(x:p)q
s&t=elemIndices |x==lasts q=[q++p]t
 |1>0=[]l=last
b a=(([id,reverse]<*>[a]>>=).)
c!(x:y:z)=last$=l$(y:c)!(x:z):((!)[].(:c++z)<$>(b<$>b x)(b y)o(\(x:p)q->[q++p]<*x&[l q]))
c!(x:[])=x:[]!c
c!z=z
main=interact(\m->let{n:_=e '\n'm;
g=e 'g=' 'm;'&m;
j k=[k+d|d<u=(\\[k|k<-[1g,n+1length(k&concat v)==2])<$>[]!v;
v=[[x,y]|x<-1g,-ny<-1]g,elem(k+dy-x-1)g];
k#x[0,head$'\n'&m]];
}in |d'|':[]<-j k\\[x]=d#k
 |1>0=k;
u=[]!nub[sort[k,l]|k<-g,(lengthu>>=(++"|").jinit.(>>=(:" ")k>2,l<-.toEnum.(#k(+)<$>j k];
v=zip<*>(nub$u>>=init.tail+65)$[65.(*32).90]++[96(`div`26)).l.122]
}in '|':(u>>= \r->init1:).(r>>= \s->maybe ' 'chr&(lookup s vnub$u>>=init.tail):" ")++"|")))

This gets the list of spaces that are in the maze (numbered by index in the string) and walks down the corridors from each junctionuses it to create a list offind all the pairs of dead ends and junctions in the maze which are directly connected by corridorsadjacent spaces. It then stitches the pairs together into longer sequences of points based on matching first/last elements, and removes the ones corresponding to corridors so that each sequence is one room in the 1D maze. A pairing between letters and points on the interior of these rooms (the warp points) is created and used to translate the lists of numbers into a string, which is the output.

The 2D maze is read from STDIN and the 1D maze is printed to STDOUT.

Edit: Reduced by 62 bytes rearranging a bunch of stuff and modifying the algorithm a bit, and another 14 by replacing chr with toEnum as suggested by Laikoni.

Haskell - 481 bytes

import Data.List
import Data.Char
e=elemIndices
o(x:p)q
 |x==last q=[q++p]
 |1>0=[]
b a=(([id,reverse]<*>[a]>>=).)
c!(x:y:z)=last$(y:c)!(x:z):((!)[].(:c++z)<$>(b x)(b y)o)
c!(x:[])=x:[]!c
c!z=z
main=interact(\m->let{n:_=e '\n'm;
g=e ' 'm;
j k=[k+d|d<-[1,n+1,-1,-n-1],elem(k+d)g];
k#x
 |d:[]<-j k\\[x]=d#k
 |1>0=k;
u=[]!nub[sort[k,l]|k<-g,(length.j)k>2,l<-(#k)<$>j k];
v=zip(nub$u>>=init.tail)$[65..90]++[96..122]
}in '|':(u>>= \r->init(r>>= \s->maybe ' 'chr(lookup s v):" ")++"|"))

This gets the list of spaces that are in the maze (numbered by index in the string) and walks down the corridors from each junction to create a list of pairs of dead ends and junctions in the maze which are directly connected by corridors. It then stitches the pairs together into longer sequences of points based on matching first/last elements, so that each sequence is one room in the 1D maze. A pairing between letters and points on the interior of these rooms (the warp points) is created and used to translate the lists of numbers into a string, which is the output.

The 2D maze is read from STDIN and the 1D maze is printed to STDOUT.

Haskell - 481 405 bytes

import Data.List
s&t=elemIndices s t
l=last
b a=(([id,reverse]<*>[a]>>=).)
c!(x:y:z)=l$(y:c)!(x:z):((!)[].(:c++z)<$>b x(b y)(\(x:p)q->[q++p]<*x&[l q]))
c!(x:[])=x:[]!c
c!z=z
main=interact(\m->let{g=' '&m;
u=(\\[k|k<-g,length(k&concat v)==2])<$>[]!v;
v=[[x,y]|x<-g,y<-g,elem(y-x-1)[0,head$'\n'&m]];
}in '|':(u>>=(++"|").init.(>>=(:" ").toEnum.((+)<*>(+65).(*32).(`div`26)).l.(-1:).(&(nub$u>>=init.tail)))))

This gets the list of spaces that are in the maze (numbered by index in the string) uses it to find all the pairs of adjacent spaces. It then stitches the pairs together into longer sequences of points based on matching first/last elements and removes the ones corresponding to corridors so that each sequence is one room in the 1D maze. A pairing between letters and points on the interior of these rooms (the warp points) is created and used to translate the lists of numbers into a string, which is the output.

The 2D maze is read from STDIN and the 1D maze is printed to STDOUT.

Edit: Reduced by 62 bytes rearranging a bunch of stuff and modifying the algorithm a bit, and another 14 by replacing chr with toEnum as suggested by Laikoni.

added 33 characters in body
Source Link
faubi
  • 2.6k
  • 1
  • 13
  • 11

Haskell - 481 bytes

import Data.List
import Data.Char
e=elemIndices
o(x:p)q
 |x==last q=[q++p]
 |1>0=[]
b a=(([id,reverse]<*>[a]>>=).)
c!(x:y:z)=last$(y:c)!(x:z):((!)[].(:c++z)<$>(b x)(b y)o)
c!(x:[])=x:[]!c
c!z=z
main=interact(\m->let{n:_=e '\n'm;
g=e ' 'm;
j k=[k+d|d<-[1,n+1,-1,-n-1],elem(k+d)g];
k#x
 |d:[]<-j k\\[x]=d#k
 |1>0=k;
u=[]!nub[sort[k,l]|k<-g,(length.j)k>2,l<-(#k)<$>j k];
v=zip(nub$u>>=init.tail)$[65..90]++[96..122]
}in '|':(u>>= \r->init(r>>= \s->maybe ' 'chr(lookup s v):" ")++"|"))
import Data.List
import Data.Char
e=elemIndices
o(x:p)q
 |x==last q=[q++p]
 |1>0=[]
b a=(([id,reverse]<*>[a]>>=).)
c!(x:y:z)=last$(y:c)!(x:z):((!)[].(:c++z)<$>(b x)(b y)o)
c!(x:[])=x:[]!c
c!z=z
main=interact(\m->let{n:_=e '\n'm;
g=e ' 'm;
j k=[k+d|d<-[1,n+1,-1,-n-1],elem(k+d)g];
k#x
 |d:[]<-j k\\[x]=d#k
 |1>0=k;
u=[]!nub[sort[k,l]|k<-g,(length.j)k>2,l<-(#k)<$>j k];
v=zip(nub$u>>=init.tail)$[65..90]++[96..122]
}in '|':(u>>= \r->init(r>>= \s->maybe ' 'chr(lookup s v):" ")++"|"))

This gets the list of spaces that are in the maze (numbered by index in the string) and walks down the corridors from each junction to create a list of pairs of dead ends and junctions in the maze which are directly connected by corridors. It then stitches the pairs together into longer sequences of points based on matching first/last elements, so that each sequence is one room in the 1D maze. A pairing between letters and points on the interior of these rooms (the warp points) is created and used to translate the lists of numbers into a string, which is the output.

The 2D maze is read from STDIN and the 1D maze is printed to STDOUT.

Haskell - 481 bytes

import Data.List
import Data.Char
e=elemIndices
o(x:p)q
 |x==last q=[q++p]
 |1>0=[]
b a=(([id,reverse]<*>[a]>>=).)
c!(x:y:z)=last$(y:c)!(x:z):((!)[].(:c++z)<$>(b x)(b y)o)
c!(x:[])=x:[]!c
c!z=z
main=interact(\m->let{n:_=e '\n'm;
g=e ' 'm;
j k=[k+d|d<-[1,n+1,-1,-n-1],elem(k+d)g];
k#x
 |d:[]<-j k\\[x]=d#k
 |1>0=k;
u=[]!nub[sort[k,l]|k<-g,(length.j)k>2,l<-(#k)<$>j k];
v=zip(nub$u>>=init.tail)$[65..90]++[96..122]
}in '|':(u>>= \r->init(r>>= \s->maybe ' 'chr(lookup s v):" ")++"|"))

This gets the list of spaces that are in the maze (numbered by index in the string) and walks down the corridors from each junction to create a list of pairs of dead ends and junctions in the maze which are directly connected by corridors. It then stitches the pairs together into longer sequences of points based on matching first/last elements, so that each sequence is one room in the 1D maze. A pairing between letters and points on the interior of these rooms (the warp points) is created and used to translate the lists of numbers into a string, which is the output.

The 2D maze is read from STDIN and the 1D maze is printed to STDOUT.

Haskell - 481 bytes

import Data.List
import Data.Char
e=elemIndices
o(x:p)q
 |x==last q=[q++p]
 |1>0=[]
b a=(([id,reverse]<*>[a]>>=).)
c!(x:y:z)=last$(y:c)!(x:z):((!)[].(:c++z)<$>(b x)(b y)o)
c!(x:[])=x:[]!c
c!z=z
main=interact(\m->let{n:_=e '\n'm;
g=e ' 'm;
j k=[k+d|d<-[1,n+1,-1,-n-1],elem(k+d)g];
k#x
 |d:[]<-j k\\[x]=d#k
 |1>0=k;
u=[]!nub[sort[k,l]|k<-g,(length.j)k>2,l<-(#k)<$>j k];
v=zip(nub$u>>=init.tail)$[65..90]++[96..122]
}in '|':(u>>= \r->init(r>>= \s->maybe ' 'chr(lookup s v):" ")++"|"))

This gets the list of spaces that are in the maze (numbered by index in the string) and walks down the corridors from each junction to create a list of pairs of dead ends and junctions in the maze which are directly connected by corridors. It then stitches the pairs together into longer sequences of points based on matching first/last elements, so that each sequence is one room in the 1D maze. A pairing between letters and points on the interior of these rooms (the warp points) is created and used to translate the lists of numbers into a string, which is the output.

The 2D maze is read from STDIN and the 1D maze is printed to STDOUT.

Source Link
faubi
  • 2.6k
  • 1
  • 13
  • 11
Loading