1 module Main(main) where
3 import Data.List (tails, foldl')
6 -- input = ".^^.^.^^^^"
7 input = "^.^^^.^..^....^^....^^^^.^^.^...^^.^.^^.^^.^^..^.^...^.^..^.^^.^..^.....^^^.^.^^^..^^...^^^...^...^."
15 part1 = print $ fst $ foldl' nextRowFold (countSafe row, row) [2..40]
16 where row = readRow input
19 part2 = print $ fst $ foldl' nextRowFold (countSafe row, row) [2..400000]
20 where row = readRow input
22 readRow :: String -> [Bool]
25 showRow :: [Bool] -> String
26 showRow = map (\c -> if c then '^' else '.')
28 extended :: [Bool] -> [Bool]
29 extended row = [False] ++ row ++ [False]
31 nextRow :: [Bool] -> [Bool]
32 nextRow = map (isTrap) . segments . extended
34 nextRowFold :: (Int, [Bool]) -> Int -> (Int, [Bool])
35 nextRowFold (n, row) _ = (n + countSafe newRow, newRow)
36 where newRow = nextRow row
38 countSafe :: [Bool] -> Int
39 countSafe = length . filter (not)
41 segments :: [a] -> [[a]]
42 segments = filter ((==3) . length) . map (take 3) . tails
44 isTrap :: [Bool] -> Bool
46 | segment == [True, True, False] = True
47 | segment == [False, True, True] = True
48 | segment == [True, False, False] = True
49 | segment == [False, False, True] = True