this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at c8d3db63099e68d453f8d28e369c6e9d129bd94c 38 lines 1.2 kB view raw
1module Millisecond02 where 2 3import Data.List.Split 4 5checksum :: [String] -> Integer 6checksum = 7 sum . map (\l -> let maxVal = maximum $ lineToInts l 8 minVal = minimum $ lineToInts l 9 in maxVal - minVal) 10 11lineToInts :: String -> [Integer] 12lineToInts = map (\w -> read w :: Integer) . words 13 14sumEvenlyDivisible :: [String] -> Integer 15sumEvenlyDivisible = 16 sum . map (\l -> let pair = getEvenlyDivisiblePair $ lineToInts l 17 in case pair of 18 Nothing -> 0 19 Just (x, y) -> div x y) 20 21getEvenlyDivisiblePair :: [Integer] -> Maybe (Integer, Integer) 22getEvenlyDivisiblePair ints = 23 let pairs = [ (x, y) | x <- ints, y <- ints ] 24 go :: [(Integer, Integer)] -> Maybe (Integer, Integer) 25 go [] = Nothing 26 go ((x, y):xs) 27 | (x `mod` y) == 0 = Just (x, y) 28 | otherwise = go xs 29 in go $ filter (\(x, y) -> x /= y) pairs 30 31 32main :: IO () 33main = do 34 inputStr <- readFile "input.txt" 35 let input = init inputStr -- Drops the \n at the end 36 -- answer = checksum $ splitOn "\n" input 37 answer = sumEvenlyDivisible $ splitOn "\n" input 38 in print answer