diff options
Diffstat (limited to '2026/5.hs')
| -rw-r--r-- | 2026/5.hs | 17 |
1 files changed, 10 insertions, 7 deletions
@@ -1,6 +1,7 @@ {-# OPTIONS -Wno-x-partial #-} module Main where +import Data.Foldable (toList) import Data.List (foldl') import Data.Set qualified as Set import Data.Set (Set) @@ -33,15 +34,17 @@ press n btn = Bksp -> n `quot` 10 in if n' >= 10000 then n else n' -search :: [Btn] -> Set Int -> Set Int -> Set Int -search _ seen front | Set.null front = seen -search btns seen front = - let seen' = seen <> front - news = Set.fromList [press n b | n <- Set.toList front, b <- btns] Set.\\ seen' - in search btns seen' news +bfs :: (Ord a, Foldable t) => (a -> t a) -> Set a -> Set a +bfs f = go mempty + where + go seen front | Set.null front = seen + go seen front = + let seen' = seen <> front + news = Set.fromList [y | x <- Set.toList front, y <- toList (f x)] Set.\\ seen' + in go seen' news main :: IO () main = do btns <- map (parseBtn . init . tail) . words <$> getContents print . sum . Set.toList $ Set.fromList . map (foldl' press 0) $ cartprod 3 btns - print . sum . Set.toList $ search btns mempty (Set.singleton 0) + print . sum . Set.toList $ bfs (\n -> [press n b | b <- btns]) (Set.singleton 0) |
