Skip to main content
added 28 characters in body
Source Link
flawr
  • 43.9k
  • 7
  • 104
  • 249

Haskell, 212212 207 bytes

This is probably way too long, but it finally works now. (Thanks to @Lynn for the cartesian product trick!) Thansk @nimi for -5 bytes!

import Data.List
b%l=[l++[x|b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v]]|x<-mapM id[[1>0,1<0]|_<-b]]id$[0>1..]<$b]
b!a|f<-nub.concat.((b%)<$>)=snd$maximum$map(length>>=(,))$filter((==b).last)$until(f>>=(==))f[[a]]

Try it online!Try it online!

b%l -- helper function:
    -- given a path l (that should end in b) this generates all possible extensions
    -- of l (if not possible also l itself) 
            x<-mapM id[[1>0,1<0]|_<-b]id$[0>1..]<$b -- generate all possible vertices of the hypercube
             -- and check the criteria
           b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v] 
             -- extend if possible
    [l++[x|  ...                                                   ]| ... ]
b!a| -- actual function: 
     -- first define a helper function:
    f<-nub.concat.((b%)<$>)
     -- begin with the vertex a and apply the function from above repeatedly
     -- until you cannot make the path any longer without violating the
     -- criteria 
                                                                             until(f>>=(==))f[[a]]
     -- only take the paths that actually end in b          
                                                          filter((==b).last)$
     -- and find the one with the maximum length    
                           =snd$maximum$map(length>>=(,))$    

Haskell, 212 bytes

This is probably way too long, but it finally works now. (Thanks to @Lynn for the cartesian product trick!)

import Data.List
b%l=[l++[x|b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v]]|x<-mapM id[[1>0,1<0]|_<-b]]
b!a|f<-nub.concat.((b%)<$>)=snd$maximum$map(length>>=(,))$filter((==b).last)$until(f>>=(==))f[[a]]

Try it online!

b%l -- helper function:
    -- given a path l (that should end in b) this generates all possible extensions
    -- of l (if not possible also l itself) 
            mapM id[[1>0,1<0]|_<-b] -- generate all possible vertices of the hypercube
             -- and check the criteria
           b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v] 
             -- extend if possible
    [l++[x|  ...                                                   ]| ... ]
b!a| -- actual function: 
     -- first define a helper function:
    f<-nub.concat.((b%)<$>)
     -- begin with the vertex a and apply the function from above repeatedly
     -- until you cannot make the path any longer without violating the
     -- criteria 
                                                                             until(f>>=(==))f[[a]]
     -- only take the paths that actually end in b          
                                                          filter((==b).last)$
     -- and find the one with the maximum length    
                           =snd$maximum$map(length>>=(,))$    

Haskell, 212 207 bytes

This is probably way too long, but it finally works now. (Thanks to @Lynn for the cartesian product trick!) Thansk @nimi for -5 bytes!

import Data.List
b%l=[l++[x|b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v]]|x<-mapM id$[0>1..]<$b]
b!a|f<-nub.concat.((b%)<$>)=snd$maximum$map(length>>=(,))$filter((==b).last)$until(f>>=(==))f[[a]]

Try it online!

b%l -- helper function:
    -- given a path l (that should end in b) this generates all possible extensions
    -- of l (if not possible also l itself) 
            x<-mapM id$[0>1..]<$b -- generate all possible vertices of the hypercube
             -- and check the criteria
           b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v] 
             -- extend if possible
    [l++[x|  ...                                                   ]| ... ]
b!a| -- actual function: 
     -- first define a helper function:
    f<-nub.concat.((b%)<$>)
     -- begin with the vertex a and apply the function from above repeatedly
     -- until you cannot make the path any longer without violating the
     -- criteria 
                                                                             until(f>>=(==))f[[a]]
     -- only take the paths that actually end in b          
                                                          filter((==b).last)$
     -- and find the one with the maximum length    
                           =snd$maximum$map(length>>=(,))$    
added 1134 characters in body
Source Link
flawr
  • 43.9k
  • 7
  • 104
  • 249

Haskell, 212 bytes

This is probably way too long, but it finally works now. (Thanks to @Lynn for the cartesian product trick!)

import Data.List
b%l=[l++[x|b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v]]|x<-mapM id[[1>0,1<0]|_<-b]]
b!a|f<-nub.concat.((b%)<$>)=snd$maximum$map(length>>=(,))$filter((==b).last)$until(f>>=(==))f[[a]]

Try it online!

Explanation:

b%l -- helper function:
    -- given a path l (that should end in b) this generates all possible extensions
    -- of l (if not possible also l itself) 
            mapM id[[1>0,1<0]|_<-b] -- generate all possible vertices of the hypercube
             -- and check the criteria
           b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v] 
             -- extend if possible
    [l++[x|  ...                                                   ]| ... ]
b!a| -- actual function: 
     -- first define a helper function:
    f<-nub.concat.((b%)<$>)
     -- begin with the vertex a and apply the function from above repeatedly
     -- until you cannot make the path any longer without violating the
     -- criteria 
                                                                             until(f>>=(==))f[[a]]
     -- only take the paths that actually end in b          
                                                          filter((==b).last)$
     -- and find the one with the maximum length    
                           =snd$maximum$map(length>>=(,))$    

Haskell, 212 bytes

This is probably way too long, but it finally works now. (Thanks to @Lynn for the cartesian product trick!)

import Data.List
b%l=[l++[x|b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v]]|x<-mapM id[[1>0,1<0]|_<-b]]
b!a|f<-nub.concat.((b%)<$>)=snd$maximum$map(length>>=(,))$filter((==b).last)$until(f>>=(==))f[[a]]

Try it online!

Haskell, 212 bytes

This is probably way too long, but it finally works now. (Thanks to @Lynn for the cartesian product trick!)

import Data.List
b%l=[l++[x|b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v]]|x<-mapM id[[1>0,1<0]|_<-b]]
b!a|f<-nub.concat.((b%)<$>)=snd$maximum$map(length>>=(,))$filter((==b).last)$until(f>>=(==))f[[a]]

Try it online!

Explanation:

b%l -- helper function:
    -- given a path l (that should end in b) this generates all possible extensions
    -- of l (if not possible also l itself) 
            mapM id[[1>0,1<0]|_<-b] -- generate all possible vertices of the hypercube
             -- and check the criteria
           b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v] 
             -- extend if possible
    [l++[x|  ...                                                   ]| ... ]
b!a| -- actual function: 
     -- first define a helper function:
    f<-nub.concat.((b%)<$>)
     -- begin with the vertex a and apply the function from above repeatedly
     -- until you cannot make the path any longer without violating the
     -- criteria 
                                                                             until(f>>=(==))f[[a]]
     -- only take the paths that actually end in b          
                                                          filter((==b).last)$
     -- and find the one with the maximum length    
                           =snd$maximum$map(length>>=(,))$    
edited body
Source Link
flawr
  • 43.9k
  • 7
  • 104
  • 249

Haskell, 212 bytes

This is probably way too long, but it finally works now finally. (Thanks to @Lynn for the cartesian product trick!)

import Data.List
b%l=[l++[x|b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v]]|x<-mapM id[[1>0,1<0]|_<-b]]
b!a|f<-nub.concat.((b%)<$>)=snd$maximum$map(length>>=(,))$filter((==b).last)$until(f>>=(==))f[[a]]

Try it online!

Haskell, 212 bytes

This is probably way too long, but it works now finally. (Thanks to @Lynn for the cartesian product trick!)

import Data.List
b%l=[l++[x|b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v]]|x<-mapM id[[1>0,1<0]|_<-b]]
b!a|f<-nub.concat.((b%)<$>)=snd$maximum$map(length>>=(,))$filter((==b).last)$until(f>>=(==))f[[a]]

Try it online!

Haskell, 212 bytes

This is probably way too long, but it finally works now. (Thanks to @Lynn for the cartesian product trick!)

import Data.List
b%l=[l++[x|b/=last l,x`notElem`l,1==sum[1|(u,v)<-x`zip`last l,u/=v]]|x<-mapM id[[1>0,1<0]|_<-b]]
b!a|f<-nub.concat.((b%)<$>)=snd$maximum$map(length>>=(,))$filter((==b).last)$until(f>>=(==))f[[a]]

Try it online!

Post Undeleted by flawr
deleted 2 characters in body
Source Link
flawr
  • 43.9k
  • 7
  • 104
  • 249
Loading
Post Deleted by flawr
Post Undeleted by flawr
deleted 728 characters in body
Source Link
flawr
  • 43.9k
  • 7
  • 104
  • 249
Loading
added 122 characters in body
Source Link
flawr
  • 43.9k
  • 7
  • 104
  • 249
Loading
Post Deleted by flawr
added 642 characters in body
Source Link
flawr
  • 43.9k
  • 7
  • 104
  • 249
Loading
Source Link
flawr
  • 43.9k
  • 7
  • 104
  • 249
Loading