diff options
| author | Tom Smeding <tom@tomsmeding.com> | 2026-12-05 20:14:38 +0100 |
|---|---|---|
| committer | Tom Smeding <tom@tomsmeding.com> | 2026-12-05 20:15:21 +0100 |
| commit | 91c4402d945cb08abcb7523ccf19155ef10aeea2 (patch) | |
| tree | 03c709a422396cac44bc93ed6b76de41c90964c2 /2026 | |
| parent | 50feadead454fab72198db027ee7d7f6a404800a (diff) | |
Diffstat (limited to '2026')
| -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) |
