diff --git a/haskell/Pre.hs b/haskell/Pre.hs index 0350f6f..c55e865 100644 --- a/haskell/Pre.hs +++ b/haskell/Pre.hs @@ -70,7 +70,7 @@ import Data.Foldable1 import Data.Function import Data.Functor import Data.List (sortOn, transpose) -import Data.List.Extra (dropEnd, enumerate, notNull, splitOn) +import Data.List.Extra (dropEnd, enumerate, firstJust, notNull, splitOn) import Data.List.NonEmpty (NonEmpty ((:|)), nonEmpty, some1, tail, tails) import Data.Maybe import Data.Ord diff --git a/haskell/Puzzles/Day10.hs b/haskell/Puzzles/Day10.hs index 659f662..e57cf35 100644 --- a/haskell/Puzzles/Day10.hs +++ b/haskell/Puzzles/Day10.hs @@ -2,14 +2,49 @@ module Puzzles.Day10 (puzzle) where import Pre +import Data.IntMap qualified as IM + puzzle :: Puzzle puzzle = Puzzle { number = 10 - , parser = mempty + , parser = const $ flip sepEndBy newline do + void $ single '[' + lights <- some $ (single '.' $> Off) <|> (single '#' $> On) + void $ single ']' + void space1 + switches <- flip sepEndBy space1 do + void $ single '(' + r <- Switch <$> decimal `sepBy` single ',' + void $ single ')' + pure r + void $ single '{' + void $ decimal @_ @_ @_ @Int `sepBy` single ',' + void $ single '}' + pure (Lights $ IM.fromList $ zip [0 ..] lights, switches) , parts = - [ \() -> - () + [ sum . map \(lights, switches) -> + maybe (error "no solution") length + . firstJust (firstJust (\(s, ls) -> guard (allOff ls) $> s)) + $ flip iterate [([], lights)] \ls -> + concatMap (\s -> map (\(ss, l) -> (s : ss, applySwitch s l)) ls) switches ] , extraTests = mempty } + +data Light = On | Off + deriving (Eq, Ord, Show) +flipLight :: Light -> Light +flipLight = \case + On -> Off + Off -> On + +newtype Lights = Lights (IM.IntMap Light) + deriving (Eq, Ord, Show) +allOff :: Lights -> Bool +allOff (Lights ls) = all (== Off) $ map snd $ IM.toList ls + +newtype Switch = Switch [Int] + deriving (Eq, Ord, Show) +applySwitch :: Switch -> Lights -> Lights +applySwitch (Switch ss) (Lights ls) = Lights $ foldl' (flip $ IM.adjust flipLight) ls ss diff --git a/outputs/real/10/1 b/outputs/real/10/1 new file mode 100644 index 0000000..8410b8b --- /dev/null +++ b/outputs/real/10/1 @@ -0,0 +1 @@ +473