1 module Main(main) where
3 import Data.List (transpose, sort)
4 import Data.List.Split (splitOn, chunksOf)
6 type Triple = [Integer]
10 instrText <- readFile "advent03.txt"
11 let triangles = map (parseLine) $ lines instrText
16 part1 :: [Triple] -> IO ()
18 print $ length $ filter (validTriangle) triangles
20 part2 :: [Triple] -> IO ()
22 print $ length $ filter (validTriangle) $ byColumns triangles
25 parseLine :: String -> Triple
26 parseLine = map (read) . filter (not . null) . splitOn " "
28 validTriangle :: Triple -> Bool
29 validTriangle triple = sortedTriple!!0 + sortedTriple!!1 > sortedTriple!!2
30 where sortedTriple = sort triple
32 byColumns :: [[Integer]] -> [Triple]
33 byColumns = chunksOf 3 . concat . transpose