From: Neil Smith Date: Wed, 10 Jan 2024 17:37:46 +0000 (+0000) Subject: Tidying, mainly parsers X-Git-Url: https://git.njae.me.uk/?a=commitdiff_plain;h=a7b02636b8045a73a4dff090c1b932cfa1afd9bd;p=advent-of-code-23.git Tidying, mainly parsers --- diff --git a/advent02/Main.hs b/advent02/Main.hs index 2e96f1b..e8e8f03 100644 --- a/advent02/Main.hs +++ b/advent02/Main.hs @@ -91,11 +91,11 @@ cubeP :: Parser Cube colourP, redP, greenP, blueP :: Parser Colour gamesP = gameP `sepBy` endOfLine -gameP = ParsedGame <$> (("Game " *> decimal) <* ": ") <*> showingsP +gameP = ParsedGame <$ "Game " <*> decimal <* ": " <*> showingsP showingsP = showingP `sepBy` "; " showingP = cubeP `sepBy` ", " -cubeP = (flip Cube) <$> (decimal <* " ") <*> colourP +cubeP = (flip Cube) <$> decimal <* " " <*> colourP colourP = redP <|> greenP <|> blueP redP = Red <$ "red" diff --git a/advent02/MainApplicative.hs b/advent02/MainApplicative.hs index 14cb248..acbcb5f 100644 --- a/advent02/MainApplicative.hs +++ b/advent02/MainApplicative.hs @@ -85,11 +85,11 @@ cubeP :: Parser Cube colourP, redP, greenP, blueP :: Parser Colour gamesP = gameP `sepBy` endOfLine -gameP = ParsedGame <$> (("Game " *> decimal) <* ": ") <*> showingsP +gameP = ParsedGame <$ "Game " <*> decimal <* ": " <*> showingsP showingsP = showingP `sepBy` "; " showingP = cubeP `sepBy` ", " -cubeP = (flip Cube) <$> (decimal <* " ") <*> colourP +cubeP = (flip Cube) <$> decimal <* " " <*> colourP colourP = redP <|> greenP <|> blueP redP = Red <$ "red" diff --git a/advent04/Main.hs b/advent04/Main.hs index 0d8b773..3448edc 100644 --- a/advent04/Main.hs +++ b/advent04/Main.hs @@ -62,8 +62,9 @@ cardP :: Parser Card numbersP :: Parser [Int] cardsP = cardP `sepBy` endOfLine -cardP = Card <$> (("Card" *> skipSpace *> decimal) <* ":" <* skipSpace) - <*> (numbersP <* " |" <* skipSpace) +cardP = Card <$ "Card" <* skipSpace + <*> decimal <* ":" <* skipSpace + <*> numbersP <* " |" <* skipSpace <*> numbersP numbersP = decimal `sepBy` skipSpace diff --git a/advent05/Main.hs b/advent05/Main.hs index d8bb806..16e4f4c 100644 --- a/advent05/Main.hs +++ b/advent05/Main.hs @@ -125,10 +125,10 @@ seedsP = "seeds: " *> numbersP almanacP = M.fromList <$> (aMapP `sepBy` blankLineP) aMapP = aMapify <$> aMapHeaderP <*> rulesP -aMapHeaderP = (,) <$> (nameP <* "-to-") <*> (nameP <* " map:" <* endOfLine) +aMapHeaderP = (,) <$> nameP <* "-to-" <*> nameP <* " map:" <* endOfLine rulesP = ruleP `sepBy` endOfLine -ruleP = ruleify <$> (decimal <* space) <*> (decimal <* space) <*> decimal +ruleP = ruleify <$> decimal <* space <*> decimal <* space <*> decimal numbersP = decimal `sepBy` skipSpace nameP = many1 letter diff --git a/advent05/MainDirect.hs b/advent05/MainDirect.hs index ca59a51..9a56bba 100644 --- a/advent05/MainDirect.hs +++ b/advent05/MainDirect.hs @@ -82,10 +82,10 @@ seedsP = "seeds: " *> numbersP almanacP = M.fromList <$> (aMapP `sepBy` blankLineP) aMapP = aMapify <$> aMapHeaderP <*> rulesP -aMapHeaderP = (,) <$> (nameP <* "-to-") <*> (nameP <* " map:" <* endOfLine) +aMapHeaderP = (,) <$> nameP <* "-to-" <*> nameP <* " map:" <* endOfLine rulesP = ruleP `sepBy` endOfLine -ruleP = Rule <$> (decimal <* space) <*> (decimal <* space) <*> decimal +ruleP = Rule <$> decimal <* space <*> decimal <* space <*> decimal numbersP = decimal `sepBy` skipSpace nameP = many1 letter diff --git a/advent06/Main.hs b/advent06/Main.hs index 2a056bb..8e29367 100644 --- a/advent06/Main.hs +++ b/advent06/Main.hs @@ -30,9 +30,9 @@ waysToWin (Race timeLimit record) = racesP :: Parser [Race] timesP, distancesP, numbersP :: Parser [Int] -racesP = zipWith Race <$> (timesP <* endOfLine) <*> distancesP -timesP = ("Time:" *> skipSpace) *> numbersP -distancesP = ("Distance:" *> skipSpace) *> numbersP +racesP = zipWith Race <$> timesP <* endOfLine <*> distancesP +timesP = "Time:" *> skipSpace *> numbersP +distancesP = "Distance:" *> skipSpace *> numbersP numbersP = decimal `sepBy` skipSpace successfulParse :: T.Text -> [Race] diff --git a/advent07/Main.hs b/advent07/Main.hs index 8f4350d..8f3a424 100644 --- a/advent07/Main.hs +++ b/advent07/Main.hs @@ -95,7 +95,7 @@ handP :: Parser Hand cardP :: Parser Card handsP = handP `sepBy` endOfLine -handP = Hand <$> ((many1 cardP) <* space) <*> decimal +handP = Hand <$> many1 cardP <* space <*> decimal cardP = (Two <$ "2") <|> (Three <$ "3") <|> (Four <$ "4") <|> (Five <$ "5") <|> (Six <$ "6") <|> (Seven <$ "7") <|> diff --git a/advent07/MainWithCase.hs b/advent07/MainWithCase.hs index e5a181f..bd0aa85 100644 --- a/advent07/MainWithCase.hs +++ b/advent07/MainWithCase.hs @@ -71,7 +71,7 @@ handP :: Parser Hand cardP :: Parser Card handsP = handP `sepBy` endOfLine -handP = Hand <$> ((many1 cardP) <* space) <*> decimal +handP = Hand <$> many1 cardP <* space <*> decimal cardP = (Two <$ "2") <|> (Three <$ "3") <|> (Four <$ "4") <|> (Five <$ "5") <|> (Six <$ "6") <|> (Seven <$ "7") <|> 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) diff --git a/advent12/Main.hs b/advent12/Main.hs index 2c07b38..7f1177d 100644 --- a/advent12/Main.hs +++ b/advent12/Main.hs @@ -82,7 +82,7 @@ recordP :: Parser Record springP :: Parser Spring recordsP = recordP `sepBy` endOfLine -recordP = Record <$> (many1 springP <* " ") <*> (decimal `sepBy` ",") +recordP = Record <$> many1 springP <* " " <*> decimal `sepBy` "," springP = (Unknown <$ "?") <|> (Damaged <$ "#") <|> (Operational <$ ".") successfulParse :: Text -> [Record] diff --git a/advent12/MainBruteForce.hs b/advent12/MainBruteForce.hs index 6b86203..7d11334 100644 --- a/advent12/MainBruteForce.hs +++ b/advent12/MainBruteForce.hs @@ -78,7 +78,7 @@ recordP :: Parser Record springP :: Parser Spring recordsP = recordP `sepBy` endOfLine -recordP = Record <$> (many1 springP <* " ") <*> (decimal `sepBy` ",") +recordP = Record <$> many1 springP <* " " <*> decimal `sepBy` "," springP = (Unknown <$ "?") <|> (Damaged <$ "#") <|> (Operational <$ ".") successfulParse :: Text -> [Record] diff --git a/advent15/Main.hs b/advent15/Main.hs index 7fa4789..384c88c 100644 --- a/advent15/Main.hs +++ b/advent15/Main.hs @@ -69,8 +69,8 @@ instructionP, removeP, insertP :: Parser Instruction instructionsP = instructionP `sepBy` "," instructionP = removeP <|> insertP -removeP = Remove <$> (many1 letter) <* "-" -insertP = Insert <$> ((many1 letter) <* "=") <*> decimal +removeP = Remove <$> many1 letter <* "-" +insertP = Insert <$> many1 letter <* "=" <*> decimal successfulParse :: Text -> [Instruction] successfulParse input = diff --git a/advent18/Main.hs b/advent18/Main.hs index 9e5a97c..97bcf6d 100644 --- a/advent18/Main.hs +++ b/advent18/Main.hs @@ -79,9 +79,9 @@ direction2P = choice [ U <$ "3" instructions2P = instruction2P `sepBy` endOfLine instruction2P = - instrify <$> (preambleP *> (AT.take 5)) <*> (direction2P <* ")") + instrify <$ preambleP <*> AT.take 5 <*> direction2P <* ")" -preambleP = (direction1P *> " " *> decimal <* " (#") +preambleP = direction1P *> " " *> decimal <* " (#" instrify :: Text -> Direction -> Instruction instrify h d = Instr d (fst $ fromRight (0, "") $ TR.hexadecimal h) diff --git a/advent19/Main.hs b/advent19/Main.hs index eed9e35..1a05fa5 100644 --- a/advent19/Main.hs +++ b/advent19/Main.hs @@ -217,13 +217,13 @@ partP :: Parser (Part Int) rulePartP = (,) <$> (rulesP <* endOfLine <* endOfLine) <*> partsP rulesP = M.fromList <$> ruleP `sepBy` endOfLine -ruleP = (,) <$> (nameP <* "{") <*> (ruleBodyP <* "}") +ruleP = (,) <$> nameP <* "{" <*> ruleBodyP <* "}" nameP = unpack <$> AT.takeWhile (inClass "a-z") -- many1 letter ruleBodyP = ruleElementP `sepBy` "," ruleElementP = withTestP <|> withoutTestP -withTestP = WithTest <$> (testP <* ":") <*> destinationP +withTestP = WithTest <$> testP <* ":" <*> destinationP withoutTestP = WithoutTest <$> destinationP testP = Test <$> registerP <*> comparatorP <*> decimal diff --git a/advent20/Main.hs b/advent20/Main.hs index 931c867..c3a9cf3 100644 --- a/advent20/Main.hs +++ b/advent20/Main.hs @@ -9,7 +9,7 @@ import Data.Attoparsec.Text hiding (take) import Control.Applicative import Data.List import qualified Data.Map.Strict as M -import Data.Map ((!)) +import Data.Map.Strict ((!)) import qualified Data.Sequence as Q import Data.Sequence ((|>), (><), Seq( (:|>), (:<|) ) ) import Control.Lens hiding (Level) @@ -179,13 +179,13 @@ moduleP, broadcastP, flipFlopP, conjunctionP :: Parser (Module, Name) nameP :: Parser Name configLinesP = configLineP `sepBy` endOfLine -configLineP = (,) <$> (moduleP <* " -> ") <*> (nameP `sepBy` ", ") +configLineP = (,) <$> moduleP <* " -> " <*> nameP `sepBy` ", " moduleP = broadcastP <|> flipFlopP <|> conjunctionP broadcastP = (Broadcast, "broadcaster") <$ "broadcaster" -flipFlopP = (FlipFlop False, ) <$> ("%" *> nameP) -conjunctionP = (Conjunction M.empty, ) <$> ("&" *> nameP) +flipFlopP = (FlipFlop False, ) <$ "%" <*> nameP +conjunctionP = (Conjunction M.empty, ) <$ "&" <*> nameP -- namesP = nameP `sepBy` ", " nameP = many1 letter diff --git a/advent22/Main.hs b/advent22/Main.hs index a80afe4..55b64b6 100644 --- a/advent22/Main.hs +++ b/advent22/Main.hs @@ -12,8 +12,8 @@ import Linear -- import Linear.V3 import Data.List import Data.Function -import qualified Data.Map as M -import Data.Map ((!)) +import qualified Data.Map.Strict as M +import Data.Map.Strict ((!)) import qualified Data.Set as S type Block = (V3 Int, V3 Int) @@ -126,7 +126,7 @@ blockP :: Parser Block vertexP :: Parser (V3 Int) blocksP = blockP `sepBy` endOfLine -blockP = cubify <$> (vertexP <* "~") <*> vertexP +blockP = cubify <$> vertexP <* "~" <*> vertexP where cubify (V3 x1 y1 z1) (V3 x2 y2 z2) = ( (V3 (min x1 x2) (min y1 y2) (min z1 z2)) , (V3 (max x1 x2) (max y1 y2) (max z1 z2)) diff --git a/advent23/Main.hs b/advent23/Main.hs index 457f42b..8298261 100644 --- a/advent23/Main.hs +++ b/advent23/Main.hs @@ -5,7 +5,7 @@ import qualified Debug.Trace as DT import AoC import Linear -- (V2(..), (^+^)) import qualified Data.Set as S -import qualified Data.Map as M +import qualified Data.Map.Strict as M import Control.Lens import Data.List (foldl') diff --git a/advent24/Main.hs b/advent24/Main.hs index 4e9cc78..b33b2f3 100644 --- a/advent24/Main.hs +++ b/advent24/Main.hs @@ -115,12 +115,14 @@ stoneP :: Parser Hailstone vertexP :: Parser (V3 Rational) stonesP = stoneP `sepBy` endOfLine -stoneP = Hailstone <$> (vertexP <* symbolP "@") <*> vertexP -vertexP = vecify <$> signed decimal <*> (symbolP "," *> signed decimal) <*> (symbolP "," *> signed decimal) +stoneP = Hailstone <$> vertexP <* symbolP "@" <*> vertexP +vertexP = vecify <$> signed decimal <* symbolP "," + <*> signed decimal <* symbolP "," + <*> signed decimal where vecify x y z = V3 (x % 1) (y % 1) (z % 1) symbolP :: Text -> Parser Text -symbolP s = (skipSpace *> string s) <* skipSpace +symbolP s = skipSpace *> string s <* skipSpace successfulParse :: Text -> [Hailstone] successfulParse input = diff --git a/advent25/Main.hs b/advent25/Main.hs index 0b9c02a..57d94f2 100644 --- a/advent25/Main.hs +++ b/advent25/Main.hs @@ -7,8 +7,8 @@ import qualified Data.Text.IO as TIO import Data.Attoparsec.Text hiding (take) -- import Control.Applicative import Data.List (foldl', unfoldr, sort, delete) -import qualified Data.Map as M -import Data.Map ((!)) +import qualified Data.Map.Strict as M +import Data.Map.Strict ((!)) import qualified Data.Set as S import Data.Set ((\\)) import qualified Data.Sequence as Q @@ -93,7 +93,7 @@ nodeP :: Parser (String, [String]) nameP :: Parser String graphP = M.fromList <$> nodeP `sepBy` endOfLine -nodeP = (,) <$> (nameP <* ": ") <*> (nameP `sepBy` " ") +nodeP = (,) <$> nameP <* ": " <*> nameP `sepBy` " " nameP = many1 letter