1 {-# LANGUAGE OverloadedStrings #-}
5 import qualified Data.Set as S
7 data Cell = Empty | Horizontal | Vertical | Clockwise | Anticlockwise | Junction deriving (Show, Eq)
8 data Direction = Up | Right | Down | Left deriving (Show, Eq, Ord, Enum, Bounded)
12 text <- TIO.readFile "data/advent12.txt"
13 let (initial, rules) = successfulParse text
14 let row = makeWorld 0 initial
15 print $ part1 rules row
16 print $ part2 rules row
20 -- Move in the current direction
21 takeStep :: Direction -> Cell -> Int -> Int -> (Direction, Int, Int)
22 takeStep Up x y = (x, y-1)
23 takeStep Down x y = (x, y+1)
24 takeStep Left x y = ( x-1, y)
25 takeStep Right x y = (x+1, y)
28 -- | a `succ` that wraps
29 turnCW :: (Bounded a, Enum a, Eq a) => a -> a
30 turnCW dir | dir == maxBound = minBound
31 | otherwise = succ dir
33 -- | a `pred` that wraps
34 turnACW :: (Bounded a, Enum a, Eq a) => a -> a
35 turnACW dir | dir == minBound = maxBound
36 | otherwise = pred dir
40 -- Parse the input file
42 type Parser = Parsec Void Text
45 sc = L.space (skipSome spaceChar) CA.empty CA.empty
48 potP = (char '.' *> pure False) <|> (char '#' *> pure True)
50 initialPrefix = symb "initial state:"
53 fileP = (,) <$> initialP <*> many ruleP
54 initialP = initialPrefix *> many potP <* sc
55 ruleP = Rule <$> ruleLHS <* ruleSepP <*> ruleRHS
56 ruleLHS = count 5 potP <* sc
59 successfulParse :: Text -> ([Bool], [Rule])
60 successfulParse input =
61 case parse fileP "input" input of
62 Left _error -> ([], []) -- TIO.putStr $ T.pack $ parseErrorPretty err