Skip to main content
added 116 characters in body
Source Link
Cristian Lupascu
  • 8.7k
  • 2
  • 25
  • 52

Haskell, 207207 202 bytes

5 bytes saved thanks to BMO.

l=length
x!p|(_,i)<p|i<-lasth p,d<-x!!i=[p++[x]|x<-[(-d,i-d),(d,i+d)],x`notElem`p]
x?p|(_,i)<p|i<-lasth p=i<0||i>=l x
h=snd.last
x#[]=[]
x#p|x#p|l(l r<1x%p)=x#<1=x#(p>>=(x!))|1<2=r where r=filter|1>0=x%p
(x%)=filter.(?)p
f x=(tail.map fst)<$>x#[[(0,l x`div`2)]]

Try it online!Try it online!

This is a function that takes a list of Int as a parameter and returns a list of paths where each path is a list of relative jumps taken to get out of the array.

The ungolfed version:

move :: [Int] -> [(Int, Int)] -> [Path]
move xs path = map(\x->path++[x]) $ filter (\s -> s`notElem`path) $ [(-delta, i-delta), (delta, i+delta)]
  where (_,i) = last path
        delta = xs!!i :: Int

outside :: [Int] -> Path -> Bool
outside xs paths = i < 0 || i >= length xs
  where (_,i) = last paths

shortest' :: [Path] -> [Int] -> [Path]
shortest' paths xs | null paths       = []
                   | not (null ready) = ready
                   | otherwise        = shortest' paths' xs
                   where ready  = filter (outside xs) paths
                         paths' = concatMap (move xs) paths

shortest xs = map tail $ map (map fst) $ shortest' [[(0,length xs`div`2)]] xs

Haskell, 207 bytes

l=length
x!p|(_,i)<-last p,d<-x!!i=[p++[x]|x<-[(-d,i-d),(d,i+d)],x`notElem`p]
x?p|(_,i)<-last p=i<0||i>=l x
x#[]=[]
x#p|(l r<1)=x#(p>>=(x!))|1<2=r where r=filter(x?)p
f x=(tail.map fst)<$>x#[[(0,l x`div`2)]]

Try it online!

This is a function that takes a list of Int as a parameter and returns a list of paths where each path is a list of relative jumps taken to get out of the array.

The ungolfed version:

move :: [Int] -> [(Int, Int)] -> [Path]
move xs path = map(\x->path++[x]) $ filter (\s -> s`notElem`path) $ [(-delta, i-delta), (delta, i+delta)]
  where (_,i) = last path
        delta = xs!!i :: Int

outside :: [Int] -> Path -> Bool
outside xs paths = i < 0 || i >= length xs
  where (_,i) = last paths

shortest' :: [Path] -> [Int] -> [Path]
shortest' paths xs | null paths       = []
                   | not (null ready) = ready
                   | otherwise        = shortest' paths' xs
                   where ready  = filter (outside xs) paths
                         paths' = concatMap (move xs) paths

shortest xs = map tail $ map (map fst) $ shortest' [[(0,length xs`div`2)]] xs

Haskell, 207 202 bytes

5 bytes saved thanks to BMO.

l=length
x!p|i<-h p,d<-x!!i=[p++[x]|x<-[(-d,i-d),(d,i+d)],x`notElem`p]
x?p|i<-h p=i<0||i>=l x
h=snd.last
x#[]=[]
x#p|l(x%p)<1=x#(p>>=(x!))|1>0=x%p
(%)=filter.(?)
f x=(tail.map fst)<$>x#[[(0,l x`div`2)]]

Try it online!

This is a function that takes a list of Int as a parameter and returns a list of paths where each path is a list of relative jumps taken to get out of the array.

The ungolfed version:

move :: [Int] -> [(Int, Int)] -> [Path]
move xs path = map(\x->path++[x]) $ filter (\s -> s`notElem`path) $ [(-delta, i-delta), (delta, i+delta)]
  where (_,i) = last path
        delta = xs!!i :: Int

outside :: [Int] -> Path -> Bool
outside xs paths = i < 0 || i >= length xs
  where (_,i) = last paths

shortest' :: [Path] -> [Int] -> [Path]
shortest' paths xs | null paths       = []
                   | not (null ready) = ready
                   | otherwise        = shortest' paths' xs
                   where ready  = filter (outside xs) paths
                         paths' = concatMap (move xs) paths

shortest xs = map tail $ map (map fst) $ shortest' [[(0,length xs`div`2)]] xs
Source Link
Cristian Lupascu
  • 8.7k
  • 2
  • 25
  • 52

Haskell, 207 bytes

l=length
x!p|(_,i)<-last p,d<-x!!i=[p++[x]|x<-[(-d,i-d),(d,i+d)],x`notElem`p]
x?p|(_,i)<-last p=i<0||i>=l x
x#[]=[]
x#p|(l r<1)=x#(p>>=(x!))|1<2=r where r=filter(x?)p
f x=(tail.map fst)<$>x#[[(0,l x`div`2)]]

Try it online!

This is a function that takes a list of Int as a parameter and returns a list of paths where each path is a list of relative jumps taken to get out of the array.

The ungolfed version:

move :: [Int] -> [(Int, Int)] -> [Path]
move xs path = map(\x->path++[x]) $ filter (\s -> s`notElem`path) $ [(-delta, i-delta), (delta, i+delta)]
  where (_,i) = last path
        delta = xs!!i :: Int

outside :: [Int] -> Path -> Bool
outside xs paths = i < 0 || i >= length xs
  where (_,i) = last paths

shortest' :: [Path] -> [Int] -> [Path]
shortest' paths xs | null paths       = []
                   | not (null ready) = ready
                   | otherwise        = shortest' paths' xs
                   where ready  = filter (outside xs) paths
                         paths' = concatMap (move xs) paths

shortest xs = map tail $ map (map fst) $ shortest' [[(0,length xs`div`2)]] xs