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