aoc

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

Main2.hs (1816B)


      1 import Data.List.Split
      2 import Data.List
      3 import Data.Maybe
      4 
      5 windows :: [a] -> [[a]]
      6 windows = filter (\xs -> length xs == 2) . map (take 2) . tails
      7 
      8 genBetweens :: [[Int]] -> [[Int]]
      9 genBetweens section = map (\[[dl, sl, rl], [du, su, ru]] -> [sl + rl, sl + rl, su + ru - sl]) (windows section)
     10 
     11 -- 79 14
     12 --
     13 -- 50 98 2
     14 -- 52 50 48
     15 sectionToRange :: [Int] -> [Int] -> [Int]
     16 sectionToRange [d,s,r] [start,range] 
     17   | begin <= s + r - 1 && end >= s = [d + (begin - s), end - begin + 1]
     18   | otherwise = [start, range]
     19   where 
     20     begin = max start s
     21     end = min (start+range -1) (s+r-1)
     22 
     23 toMapFn :: [[Int]] -> [[Int]] -> [[Int]]
     24 toMapFn section seedPairs = nub $ filter (/= []) $ concat $ map (\f -> (map (f) seedPairs)) (map (sectionToRange) section) 
     25 
     26 main :: IO ()
     27 main = do
     28   lines <- lines <$> getContents
     29 
     30   let sections = splitOn [""] lines
     31   let seeds = map read $ tail $ splitOn [' '] (sections!!0!!0) :: [Int]
     32   let seedPairs = chunksOf 2 seeds
     33 
     34   let maps = map (\(_:xs) -> (map (\l -> map (\i -> (read i) :: Int) (splitOn [' '] l)) xs)) (tail sections)
     35   let mapsBetweens = map (\x -> [0, 0, (x!!0!!2 -1)] ++ mapBetweens map ++ maps ++ [(last x)!!1 + (last x)!!2, (last x)!!1 + (last x)!!2, (maxBound :: Int) - (last x)!!1 + (last x)!!2] ) maps
     36   let myMapFns = map (toMapFn) maps
     37   let myFn = foldr (.) id (reverse myMapFns)
     38 
     39   let seedRanges = concat $ map (\seedPair -> myFn [seedPair]) seedPairs
     40 
     41   print ((myMapFns!!0) [seedPairs!!0])
     42   -- print ((myMapFns!!1) ((myMapFns!!0) [seedPairs!!0]))
     43   -- print ((myFn) [seedPairs!!0])
     44   -- print ((map (head) ((myFn) [seedPairs!!0])))
     45   -- print ((map (head) ((myFn) [seedPairs!!0])))
     46   -- print seedRanges
     47   -- print ((map (\[start, range] -> start) seedRanges))
     48   -- print (minimum (map (\[start, range] -> start) seedRanges))
     49 
     50 
     51   -- mapM_ putStrLn lines
     52