From 5eb58ad076f2cd435b11b140820da224b60b73d5 Mon Sep 17 00:00:00 2001 From: Aria Date: Mon, 2 Jan 2023 21:58:56 +0000 Subject: initial commit --- 2020/10b.hs | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 2020/10b.hs (limited to '2020/10b.hs') diff --git a/2020/10b.hs b/2020/10b.hs new file mode 100644 index 0000000..e6851f6 --- /dev/null +++ b/2020/10b.hs @@ -0,0 +1,34 @@ +module Main where + +import System.Environment (getArgs) +import Data.List (sort) +import qualified Data.IntMap.Strict as M + +-- Delete elements from a list until element t is encountered +deleteUntil :: Eq a => [a] -> a -> [a] +deleteUntil [] _ = [] +deleteUntil (x:xs) t | x == t = xs + | otherwise = deleteUntil xs t + +-- Get the number of posisble valid permutations of a list of adapters +possiblePerms :: [Int] -> M.IntMap Int +possiblePerms ss = foldl addToMap (M.fromList [(0, 1)]) (sort ss ++ [end]) + where getChildren x m = [k | k <- [x-3..x-1], k `M.member` m] + end = maximum ss + 3 + addToMap m x = M.insert x (sum $ map (m M.!) (getChildren x m)) m + + +-- Read a line-seperated file of numbers +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; + + print $ possiblePerms xs; + return (); -- cgit v1.2.3