Day 1 part 1
[advent-of-code-16.git] / advent18.hs
1 import Data.List (tails)
2
3 -- input = "..^^."
4 -- input = ".^^.^.^^^^"
5 input = "^.^^^.^..^....^^....^^^^.^^.^...^^.^.^^.^^.^^..^.^...^.^..^.^^.^..^.....^^^.^.^^^..^^...^^^...^...^."
6
7 main :: IO ()
8 main = do
9 part1
10 part2
11
12 part1 :: IO ()
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
15
16 part2 :: IO ()
17 part2 = print $ length $ filter (not) $ concat $ take 400000 $ iterate nextRow $ readRow input
18
19 readRow :: String -> [Bool]
20 readRow = map (=='^')
21
22 showRow :: [Bool] -> String
23 showRow = map (\c -> if c then '^' else '.')
24
25 extended :: [Bool] -> [Bool]
26 extended row = [False] ++ row ++ [False]
27
28 nextRow :: [Bool] -> [Bool]
29 nextRow = map (isTrap) . segments . extended
30
31 segments :: [a] -> [[a]]
32 segments = filter ((==3) . length) . map (take 3) . tails
33
34 isTrap :: [Bool] -> Bool
35 isTrap segment
36 | segment == [True, True, False] = True
37 | segment == [False, True, True] = True
38 | segment == [True, False, False] = True
39 | segment == [False, False, True] = True
40 | otherwise = False