X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=adventofcode1702%2Fapp%2Fadvent02.hs;fp=adventofcode1702%2Fapp%2Fadvent02.hs;h=c7a43483f8a025680d1cda17fdd2a1b3460655a0;hb=bb4e947b1e698e036b6b5462b7c0d22d091014fd;hp=0000000000000000000000000000000000000000;hpb=1c73a76288c91ef6b410b2034bc324b60eb9a827;p=advent-of-code-17.git diff --git a/adventofcode1702/app/advent02.hs b/adventofcode1702/app/advent02.hs new file mode 100644 index 0000000..c7a4348 --- /dev/null +++ b/adventofcode1702/app/advent02.hs @@ -0,0 +1,44 @@ +module Main(main) where + +import Text.Parsec +import Text.ParserCombinators.Parsec.Number + + +main :: IO () +main = do + text <- readFile "data/advent02.txt" + let sheet = successfulParse $ parseFile text + print $ part1 sheet + print $ part2 sheet + + +part1 :: [[Int]] -> Int +part1 = sum . map p1cSum + +part2 :: [[Int]] -> Int +part2 = sum . map p2cSum + + +p1cSum :: [Int] -> Int +p1cSum row = (maximum row) - (minimum row) + +p2cSum :: [Int] -> Int +p2cSum digits = sum [a `div` b | a <- digits, b <- digits, a /= b, a `mod` b == 0] + + + +sFile = sLine `sepEndBy` newline +sLine = int `sepBy` onlySpaces + +onlySpaces = many (oneOf " \t") + +parseFile :: String -> Either ParseError [[Int]] +parseFile input = parse sFile "(unknown)" input + +parseLine :: String -> Either ParseError [Int] +parseLine input = parse sLine "(unknown)" input + +successfulParse :: Either ParseError [a] -> [a] +successfulParse (Left _) = [] +successfulParse (Right a) = a +