aoc

advent of code
git clone git://source.orangerot.dev:/aoc.git
Log | Files | Refs

MainOpt.hs (638B)


      1 import Data.List.Split
      2 import Data.List
      3 
      4 toMapFn :: [[Int]] -> Int -> Int
      5 toMapFn map seed  
      6   | Just [d,s, r] <- find (\[drs, srs, r] -> (srs <= seed) && (seed <= srs + r)) map = d + (seed - s)
      7   | otherwise = seed
      8 
      9 main :: IO ()
     10 main = do
     11   lines <- lines <$> getContents
     12 
     13   let sections = splitOn [""] lines
     14   let seeds = map read $ tail $ splitOn [' '] (sections!!0!!0) :: [Int]
     15 
     16   let maps = map (\(_:xs) -> (map (\l -> map (\i -> (read i) :: Int) (splitOn [' '] l)) xs)) (tail sections)
     17   let myMapFns = map (toMapFn) maps
     18   let myFn = foldr (.) id (reverse myMapFns)
     19 
     20   print (minimum (map myFn seeds))
     21 
     22   -- mapM_ putStrLn lines
     23