-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday05.hs
More file actions
29 lines (23 loc) · 1.03 KB
/
Copy pathday05.hs
File metadata and controls
29 lines (23 loc) · 1.03 KB
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
{-# LANGUAGE RecordWildCards #-}
module Main where
import Data.List.Split (splitOn)
import Control.Monad (msum, ap)
import Data.Maybe (mapMaybe, fromMaybe)
import Debug.Trace (trace)
import Data.Bifunctor (first, second)
data Range = Range { destStart :: Int, sourceStart :: Int, range :: Int } deriving (Show)
main = interact (unlines . sequence [part1] . parse . splitOn "\n\n")
part1 = ("Part 1: " ++) . show . minimum . uncurry (foldl pickRange)
pickRange :: [Int] -> [Range] -> [Int]
pickRange sources ranges = map (\s -> fromMaybe s (msum $ map (`applyRange` s) ranges)) sources
applyRange :: Range -> Int -> Maybe Int
applyRange Range{..} source =
if sourceStart <= source && source <= sourceStart + range
then return (source + destStart - sourceStart)
else Nothing
parse :: [String] -> ([Int], [[Range]])
parse (x:xs) = (seeds, ranges)
where
seeds = map read $ tail $ words x
ranges = map (map (toRange . map read . words) . tail . lines) xs
toRange [destStart, sourceStart, range] = Range destStart sourceStart range