AoC/2023/day05/day05.hs
Gattix fba7bef5d2 day5 part1
no termination yet for part 2, needs more thinky thinky
2023-12-05 21:57:17 +01:00

35 lines
792 B
Haskell

import Control.Arrow
import Data.Ix
import Data.List.Split
type SeedMap = [((Int, Int), Int)]
main :: IO ()
main = do
input <- map (map words . lines) . splitOn "\n\n" <$> readFile "input"
let (seeds, maps) = parse input
print $ day5a maps seeds
parse :: [[[String]]] -> ([Int], [SeedMap])
parse = parseSeeds &&& map parseMap . tail
where
parseSeeds = map read . tail . head . head
parseMap :: [[String]] -> SeedMap
parseMap = map (f . map read) . tail
where
f [x, y, z] = ((y, y + pred z), x - y)
applyMap :: Int -> SeedMap -> Int
applyMap x m =
if null filtered
then x
else x + snd (head filtered)
where
filtered = filter ((`inRange` x) . fst) m
day5a :: [SeedMap] -> [Int] -> Int
day5a maps = minimum . map f
where
f x = foldl applyMap x maps