From 2d02f553aa4cc4ded630628eccdf34f55937cee5 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 14 Dec 2016 20:19:02 +0100 Subject: Add 2015 sources --- 2015/day16.hs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 2015/day16.hs (limited to '2015/day16.hs') diff --git a/2015/day16.hs b/2015/day16.hs new file mode 100644 index 0000000..a18fce1 --- /dev/null +++ b/2015/day16.hs @@ -0,0 +1,36 @@ +import Data.Char +import Data.List +import Control.Monad +import qualified Data.Map.Strict as Map +import Data.Map.Strict ((!)) + +realSue :: Map.Map String (Int -> Bool) +realSue = Map.fromList [ -- for part 1, make all functions equalities + ("children", (==3)), + ("cats", (>7)), + ("samoyeds", (==2)), + ("pomeranians",(<3)), + ("akitas", (==0)), + ("vizslas", (==0)), + ("goldfish", (<5)), + ("trees", (>3)), + ("cars", (==2)), + ("perfumes", (==1))] + +parse :: String -> [(String,Int)] +parse s = parse' $ drop 2 $ words s + +parse' :: [String] -> [(String,Int)] +parse' [] = [] +parse' (k:v:xs) = (init k,read $ takeWhile isDigit v) : parse' xs + +matches :: [(String,Int)] -> Map.Map String (Int -> Bool) -> Bool +matches [] _ = True +matches ((k,v):atts) map = (map ! k) v && matches atts map + +day16 :: IO () +day16 = do + input <- liftM (zip [1..] . map parse . lines) $ readFile "day16.txt" + sequence_ $ map (print . fst) $ filter (\(i,atts) -> matches atts realSue) input + +main = day16 -- cgit v1.2.3-54-g00ecf