1 import Data.List (transpose, sort)
2 import Data.List.Split (splitOn, chunksOf)
4 type Triple = [Integer]
8 instrText <- readFile "advent03.txt"
9 let triangles = map (parseLine) $ lines instrText
14 part1 :: [Triple] -> IO ()
16 print $ length $ filter (validTriangle) triangles
18 part2 :: [Triple] -> IO ()
20 print $ length $ filter (validTriangle) $ byColumns triangles
23 parseLine :: String -> Triple
24 parseLine = map (read) . filter (not . null) . splitOn " "
26 validTriangle :: Triple -> Bool
27 validTriangle triple = sortedTriple!!0 + sortedTriple!!1 > sortedTriple!!2
28 where sortedTriple = sort triple
30 byColumns :: [[Integer]] -> [Triple]
31 byColumns = chunksOf 3 . concat . transpose