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/day15.hs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 2015/day15.hs (limited to '2015/day15.hs') diff --git a/2015/day15.hs b/2015/day15.hs new file mode 100644 index 0000000..8787c9c --- /dev/null +++ b/2015/day15.hs @@ -0,0 +1,30 @@ +import Data.Char +import Control.Monad + +total :: Int +total = 100 + +isDigitOrMinus :: Char -> Bool +isDigitOrMinus c = isDigit c || c == '-' + +parse :: [String] -> [Int] +parse [_,_,a,_,b,_,c,_,d,_,e] = map (read . takeWhile isDigitOrMinus) [a,b,c,d,e] + +getall :: [[Int]] -> [Int] +getall ing = map fst $ filter cal500 $ map computescore $ allcomb ing + where allcomb ing = allcomb' ing 0 + allcomb' [] tot = if tot == total then [[0,0,0,0,0]] else [] + allcomb' (i:is) tot = concat [map (\x -> map (\(a,b)->am*a+b) $ zip i x) $ allcomb' is (tot+am) + | am <- [0..(total-tot)]] + computescore x = (product $ map (\x -> if x < 0 then 0 else x) $ init x,last x) + cal500 (_,cal) = cal == 500 -- make this function always True for part 1 + +day15 :: IO () +day15 = do + input <- liftM (map (parse . words) . lines) $ readFile "day15.txt" + --let input = [[-1,-2,6,3],[2,3,-2,-1]] + print input + let result = maximum $ getall input + print result + +main = day15 -- cgit v1.2.3-70-g09d2