X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=adventofcode16%2Fapp%2Fadvent18.hs;fp=adventofcode16%2Fapp%2Fadvent18.hs;h=2b1462f72ed069f80b743c38942c8c082e072c38;hb=7267c0fa74db510564dc59587dd076372640114f;hp=0000000000000000000000000000000000000000;hpb=b66f0f79e01057fcb153ac16ce13ff50943a6d02;p=advent-of-code-16.git diff --git a/adventofcode16/app/advent18.hs b/adventofcode16/app/advent18.hs new file mode 100644 index 0000000..2b1462f --- /dev/null +++ b/adventofcode16/app/advent18.hs @@ -0,0 +1,42 @@ +module Main(main) where + +import Data.List (tails) + +-- input = "..^^." +-- input = ".^^.^.^^^^" +input = "^.^^^.^..^....^^....^^^^.^^.^...^^.^.^^.^^.^^..^.^...^.^..^.^^.^..^.....^^^.^.^^^..^^...^^^...^...^." + +main :: IO () +main = do + part1 + part2 + +part1 :: IO () +-- part1 = putStrLn $ unlines $ map (showRow) $ take 10 $ iterate nextRow $ readRow input +part1 = print $ length $ filter (not) $ concat $ take 40 $ iterate nextRow $ readRow input + +part2 :: IO () +part2 = print $ length $ filter (not) $ concat $ take 400000 $ iterate nextRow $ readRow input + +readRow :: String -> [Bool] +readRow = map (=='^') + +showRow :: [Bool] -> String +showRow = map (\c -> if c then '^' else '.') + +extended :: [Bool] -> [Bool] +extended row = [False] ++ row ++ [False] + +nextRow :: [Bool] -> [Bool] +nextRow = map (isTrap) . segments . extended + +segments :: [a] -> [[a]] +segments = filter ((==3) . length) . map (take 3) . tails + +isTrap :: [Bool] -> Bool +isTrap segment + | segment == [True, True, False] = True + | segment == [False, True, True] = True + | segment == [True, False, False] = True + | segment == [False, False, True] = True + | otherwise = False