day 12 part 2
This commit is contained in:
parent
8ac2ad26bb
commit
369e70d38e
|
@ -8,21 +8,24 @@ main = do
|
|||
input <- lines <$> readFile "input"
|
||||
let s = findLetter 'S' input
|
||||
let e = findLetter 'E' input
|
||||
print $ step e (input, []) [s]
|
||||
print $ step 'E' (input, []) [s]
|
||||
print $ step 'a' (input, []) [e]
|
||||
|
||||
findLetter :: Char -> [String] -> (Int, Int)
|
||||
findLetter c = f <*> fromJust . findIndex (/= Nothing) <$> map (elemIndex c)
|
||||
where
|
||||
f x y = (head $ catMaybes x, y)
|
||||
|
||||
step :: (Int,Int)->Path -> [(Int, Int)] -> Int
|
||||
step :: Char -> Path -> [(Int, Int)] -> Int
|
||||
step c (field, visited) active
|
||||
| c `elem` active = 0
|
||||
| c `elem` (map f active) = 0
|
||||
| otherwise = 1 + step c (field, active ++ visited) next
|
||||
where next = nub $ sort $ concatMap (nextSteps (field, visited)) active
|
||||
where
|
||||
next = nub $ sort $ concatMap (nextSteps (c == 'E') (field, visited)) active
|
||||
f x = field !! snd x !! fst x
|
||||
|
||||
nextSteps :: Path -> (Int, Int) -> [(Int, Int)]
|
||||
nextSteps (field, visited) (x, y) =
|
||||
nextSteps :: Bool -> Path -> (Int, Int) -> [(Int, Int)]
|
||||
nextSteps up (field, visited) (x, y) =
|
||||
[ (x + x', y + y')
|
||||
| x' <- [-1 .. 1]
|
||||
, x' + x >= 0
|
||||
|
@ -32,7 +35,7 @@ nextSteps (field, visited) (x, y) =
|
|||
, y + y' < lenY
|
||||
, abs x' /= abs y'
|
||||
, let a = field !! (y + y') !! (x + x')
|
||||
in (a `elem` ['a' .. succ current] || (a == 'E' && current == 'z'))
|
||||
in (a `elem` reachables || (a == 'E' && current == 'z'))
|
||||
, (x + x', y + y') `notElem` visited
|
||||
]
|
||||
where
|
||||
|
@ -43,3 +46,7 @@ nextSteps (field, visited) (x, y) =
|
|||
| char == 'S' = 'a'
|
||||
| char == 'E' = 'z'
|
||||
| otherwise = char
|
||||
reachables =
|
||||
if up
|
||||
then ['a' .. succ current]
|
||||
else [pred current .. 'z']
|
||||
|
|
Loading…
Reference in New Issue
Block a user