1 module Main(main) where
3 import Data.List (tails)
6 -- input = ".^^.^.^^^^"
7 input = "^.^^^.^..^....^^....^^^^.^^.^...^^.^.^^.^^.^^..^.^...^.^..^.^^.^..^.....^^^.^.^^^..^^...^^^...^...^."
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
19 part2 = print $ length $ filter (not) $ concat $ take 400000 $ iterate nextRow $ readRow input
21 readRow :: String -> [Bool]
24 showRow :: [Bool] -> String
25 showRow = map (\c -> if c then '^' else '.')
27 extended :: [Bool] -> [Bool]
28 extended row = [False] ++ row ++ [False]
30 nextRow :: [Bool] -> [Bool]
31 nextRow = map (isTrap) . segments . extended
33 segments :: [a] -> [[a]]
34 segments = filter ((==3) . length) . map (take 3) . tails
36 isTrap :: [Bool] -> Bool
38 | segment == [True, True, False] = True
39 | segment == [False, True, True] = True
40 | segment == [True, False, False] = True
41 | segment == [False, False, True] = True