1 import Data.List (tails, foldl')
4 -- input = ".^^.^.^^^^"
5 input = "^.^^^.^..^....^^....^^^^.^^.^...^^.^.^^.^^.^^..^.^...^.^..^.^^.^..^.....^^^.^.^^^..^^...^^^...^...^."
13 part1 = print $ fst $ foldl' nextRowFold (countSafe row, row) [2..40]
14 where row = readRow input
17 part2 = print $ fst $ foldl' nextRowFold (countSafe row, row) [2..400000]
18 where row = readRow input
20 readRow :: String -> [Bool]
23 showRow :: [Bool] -> String
24 showRow = map (\c -> if c then '^' else '.')
26 extended :: [Bool] -> [Bool]
27 extended row = [False] ++ row ++ [False]
29 nextRow :: [Bool] -> [Bool]
30 nextRow = map (isTrap) . segments . extended
32 nextRowFold :: (Int, [Bool]) -> Int -> (Int, [Bool])
33 nextRowFold (n, row) _ = (n + countSafe newRow, newRow)
34 where newRow = nextRow row
36 countSafe :: [Bool] -> Int
37 countSafe = length . filter (not)
39 segments :: [a] -> [[a]]
40 segments = filter ((==3) . length) . map (take 3) . tails
42 isTrap :: [Bool] -> Bool
44 | segment == [True, True, False] = True
45 | segment == [False, True, True] = True
46 | segment == [True, False, False] = True
47 | segment == [False, False, True] = True