From 5eaa0cf7eaef1a58be0f13c1273971bb118aa143 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Thu, 13 Dec 2018 14:41:25 +0000 Subject: [PATCH] Started day 13 --- src/advent13/advent13.hs | 63 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/advent13/advent13.hs diff --git a/src/advent13/advent13.hs b/src/advent13/advent13.hs new file mode 100644 index 0000000..354d24a --- /dev/null +++ b/src/advent13/advent13.hs @@ -0,0 +1,63 @@ +{-# LANGUAGE OverloadedStrings #-} + + +import Data.List +import qualified Data.Set as S + +data Cell = Empty | Horizontal | Vertical | Clockwise | Anticlockwise | Junction deriving (Show, Eq) +data Direction = Up | Right | Down | Left deriving (Show, Eq, Ord, Enum, Bounded) + +main :: IO () +main = do + text <- TIO.readFile "data/advent12.txt" + let (initial, rules) = successfulParse text + let row = makeWorld 0 initial + print $ part1 rules row + print $ part2 rules row + + + +-- Move in the current direction +takeStep :: Direction -> Cell -> Int -> Int -> (Direction, Int, Int) +takeStep Up x y = (x, y-1) +takeStep Down x y = (x, y+1) +takeStep Left x y = ( x-1, y) +takeStep Right x y = (x+1, y) + + +-- | a `succ` that wraps +turnCW :: (Bounded a, Enum a, Eq a) => a -> a +turnCW dir | dir == maxBound = minBound + | otherwise = succ dir + +-- | a `pred` that wraps +turnACW :: (Bounded a, Enum a, Eq a) => a -> a +turnACW dir | dir == minBound = maxBound + | otherwise = pred dir + + + +-- Parse the input file + +type Parser = Parsec Void Text + +sc :: Parser () +sc = L.space (skipSome spaceChar) CA.empty CA.empty + +symb = L.symbol sc +potP = (char '.' *> pure False) <|> (char '#' *> pure True) + +initialPrefix = symb "initial state:" +ruleSepP = symb "=>" + +fileP = (,) <$> initialP <*> many ruleP +initialP = initialPrefix *> many potP <* sc +ruleP = Rule <$> ruleLHS <* ruleSepP <*> ruleRHS +ruleLHS = count 5 potP <* sc +ruleRHS = potP <* sc + +successfulParse :: Text -> ([Bool], [Rule]) +successfulParse input = + case parse fileP "input" input of + Left _error -> ([], []) -- TIO.putStr $ T.pack $ parseErrorPretty err + Right world -> world -- 2.34.1