66-- Portability : non-portable
77--
88-- Day 24. See "AOC.Solver" for the types used in this module!
9- module AOC2024.Day24
9+ module AOC2024.Day24 (
10+ day24a ,
11+ day24b ,
12+ )
1013where
1114
12- -- (
13- -- day24a,
14- -- day24b,
15- -- )
16-
17- import AOC.Common (asString , loopEither , parseBinary )
15+ import AOC.Common (asString , parseBinary )
1816import AOC.Common.Parser (CharParser , pAlphaNumWord , parseMaybe' , sepByLines , tokenAssoc )
1917import AOC.Solver (noFail , type (:~> ) (.. ))
20- import Control.Applicative (Alternative (empty , many ) )
18+ import Control.Applicative (Alternative (empty , many , (<|>) ), asum )
2119import Control.DeepSeq (NFData )
22- import Control.Lens
20+ import Control.Lens ( (%=) , (.=) )
2321import Control.Monad.Free (Free , MonadFree (wrap ), iterA )
24- import Control.Monad.Logic
25- import Control.Monad.State
22+ import Control.Monad.State (MonadState (get , put ), State , StateT , execStateT , runState )
2623import Data.Bifunctor (Bifunctor (second ))
27- import Data.Either
24+ import Data.Either ( lefts )
2825import Data.Foldable (Foldable (toList ))
29- import Data.Functor
3026import Data.Generics.Labels ()
3127import Data.IntMap (IntMap )
3228import qualified Data.IntMap as IM
@@ -35,8 +31,8 @@ import Data.List.NonEmpty (NonEmpty (..))
3531import qualified Data.List.NonEmpty as NE
3632import Data.Map (Map )
3733import qualified Data.Map as M
34+ import Data.Maybe (listToMaybe )
3835import Data.Tuple (swap )
39- import Debug.Trace
4036import GHC.Generics (Generic )
4137import qualified Text.Megaparsec as P
4238import qualified Text.Megaparsec.Char as P
@@ -170,25 +166,19 @@ nameGate ::
170166 Map (Gate String ) String ->
171167 Int ->
172168 Gate (Either Int VarBit ) ->
173- LogicT ( StateT NameState Maybe ) ()
169+ StateT NameState [] ()
174170nameGate avail ng g0 = do
175171 NS {.. } <- get
176172 let gate = either (nsNames IM. ! ) showVarBit <$> g0
177173 case applySwaps nsRenames <$> M. lookup gate avail of
178174 Nothing -> empty
179175 Just here ->
180176 (# nsNames %= IM. insert ng here)
181- `interleave` foldr
182- interleave
183- empty
177+ <|> asum
184178 [ put (NS renames (IM. insert ng there nsNames) True )
185179 | not nsFound
186- , here `M.notMember` nsRenames
187- , here `notElem` nsNames
188180 , there <- toList avail
189181 , here /= there
190- , there `M.notMember` nsRenames
191- , there `notElem` nsNames
192182 , let renames = M. fromList [(here, there), (there, here)] <> nsRenames
193183 ]
194184 where
@@ -197,8 +187,8 @@ nameGate avail ng g0 = do
197187
198188nameTree ::
199189 Map (Gate String ) String ->
200- Maybe ( Map String String )
201- nameTree avail = nsRenames <$> execStateT (observeT ( traverse go outGates) ) s0
190+ [ Map String String ]
191+ nameTree avail = nsRenames <$> execStateT (traverse go outGates) s0
202192 where
203193 s0 = NS M. empty IM. empty False
204194 (outGates, gates) = unrollAdderTree 44
@@ -212,5 +202,5 @@ day24b =
212202 MkSol
213203 { sParse = fmap snd . sParse day24a
214204 , sShow = intercalate " ,"
215- , sSolve = fmap M. keys . nameTree . M. fromList
205+ , sSolve = fmap M. keys . listToMaybe . nameTree . M. fromList
216206 }
0 commit comments