c7a43483f8a025680d1cda17fdd2a1b3460655a0
[advent-of-code-17.git] / adventofcode1702 / app / advent02.hs
1 module Main(main) where
2
3 import Text.Parsec
4 import Text.ParserCombinators.Parsec.Number
5
6
7 main :: IO ()
8 main = do
9 text <- readFile "data/advent02.txt"
10 let sheet = successfulParse $ parseFile text
11 print $ part1 sheet
12 print $ part2 sheet
13
14
15 part1 :: [[Int]] -> Int
16 part1 = sum . map p1cSum
17
18 part2 :: [[Int]] -> Int
19 part2 = sum . map p2cSum
20
21
22 p1cSum :: [Int] -> Int
23 p1cSum row = (maximum row) - (minimum row)
24
25 p2cSum :: [Int] -> Int
26 p2cSum digits = sum [a `div` b | a <- digits, b <- digits, a /= b, a `mod` b == 0]
27
28
29
30 sFile = sLine `sepEndBy` newline
31 sLine = int `sepBy` onlySpaces
32
33 onlySpaces = many (oneOf " \t")
34
35 parseFile :: String -> Either ParseError [[Int]]
36 parseFile input = parse sFile "(unknown)" input
37
38 parseLine :: String -> Either ParseError [Int]
39 parseLine input = parse sLine "(unknown)" input
40
41 successfulParse :: Either ParseError [a] -> [a]
42 successfulParse (Left _) = []
43 successfulParse (Right a) = a
44