From 5eb58ad076f2cd435b11b140820da224b60b73d5 Mon Sep 17 00:00:00 2001 From: Aria Date: Mon, 2 Jan 2023 21:58:56 +0000 Subject: initial commit --- 2020/09b.hs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2020/09b.hs (limited to '2020/09b.hs') diff --git a/2020/09b.hs b/2020/09b.hs new file mode 100644 index 0000000..a643b29 --- /dev/null +++ b/2020/09b.hs @@ -0,0 +1,34 @@ +module Day9B where + +import System.Environment (getArgs) + +potentialSum :: [Int] -> Int -> [(Int, Int)] +potentialSum xs t = filter ((== t) . uncurry (+)) [(x, y) | x <- xs, y <- xs, x /= y] + +mapSumPrev :: [Int] -> Int -> [[(Int, Int)]] +mapSumPrev xs l = [potentialSum (take l $ drop (i - l) xs) v | (i, v) <- zip [25..] (drop l xs)] + +rangesSummingTo :: [Int] -> Int -> [[Int]] +rangesSummingTo [] _ = [] +rangesSummingTo xs t = filter ((== t) . sum) $ map (`take` xs) [1..length xs] + ++ rangesSummingTo (drop 1 xs) t + +numsFromFile :: String -> IO [Int] +numsFromFile p = do + c <- readFile p; + return $ map read $ lines c; + +main :: IO () +main = do + args <- getArgs; + xs <- numsFromFile $ head args; + + let unob = head $ filter (null . fst) $ zip (mapSumPrev xs 25) [25..]; + let unob_n = xs !! snd unob; + putStrLn $ "No way to sum up to " ++ show unob_n; + + let rs = head $ rangesSummingTo xs unob_n; + print rs; + + putStrLn $ "Smallest + Largest = " ++ show (minimum rs + maximum rs); + return (); -- cgit v1.2.3