summaryrefslogtreecommitdiff
path: root/2015/day15.hs
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-12-14 20:19:02 +0100
committertomsmeding <tom.smeding@gmail.com>2016-12-14 20:19:02 +0100
commit2d02f553aa4cc4ded630628eccdf34f55937cee5 (patch)
treed5377ebdff68788725b5820d5331ce7b6c9d4a84 /2015/day15.hs
parent97b4c5d86cc12447ac6845e25a863e26a88aec35 (diff)
Add 2015 sources
Diffstat (limited to '2015/day15.hs')
-rw-r--r--2015/day15.hs30
1 files changed, 30 insertions, 0 deletions
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