Solve day 5 part 1

This commit is contained in:
George Thomas 2025-12-05 13:16:33 +00:00
parent 3742e1d254
commit 62be96ed57
2 changed files with 31 additions and 3 deletions

View File

@ -1,15 +1,42 @@
module Puzzles.Day5 (puzzle) where
import Control.Monad
import Data.Functor
import Data.List.Extra
import Data.Maybe
import Data.Text.Lazy qualified as TL
import Puzzle
import Text.Megaparsec hiding (some)
import Text.Megaparsec.Char
import Text.Megaparsec.Char.Lexer qualified as Lex
puzzle :: Puzzle
puzzle =
Puzzle
{ number = 5
, parser = pure ()
, parser = do
ranges <- flip sepEndBy newline $ Range <$> Lex.decimal <* single '-' <*> Lex.decimal
void newline
vals <- sepEndBy Lex.decimal newline
pure (ranges, vals)
, parts =
[ \() ->
"TODO"
[ \(ranges, vals) ->
TL.show
. length
. map fst
. filter (notNull . snd)
$ map
(\v -> (v, mapMaybe (\r -> guard (isInRange v r) $> r) ranges))
vals
]
, extraTests = mempty
}
data Range = Range
{ lower :: Int
, upper :: Int
}
deriving (Eq, Ord, Show)
isInRange :: Int -> Range -> Bool
isInRange n r = n >= r.lower && n <= r.upper

1
outputs/real/5/1 Normal file
View File

@ -0,0 +1 @@
874