{-# Language QuasiQuotes #-}
module Main (main) where
import Advent (count, format)
main :: IO ()
IO ()
main =
do [Char]
input <- [format|2016 18 %s%n|]
Int -> IO ()
forall a. Show a => a -> IO ()
print ([Char] -> Int -> Int
solve [Char]
input Int
40)
Int -> IO ()
forall a. Show a => a -> IO ()
print ([Char] -> Int -> Int
solve [Char]
input Int
400000)
solve ::
String ->
Int ->
Int
solve :: [Char] -> Int -> Int
solve [Char]
input Int
n = Char -> [Char] -> Int
forall (f :: * -> *) a. (Foldable f, Eq a) => a -> f a -> Int
count Char
'.' ([[Char]] -> [Char]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat (Int -> [[Char]] -> [[Char]]
forall a. Int -> [a] -> [a]
take Int
n (([Char] -> [Char]) -> [Char] -> [[Char]]
forall a. (a -> a) -> a -> [a]
iterate [Char] -> [Char]
next [Char]
input)))
rule90 :: Char -> Char -> Char -> Char
rule90 :: Char -> Char -> Char -> Char
rule90 Char
x Char
_ Char
y
| Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
/= Char
y = Char
'^'
| Bool
otherwise = Char
'.'
next :: String -> String
next :: [Char] -> [Char]
next (Char
x:[Char]
xs) = Char -> Char -> [Char] -> [Char]
go Char
'.' Char
x [Char]
xs
where
go :: Char -> Char -> [Char] -> [Char]
go Char
a Char
b [] = [Char -> Char -> Char -> Char
rule90 Char
a Char
b Char
'.']
go Char
a Char
b (Char
c:[Char]
cs) = Char -> Char -> Char -> Char
rule90 Char
a Char
b Char
c Char -> [Char] -> [Char]
forall a. a -> [a] -> [a]
: Char -> Char -> [Char] -> [Char]
go Char
b Char
c [Char]
cs