cleanup
This commit is contained in:
parent
254aaef3cd
commit
a8403b76f2
|
@ -1,7 +1,6 @@
|
|||
module Main where
|
||||
|
||||
import Data.List
|
||||
import Debug.Trace
|
||||
import qualified Data.Map as M
|
||||
|
||||
mkAdjList :: Int -> Int -> AdjList
|
||||
|
|
|
@ -1,65 +0,0 @@
|
|||
module Main where
|
||||
|
||||
import Data.List
|
||||
import Debug.Trace
|
||||
import qualified Data.Map as M
|
||||
|
||||
mkAdjList :: Int -> Int -> AdjList
|
||||
mkAdjList w h = M.fromList $
|
||||
map (\p -> (,) p (mkDeltas w h p)) $
|
||||
[ (x,y) | x <- xr, y <- yr ]
|
||||
where
|
||||
xr = [0..(w-1)]
|
||||
yr = [0..(h-1)]
|
||||
|
||||
mkValList :: [[Int]] -> ValList
|
||||
mkValList [] = M.empty
|
||||
mkValList rows = M.fromList $ concat $ map f $ zip [0..] rows
|
||||
where
|
||||
f (i,v) = map (g i) $ zip [0..] v
|
||||
g i (j,v) = ((j,i), v)
|
||||
|
||||
mkDeltas :: Int -> Int -> (Int,Int) -> [(Int,Int)]
|
||||
mkDeltas w h (x,y) = filter
|
||||
(\(i,j) -> (i >= 0 && i < w && j >= 0 && j < h))
|
||||
[ ((x-1),y)
|
||||
, ((x+1),y)
|
||||
, (x,(y-1))
|
||||
, (x,(y+1))
|
||||
]
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
raw <- getContents
|
||||
let input = map (map (read . (:[]))) $ lines raw
|
||||
al = mkAdjList (length (head input)) (length input)
|
||||
vl = mkValList input
|
||||
in do
|
||||
putStrLn $ "day8a: " ++ (show $ solveA al vl)
|
||||
putStrLn $ "day8b: " ++ (show $ solveB al vl)
|
||||
|
||||
type AdjList = M.Map (Int,Int) [(Int,Int)]
|
||||
type ValList = M.Map (Int,Int) Int
|
||||
|
||||
solveA :: AdjList -> ValList -> Int
|
||||
solveA al vl = sum $ map (1 +) $ (findVals vl) (findMinima al vl)
|
||||
|
||||
solveB :: AdjList -> ValList -> Int
|
||||
solveB al vl = product $ take 3 $ (reverse . sort) $ map (length . nub) $
|
||||
map (findConnected al vl) $ (findMinima al vl)
|
||||
|
||||
findConnected :: AdjList -> ValList -> (Int,Int) -> [(Int,Int)]
|
||||
findConnected al vl p = (:) p $ concat (map (findConnected al vl) $ (filter f (al M.! p)))
|
||||
where
|
||||
f q = (vl M.! p) < (vl M.! q) && (vl M.! q) /= 9
|
||||
|
||||
mlookup :: Ord k => M.Map k a -> k -> Maybe a
|
||||
mlookup = flip M.lookup
|
||||
|
||||
findMinima :: AdjList -> ValList -> [(Int,Int)]
|
||||
findMinima al vl = filter f $ M.keys vl
|
||||
where
|
||||
f p = all (> (vl M.! p)) $ map (vl M.!) $ al M.! p
|
||||
|
||||
findVals :: ValList -> [(Int,Int)] -> [Int]
|
||||
findVals vl cs = [ x | Just x <- map (mlookup vl) cs ]
|
Loading…
Reference in a new issue