Got Stack working with days in separate packages
[advent-of-code-16.git] / adventofcode16 / app / advent18.hs
1 module Main(main) where
2
3 import Data.List (tails)
4
5 -- input = "..^^."
6 -- input = ".^^.^.^^^^"
7 input = "^.^^^.^..^....^^....^^^^.^^.^...^^.^.^^.^^.^^..^.^...^.^..^.^^.^..^.....^^^.^.^^^..^^...^^^...^...^."
8
9 main :: IO ()
10 main = do
11 part1
12 part2
13
14 part1 :: IO ()
15 -- part1 = putStrLn $ unlines $ map (showRow) $ take 10 $ iterate nextRow $ readRow input
16 part1 = print $ length $ filter (not) $ concat $ take 40 $ iterate nextRow $ readRow input
17
18 part2 :: IO ()
19 part2 = print $ length $ filter (not) $ concat $ take 400000 $ iterate nextRow $ readRow input
20
21 readRow :: String -> [Bool]
22 readRow = map (=='^')
23
24 showRow :: [Bool] -> String
25 showRow = map (\c -> if c then '^' else '.')
26
27 extended :: [Bool] -> [Bool]
28 extended row = [False] ++ row ++ [False]
29
30 nextRow :: [Bool] -> [Bool]
31 nextRow = map (isTrap) . segments . extended
32
33 segments :: [a] -> [[a]]
34 segments = filter ((==3) . length) . map (take 3) . tails
35
36 isTrap :: [Bool] -> Bool
37 isTrap segment
38 | segment == [True, True, False] = True
39 | segment == [False, True, True] = True
40 | segment == [True, False, False] = True
41 | segment == [False, False, True] = True
42 | otherwise = False