Started day 13
[advent-of-code-18.git] / src / advent13 / advent13.hs
1 {-# LANGUAGE OverloadedStrings #-}
2
3
4 import Data.List
5 import qualified Data.Set as S
6
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)
9
10 main :: IO ()
11 main = do
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
17
18
19
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)
26
27
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
32
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
37
38
39
40 -- Parse the input file
41
42 type Parser = Parsec Void Text
43
44 sc :: Parser ()
45 sc = L.space (skipSome spaceChar) CA.empty CA.empty
46
47 symb = L.symbol sc
48 potP = (char '.' *> pure False) <|> (char '#' *> pure True)
49
50 initialPrefix = symb "initial state:"
51 ruleSepP = symb "=>"
52
53 fileP = (,) <$> initialP <*> many ruleP
54 initialP = initialPrefix *> many potP <* sc
55 ruleP = Rule <$> ruleLHS <* ruleSepP <*> ruleRHS
56 ruleLHS = count 5 potP <* sc
57 ruleRHS = potP <* sc
58
59 successfulParse :: Text -> ([Bool], [Rule])
60 successfulParse input =
61 case parse fileP "input" input of
62 Left _error -> ([], []) -- TIO.putStr $ T.pack $ parseErrorPretty err
63 Right world -> world