-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaoc12.hs
40 lines (36 loc) · 1.47 KB
/
aoc12.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{-# LANGUAGE TupleSections #-}
import System.Environment (getArgs)
import Dijkstra (dijkstra)
neighbors :: Bool -> [String] -> (Int, Int) -> [(Int, Int)]
neighbors isPart1 grid (row, col) =
let allposs = [(row - 1, col), (row, col - 1), (row + 1, col), (row, col + 1)]
allpos' = filter ((>= 0) . snd) $ filter ((>= 0) . fst) allposs
allpos'' = filter ((< length grid) . fst) $ filter ((< (length $ head grid)) . snd) allpos'
toelev 'S' = 'a'
toelev 'E' = 'z'
toelev x = x
celev (r, c) = toelev ((grid !! r) !! c)
nowelev = celev (row, col)
in if isPart1
then [p | p <- allpos'', celev p <= succ nowelev]
else [p | p <- allpos'', celev p >= pred nowelev]
main :: IO ()
main = do
args <- getArgs
let filename =
if null args
then "aoc12.in"
else head args
s <- lines <$> readFile filename
let getSpot (r, c) = (s !! r) !! c
let startrow = head $ filter (\r -> 'S' `elem` (s !! r)) [0 .. (length s - 1)]
let startcol = head $ filter (\c -> 'S' == getSpot (startrow, c)) [0 .. (length (head s) - 1)]
let addOneInt =
let one = 1 :: Int
in map (, one)
let Just (final, dist) =
dijkstra (addOneInt . neighbors True s) (startrow, startcol) (\p -> 'E' == getSpot p)
print dist
let Just (_, dist') =
dijkstra (addOneInt . neighbors False s) final (\p -> getSpot p `elem` "aS")
print dist'