1 import Data.Text (Text)
2 import qualified Data.Text.IO as TIO
4 import Data.Void (Void)
6 import Text.Megaparsec hiding (State)
7 import Text.Megaparsec.Char
8 import qualified Text.Megaparsec.Char.Lexer as L
9 import qualified Control.Applicative as CA
11 import Data.List (foldl')
12 import qualified Data.Map as M
15 import Linear (V2(..), (^+^), (^-^), (*^), (*^))
17 data Direction = East | South | West | North deriving (Show, Eq)
19 type Location = V2 Int -- x, y
21 type Visited = M.Map Location Int
23 data Path = Path { _visited :: Visited
25 , _currentLength :: Int
29 data Segment = Segment { _direction :: Direction
36 text <- TIO.readFile "data/advent03.txt"
37 let segmentss = successfulParse text
38 let paths = travelAllPaths segmentss
39 -- print $ travelPath $ head segmentss
43 part1 :: [Path] -> Int
44 part1 paths = closest $ crossovers paths
46 part2 :: [Path] -> Int
47 part2 paths = shortestPaths $ crossovers paths
50 closest :: Visited -> Int
51 closest points = snd $ M.findMin $ M.mapWithKey (\k _ -> manhattan k) points
53 shortestPaths :: Visited -> Int
54 shortestPaths crossings = minimum $ M.elems crossings
57 crossovers :: [Path] -> Visited
58 crossovers travelledPaths =
59 foldl' (M.intersectionWith (+))
60 (_visited $ head travelledPaths)
61 (map _visited $ drop 1 travelledPaths)
63 travelAllPaths :: [[Segment]] -> [Path]
64 travelAllPaths = map travelPath
66 travelPath :: [Segment] -> Path
67 travelPath segments = foldl' travelSegment path0 segments
68 where path0 = Path { _visited = M.empty, _tip = V2 0 0, _currentLength = 0 }
70 travelSegment :: Path -> Segment -> Path
71 travelSegment path segment = path { _tip = tip', _visited = visited', _currentLength = len'}
72 where delta = facing $ _direction segment
73 distance = _steps segment
75 len = _currentLength path
77 visited = _visited path
78 visited' = foldl' insertStep visited $ take distance $ drop 1 $ zip [len, (len + 1) ..] $ iterate (^+^ delta) start
79 tip' = start ^+^ distance *^ delta
80 insertStep visits (dist, loc) = if loc `M.member` visits
82 else M.insert loc dist visits
84 facing :: Direction -> Location
86 facing South = V2 0 (-1)
87 facing West = V2 (-1) 0
91 manhattan (V2 x y) = (abs x) + (abs y)
93 -- Parse the input file
94 type Parser = Parsec Void Text
97 sc = L.space (skipSome spaceChar) CA.empty CA.empty
98 -- sc = L.space (skipSome (char ' ')) CA.empty CA.empty
101 integer = lexeme L.decimal
102 -- signedInteger = L.signed sc integer
107 pathP = segmentP `sepBy1` comma
109 segmentP = segmentify <$> directionP <*> integer
110 where segmentify direction steps =
111 Segment { _direction = direction, _steps = steps }
114 directionP = eP <|> sP <|> wP <|> nP
115 eP = (symb "R" *> pure East)
116 sP = (symb "D" *> pure South)
117 wP = (symb "L" *> pure West)
118 nP = (symb "U" *> pure North)
121 successfulParse :: Text -> [[Segment]]
122 successfulParse input =
123 case parse wiresP "input" input of
124 Left _err -> [] -- TIO.putStr $ T.pack $ parseErrorPretty err