Day 10 solution
[?]
Jan 15, 2021, 1:45 AM
77HPKKSDM4FNVTQBYKNK3WW6XM2STQRNJTATIJAN4ILKJDTPRASQCDependencies
- [2]
B527MN66Solve day 1 for 2020
Change contents
- file addition: day10.hs[2.17]
{-# LANGUAGE LambdaCase #-}import Data.Listimport Data.Maybeimport qualified Data.IntMap as IMimport System.Environmentdistribution :: [Int] -> (Int,Int,Int)distribution = foldl' (\(a,b,c) d -> case d of1 -> let a' = a + 1 in a' `seq` (a',b,c)2 -> let b' = b + 1 in b' `seq` (a,b',c)3 -> let c' = c + 1 in c' `seq` (a,b,c')_ -> (a,b,c)) (0,0,0)differences :: [Int] -> [Int]differences = unfoldr (\case(a:r@(b:_)) -> Just (b - a, r)[_] -> Just (3,[])_ -> Nothing) . (0:) . sortarrangements :: [Int] -> Integerarrangements l' = snd $ IM.findMax m wherel = foldr (\a r -> case r of[] -> [a, a + 3]_ -> a:r) [] $ sort l'm = IM.insert 0 1 $ IM.fromList $ fmap (\k ->(k, sum $ mapMaybe (\d ->IM.lookup (k - d) m) [1 .. 3])) lmain = do[fn] <- getArgsjrs <- (map read . words) <$> readFile fnlet(s,_,t) = distribution $ differences jrsprint $ fromIntegral s * fromIntegral tprint $ arrangements jrs