From c56796a2ebe192fe0832b4e5000e900fcfd5fa40 Mon Sep 17 00:00:00 2001 From: tomsmeding Date: Wed, 13 Dec 2017 09:33:20 +0100 Subject: Day 13 --- 2017/13.hs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 2017/13.hs (limited to '2017/13.hs') diff --git a/2017/13.hs b/2017/13.hs new file mode 100644 index 0000000..77254ca --- /dev/null +++ b/2017/13.hs @@ -0,0 +1,21 @@ +import Control.Monad +import Data.Char +import Data.List +import Data.Maybe + + +l2t :: [a] -> (a,a) +l2t [a,b] = (a,b) + +check :: [(Int, Int)] -> Int -> Bool +check inp off = all (\(d,r) -> if (d + off) `mod` (2 * (r - 1)) == 0 then False else True) inp + +cost :: [(Int, Int)] -> Int -> Int +cost inp off = sum $ map (\(d,r) -> if (d + off) `mod` (2 * (r - 1)) == 0 then d * r else 0) inp + +main :: IO () +main = do + input <- liftM (map (l2t . map (read . takeWhile isDigit) . words) . lines) (readFile "13.in") + + print $ cost input 0 + print $ fromJust $ findIndex id $ map (check input) [0..] -- cgit v1.2.3-70-g09d2