day 12 part 2: invert the search
This commit is contained in:
parent
afa4f955ef
commit
6d5f5c7910
|
|
@ -17,7 +17,7 @@ bfs heightmap ((q@(x,y), dist):queue) visited = if usable
|
|||
where
|
||||
current = heightmap A.! q
|
||||
inRange (x,y) = let (bx, by) = snd (A.bounds heightmap) in x >= 0 && x <= bx && y >= 0 && y <= by
|
||||
reachable coord = inRange coord && (heightmap A.! coord) <= current + 1
|
||||
reachable coord = inRange coord && (heightmap A.! coord) >= current - 1
|
||||
neighs = filter reachable [(x+1, y), (x-1, y), (x, y+1), (x, y-1)]
|
||||
usable = case M.lookup q visited of
|
||||
Nothing -> True
|
||||
|
|
@ -36,7 +36,7 @@ findIndex2 f xss = listToMaybe xs2 where
|
|||
xs2 = mapMaybe (uncurry augment) xs
|
||||
augment a (Just b) = Just (a,b)
|
||||
augment a Nothing = Nothing
|
||||
|
||||
|
||||
main = do
|
||||
m <- lines <$> getContents
|
||||
|
||||
|
|
@ -44,11 +44,13 @@ main = do
|
|||
let target = fromJust $ findIndex2 (=='E') m
|
||||
|
||||
let heightmap = A.listArray ((0,0), (length m - 1, length (head m) - 1)) (concatMap (map toHeight) m)
|
||||
print heightmap
|
||||
|
||||
let final = bfs heightmap [(start, 0)] M.empty
|
||||
let final = bfs heightmap [(target, 0)] M.empty
|
||||
|
||||
print $ final M.! target
|
||||
putStr "part 1: "
|
||||
print $ final M.! start
|
||||
|
||||
|
||||
pure ()
|
||||
putStr "part 2: "
|
||||
let starts = map fst $ filter (\x -> snd x == toHeight 'a') (A.assocs heightmap)
|
||||
let dists = mapMaybe (`M.lookup` final) starts
|
||||
print $ minimum dists
|
||||
|
|
|
|||
Loading…
Reference in New Issue