-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay05.hs
43 lines (33 loc) · 1.31 KB
/
Day05.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
41
42
43
import Data.List.Split (splitOn, chunksOf)
import Data.Array ( Ix(inRange, range) )
type Range = (Int, Int, Int)
dest :: (a, b, c) -> a
dest (x, _, _) = x
srcRange :: (a, b, c) -> b
srcRange (_, x, _) = x
rangeLen :: (a, b, c) -> c
rangeLen (_, _, x) = x
getSeeds :: [[String]] -> [Int]
getSeeds xs = map read $ drop 1 $ words $ head $ head xs
getRanges :: [[String]] -> [[Range]]
getRanges = map (map ((\[x, y, z] -> (x, y, z)) . map read . words) . drop 1) . drop 1
categories :: String -> [String]
categories = splitOn "\n\n"
main :: IO ()
main = do
inputs <- categories <$> readFile "./Input/Day05.txt"
let input = map lines inputs
ranges = getRanges input
seeds = getSeeds input
-- results in many seeds :(
expandedSeeds = concatMap (\(dest : srcRange : _) -> [dest .. (dest - 1 + srcRange)]) $ chunksOf 2 seeds
-- mapM_ print ranges
print $ minimum $ map (`getMappingResult` ranges) seeds
print $ minimum $ map (`getMappingResult` ranges) expandedSeeds
getMappingResult :: Int -> [[Range]] -> Int
getMappingResult = foldl adaptCurrent
adaptCurrent :: Int -> [Range] -> Int
adaptCurrent current [] = current
adaptCurrent current (x:xs)
| inRange (srcRange x, srcRange x + rangeLen x) current = dest x + current - srcRange x
| otherwise = adaptCurrent current xs