1 import Data.List (tails)
4 -- input = ".^^.^.^^^^"
5 input = "^.^^^.^..^....^^....^^^^.^^.^...^^.^.^^.^^.^^..^.^...^.^..^.^^.^..^.....^^^.^.^^^..^^...^^^...^...^."
13 -- part1 = putStrLn $ unlines $ map (showRow) $ take 10 $ iterate nextRow $ readRow input
14 part1 = print $ length $ filter (not) $ concat $ take 40 $ iterate nextRow $ readRow input
17 part2 = print $ length $ filter (not) $ concat $ take 400000 $ iterate nextRow $ readRow input
19 readRow :: String -> [Bool]
22 showRow :: [Bool] -> String
23 showRow = map (\c -> if c then '^' else '.')
25 extended :: [Bool] -> [Bool]
26 extended row = [False] ++ row ++ [False]
28 nextRow :: [Bool] -> [Bool]
29 nextRow = map (isTrap) . segments . extended
31 segments :: [a] -> [[a]]
32 segments = filter ((==3) . length) . map (take 3) . tails
34 isTrap :: [Bool] -> Bool
36 | segment == [True, True, False] = True
37 | segment == [False, True, True] = True
38 | segment == [True, False, False] = True
39 | segment == [False, False, True] = True