summaryrefslogtreecommitdiff
path: root/2017/13.hs
blob: 77254ca08ece83434c1aef0a658c254cbd00bb10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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..]