diff options
author | Orangerot <purple@orangerot.dev> | 2024-05-17 15:41:55 +0200 |
---|---|---|
committer | Orangerot <purple@orangerot.dev> | 2024-05-17 15:41:55 +0200 |
commit | c6075f736e67e47b940abb9c1a0c54753d4b55b8 (patch) | |
tree | 69d827646e615e00f91ef54361cc1701e4f0eff7 /2023/day05/Main2.hs | |
parent | a1895fe157e06ee4d119576163ea76390b3d402c (diff) |
Diffstat (limited to '2023/day05/Main2.hs')
-rw-r--r-- | 2023/day05/Main2.hs | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/2023/day05/Main2.hs b/2023/day05/Main2.hs new file mode 100644 index 0000000..3a57caa --- /dev/null +++ b/2023/day05/Main2.hs @@ -0,0 +1,52 @@ +import Data.List.Split +import Data.List +import Data.Maybe + +windows :: [a] -> [[a]] +windows = filter (\xs -> length xs == 2) . map (take 2) . tails + +genBetweens :: [[Int]] -> [[Int]] +genBetweens section = map (\[[dl, sl, rl], [du, su, ru]] -> [sl + rl, sl + rl, su + ru - sl]) (windows section) + +-- 79 14 +-- +-- 50 98 2 +-- 52 50 48 +sectionToRange :: [Int] -> [Int] -> [Int] +sectionToRange [d,s,r] [start,range] + | begin <= s + r - 1 && end >= s = [d + (begin - s), end - begin + 1] + | otherwise = [start, range] + where + begin = max start s + end = min (start+range -1) (s+r-1) + +toMapFn :: [[Int]] -> [[Int]] -> [[Int]] +toMapFn section seedPairs = nub $ filter (/= []) $ concat $ map (\f -> (map (f) seedPairs)) (map (sectionToRange) section) + +main :: IO () +main = do + lines <- lines <$> getContents + + let sections = splitOn [""] lines + let seeds = map read $ tail $ splitOn [' '] (sections!!0!!0) :: [Int] + let seedPairs = chunksOf 2 seeds + + let maps = map (\(_:xs) -> (map (\l -> map (\i -> (read i) :: Int) (splitOn [' '] l)) xs)) (tail sections) + 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 + let myMapFns = map (toMapFn) maps + let myFn = foldr (.) id (reverse myMapFns) + + let seedRanges = concat $ map (\seedPair -> myFn [seedPair]) seedPairs + + print ((myMapFns!!0) [seedPairs!!0]) + -- print ((myMapFns!!1) ((myMapFns!!0) [seedPairs!!0])) + -- print ((myFn) [seedPairs!!0]) + -- print ((map (head) ((myFn) [seedPairs!!0]))) + -- print ((map (head) ((myFn) [seedPairs!!0]))) + -- print seedRanges + -- print ((map (\[start, range] -> start) seedRanges)) + -- print (minimum (map (\[start, range] -> start) seedRanges)) + + + -- mapM_ putStrLn lines + |