1 {-# LANGUAGE NegativeLiterals #-}
2 {-# LANGUAGE OverloadedStrings #-}
7 -- import Prelude hiding ((++))
8 import Data.Text (Text)
9 import qualified Data.Text as T
10 import qualified Data.Text.IO as TIO
12 import Data.Void (Void)
13 import Text.Megaparsec hiding (State)
14 import Text.Megaparsec.Char
15 import qualified Text.Megaparsec.Char.Lexer as L
16 import qualified Control.Applicative as CA
18 import qualified Data.Map.Strict as M
19 import Data.Map.Strict ((!))
20 import qualified Data.Set as S
22 import Linear (V2(..))
24 import Control.Monad.State.Lazy
26 type Coord = V2 Integer -- x, y, with north and east incresing values (origin a bottom left)
27 data Door = Door Coord Coord deriving (Show, Eq, Ord)
28 type Doors = S.Set Door
30 makeDoor :: Coord -> Coord -> Door
33 | otherwise = Door b a
40 text <- TIO.readFile "data/advent20.txt"
42 -- let (ip, instrs) = successfulParse text
44 -- -- print $ part1 ip instrs
45 -- print $ sum [i | i <- [1..1032], 1032 `mod` i == 0]
46 -- -- print $ part2 ip instrs
47 -- print $ sum [i | i <- [1..10551432], 10551432 `mod` i == 0]
51 -- type Parser = Parsec Void Text
52 type Parser = ParsecT Void Text (StateT [Coord] [])
56 sc = L.space (skipSome spaceChar) CA.empty CA.empty
59 -- integer = lexeme L.decimal
62 openBranchP = symb "("
63 closeBranchP = symb ")"
68 doorP = nP <|> sP <|> eP <|> wP
69 nP = (symb "N" *> pure (V2 0 1))
70 sP = (symb "S" *> pure (V2 0 -1))
71 eP = (symb "E" *> pure (V2 1 0))
72 wP = (symb "W" *> pure (V2 -1 0))
74 -- instructionFileP = (startP `between` endP) branchesP
76 -- branchesP :: MyParser Doors
77 -- branchesP = fmap S.unions . many $ choiceP <|> pathP
79 -- choiceP :: MyParser Doors
80 -- choiceP = (openBranchP `between` closeBranchP) $ do
82 -- return fmap S.unions (`sepBy` branchSepP) $ do
88 -- pathP :: MyParser Doors
89 -- pathP = S.fromList <$> many stepP
92 -- stepP :: MyParser Door
96 let theres = map (+delta) heres
98 return (map (\h -> makeDoor h (h + delta)) heres)
100 -- choiceP :: MyParser [Door]
101 -- choiceP = (openBranchP `between` closeBranchP) $ do
106 -- branch <- (pathP `sepBy` branchSepP)
109 -- fmap concat $ (`sepBy` branchSepP) $ do
115 choices <- (`sepBy` branchSepP) $ do
118 return $ concat choices -- (S.unions choices)-}
120 -- choiceOrPathP = many (choiceP <|> pathP)
123 -- successfulParse :: Text -> (Integer, [Instruction])
124 -- successfulParse input =
125 -- case parse instructionsP "input" input of
126 -- Left _error -> (0, []) -- TIO.putStr $ T.pack $ parseErrorPretty err
127 -- Right instructions -> instructions