diff options
author | Aria <me@aria.rip> | 2023-01-02 21:58:56 +0000 |
---|---|---|
committer | Aria <me@aria.rip> | 2023-01-02 21:58:56 +0000 |
commit | 5eb58ad076f2cd435b11b140820da224b60b73d5 (patch) | |
tree | 2a67939595fbf993ff04f69b9cd3f0aa20827d96 /2020/10b.hs |
initial commit
Diffstat (limited to '2020/10b.hs')
-rw-r--r-- | 2020/10b.hs | 34 |
1 files changed, 34 insertions, 0 deletions
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 (); |