Solve 2023-05
This commit is contained in:
parent
a29eedb4ce
commit
0a5cf30889
5 changed files with 87 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
|||
module AoC (
|
||||
defaultMain,
|
||||
runAoC,
|
||||
runAoCExample,
|
||||
module AoC.Day,
|
||||
module AoC.Year,
|
||||
) where
|
||||
|
|
@ -20,6 +21,11 @@ runAoC y d = do
|
|||
riddle <- loadRiddle y d
|
||||
solve y d riddle
|
||||
|
||||
runAoCExample :: (MonadIO m) => Year -> Day -> m (Either Text Solution)
|
||||
runAoCExample y d = do
|
||||
riddle <- loadExample y d
|
||||
solve y d riddle
|
||||
|
||||
solve :: (MonadIO m) => Year -> Day -> Riddle -> m (Either Text Solution)
|
||||
solve Y2023 = Y2023.solve
|
||||
solve y = \_ _ -> pure $ Left $ show y <> ": not implementedi"
|
||||
|
|
|
|||
|
|
@ -1,6 +1,46 @@
|
|||
module AoC.Y2023.D05 (solve) where
|
||||
module AoC.Y2023.D05 where
|
||||
|
||||
import AoC.Parser
|
||||
import AoC.Riddle
|
||||
import Text.Megaparsec (choice, sepBy1)
|
||||
|
||||
import Text.Megaparsec.Char (eol, space1, string)
|
||||
import Text.Megaparsec.Char.Lexer qualified as L
|
||||
|
||||
newtype Seeds = Seeds [Int]
|
||||
deriving newtype (Show)
|
||||
|
||||
-- | Parse a list of seeds
|
||||
--
|
||||
-- >>> parseText pSeeds "seeds: 79 14 55 13"
|
||||
-- Right [79,14,55,13]
|
||||
pSeeds :: Parser Seeds
|
||||
pSeeds = Seeds <$> (string "seeds: " *> L.decimal `sepBy1` ws)
|
||||
|
||||
newtype Mapping = Mapping [((Int, Int), (Int, Int))]
|
||||
|
||||
pRanges :: Parser [((Int, Int), (Int, Int))]
|
||||
pRanges = do
|
||||
pRange `sepBy1` eol
|
||||
where
|
||||
pRange = do
|
||||
x0 <- L.decimal <* space1
|
||||
x1 <- L.decimal <* space1
|
||||
n <- L.decimal
|
||||
pure ((x0, x0 + n), (x1, x1 + n))
|
||||
|
||||
pMapping :: Parser Mapping
|
||||
pMapping = do
|
||||
_ <-
|
||||
choice
|
||||
[ string "soil-to-fertilizer map:"
|
||||
, string "fertilizer-to-water map:"
|
||||
, string "water-to-light map:"
|
||||
, string "light-to-temperature map:"
|
||||
, string "light-to-temperature map:"
|
||||
]
|
||||
<* eol
|
||||
Mapping . fromList <$> pRanges `sepBy1` eol
|
||||
|
||||
solve :: (MonadIO m) => Text -> m (Either Text Solution)
|
||||
solve _ = pure $ Left "not yet implemented"
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue