X-Git-Url: https://git.njae.me.uk/?a=blobdiff_plain;f=advent08%2FMain.hs;h=5ab2916ced95bbdf93ebe3c6b3e7667f108c33d0;hb=a7b02636b8045a73a4dff090c1b932cfa1afd9bd;hp=b2c9895b9929f4a922bb4e51c25fe83a39c291c3;hpb=bc689ec22856749b96406382eac0345f2f71cb40;p=advent-of-code-23.git diff --git a/advent08/Main.hs b/advent08/Main.hs index b2c9895..5ab2916 100644 --- a/advent08/Main.hs +++ b/advent08/Main.hs @@ -31,7 +31,7 @@ main = -- print $ desert print $ part1 desert directions print $ part2 desert directions - print $ part3 desert directions + -- print $ part3 desert directions part1, part2 :: Desert -> [Direction] -> Int part1 desert directions = getSteps $ walk desert directions (State "AAA" 0) @@ -50,16 +50,16 @@ generateRouteLengths desert directions = M.unions ((fmap snd sResults) ++ (fmap gResults = fmap (walkWithCache desert directions M.empty) fromGoals -part3 desert directions = multiWalk desert directions M.empty starts - where starts = fmap (\s -> State s 0) $ startsOf desert +-- part3 desert directions = multiWalk desert directions M.empty starts +-- where starts = fmap (\s -> State s 0) $ startsOf desert -multiWalk desert directions cache states@(s:ss) - | (all isGoal states) && (sameTime states) = states - | otherwise = multiWalk desert directions newCache $ sort (s':ss) - where (s', newCache) = walkWithCache desert directions cache s +-- multiWalk desert directions cache states@(s:ss) +-- | (all isGoal states) && (sameTime states) = states +-- | otherwise = multiWalk desert directions newCache $ sort (s':ss) +-- where (s', newCache) = walkWithCache desert directions cache s -sameTime states = (length $ nub times) == 1 - where times = fmap getSteps states +-- sameTime states = (length $ nub times) == 1 +-- where times = fmap getSteps states walk :: Desert -> [Direction] -> State -> State walk desert directions start = head $ dropWhile (not . isGoal) path @@ -103,13 +103,13 @@ desertLineP :: Parser (String, Node) nodeP :: Parser Node nameP :: Parser String -problemP = (,) <$> ((many1 directionP) <* many1 endOfLine) <*> desertP +problemP = (,) <$> many1 directionP <* many1 endOfLine <*> desertP directionP = (L <$ "L") <|> (R <$ "R") desertP = M.fromList <$> desertLineP `sepBy` endOfLine -desertLineP = (,) <$> (nameP <* " = ") <*> nodeP +desertLineP = (,) <$> nameP <* " = " <*> nodeP -nodeP = Node <$> ("(" *> nameP <* ", ") <*> (nameP <* ")") +nodeP = Node <$ "(" <*> nameP <* ", " <*> nameP <* ")" nameP = many1 (letter <|> digit) successfulParse :: Text -> ([Direction], Desert)