Strict fold
[advent-of-code-16.git] / advent03.hs
1 module Main(main) where
2
3 import Data.List (transpose, sort)
4 import Data.List.Split (splitOn, chunksOf)
5
6 type Triple = [Integer]
7
8 main :: IO ()
9 main = do
10 instrText <- readFile "advent03.txt"
11 let triangles = map (parseLine) $ lines instrText
12 part1 triangles
13 part2 triangles
14
15
16 part1 :: [Triple] -> IO ()
17 part1 triangles = do
18 print $ length $ filter (validTriangle) triangles
19
20 part2 :: [Triple] -> IO ()
21 part2 triangles = do
22 print $ length $ filter (validTriangle) $ byColumns triangles
23
24
25 parseLine :: String -> Triple
26 parseLine = map (read) . filter (not . null) . splitOn " "
27
28 validTriangle :: Triple -> Bool
29 validTriangle triple = sortedTriple!!0 + sortedTriple!!1 > sortedTriple!!2
30 where sortedTriple = sort triple
31
32 byColumns :: [[Integer]] -> [Triple]
33 byColumns = chunksOf 3 . concat . transpose