day 12: solution that actually works
This commit is contained in:
parent
0624234733
commit
8ac2ad26bb
|
@ -5,43 +5,41 @@ type Path = ([String], [(Int, Int)])
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
input <- lines <$> readFile "testinput"
|
input <- lines <$> readFile "input"
|
||||||
let s = findLetter 'S' input
|
let s = findLetter 'S' input
|
||||||
print $ walkTo 'E' (input, []) s
|
let e = findLetter 'E' input
|
||||||
|
print $ step e (input, []) [s]
|
||||||
|
|
||||||
findLetter :: Char -> [String] -> (Int, Int)
|
findLetter :: Char -> [String] -> (Int, Int)
|
||||||
findLetter c = f <*> fromJust . findIndex (/= Nothing) <$> map (elemIndex c)
|
findLetter c = f <*> fromJust . findIndex (/= Nothing) <$> map (elemIndex c)
|
||||||
where
|
where
|
||||||
f x y = (head $ catMaybes x, y)
|
f x y = (head $ catMaybes x, y)
|
||||||
|
|
||||||
walkTo :: Char -> Path -> (Int, Int) -> Int
|
step :: (Int,Int)->Path -> [(Int, Int)] -> Int
|
||||||
walkTo c (field, visited) (x, y)
|
step c (field, visited) active
|
||||||
| field !! y !! x == c = 0
|
| c `elem` active = 0
|
||||||
| null newCoords = (maxBound :: Int) `div` 2
|
| otherwise = 1 + step c (field, active ++ visited) next
|
||||||
| otherwise =
|
where next = nub $ sort $ concatMap (nextSteps (field, visited)) active
|
||||||
if length visited > lenX * lenY
|
|
||||||
then (maxBound :: Int) `div` 2
|
nextSteps :: Path -> (Int, Int) -> [(Int, Int)]
|
||||||
else succ $
|
nextSteps (field, visited) (x, y) =
|
||||||
minimum $
|
[ (x + x', y + y')
|
||||||
map (walkTo c (field, (x, y) : visited)) newCoords
|
| x' <- [-1 .. 1]
|
||||||
|
, x' + x >= 0
|
||||||
|
, x' + x < lenX
|
||||||
|
, y' <- [-1 .. 1]
|
||||||
|
, y' + y >= 0
|
||||||
|
, y + y' < lenY
|
||||||
|
, abs x' /= abs y'
|
||||||
|
, let a = field !! (y + y') !! (x + x')
|
||||||
|
in (a `elem` ['a' .. succ current] || (a == 'E' && current == 'z'))
|
||||||
|
, (x + x', y + y') `notElem` visited
|
||||||
|
]
|
||||||
where
|
where
|
||||||
lenX = length $ head field
|
lenX = length $ head field
|
||||||
lenY = length field
|
lenY = length field
|
||||||
char = field !! y !! x
|
char = field !! y !! x
|
||||||
current =
|
current
|
||||||
if char == 'S'
|
| char == 'S' = 'a'
|
||||||
then pred 'a'
|
| char == 'E' = 'z'
|
||||||
else char
|
| otherwise = char
|
||||||
newCoords =
|
|
||||||
[ (x + x', y + y')
|
|
||||||
| x' <- [-1 .. 1]
|
|
||||||
, x' + x >= 0
|
|
||||||
, x' + x < lenX
|
|
||||||
, y' <- [-1 .. 1]
|
|
||||||
, y' + y >= 0
|
|
||||||
, y + y' < lenY
|
|
||||||
, abs x' /= abs y'
|
|
||||||
, let a = field !! (y + y') !! (x + x')
|
|
||||||
in (a `elem` ['a' .. succ current] || (a == 'E' && current == 'z'))
|
|
||||||
, (x + x', y + y') `notElem` visited
|
|
||||||
]
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user