6
6
-- Portability : non-portable
7
7
--
8
8
-- 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
+ )
10
13
where
11
14
12
- -- (
13
- -- day24a,
14
- -- day24b,
15
- -- )
16
-
17
- import AOC.Common (asString , loopEither , parseBinary )
15
+ import AOC.Common (asString , parseBinary )
18
16
import AOC.Common.Parser (CharParser , pAlphaNumWord , parseMaybe' , sepByLines , tokenAssoc )
19
17
import AOC.Solver (noFail , type (:~> ) (.. ))
20
- import Control.Applicative (Alternative (empty , many ) )
18
+ import Control.Applicative (Alternative (empty , many , (<|>) ), asum )
21
19
import Control.DeepSeq (NFData )
22
- import Control.Lens
20
+ import Control.Lens ( (%=) , (.=) )
23
21
import 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 )
26
23
import Data.Bifunctor (Bifunctor (second ))
27
- import Data.Either
24
+ import Data.Either ( lefts )
28
25
import Data.Foldable (Foldable (toList ))
29
- import Data.Functor
30
26
import Data.Generics.Labels ()
31
27
import Data.IntMap (IntMap )
32
28
import qualified Data.IntMap as IM
@@ -35,8 +31,8 @@ import Data.List.NonEmpty (NonEmpty (..))
35
31
import qualified Data.List.NonEmpty as NE
36
32
import Data.Map (Map )
37
33
import qualified Data.Map as M
34
+ import Data.Maybe (listToMaybe )
38
35
import Data.Tuple (swap )
39
- import Debug.Trace
40
36
import GHC.Generics (Generic )
41
37
import qualified Text.Megaparsec as P
42
38
import qualified Text.Megaparsec.Char as P
@@ -170,25 +166,19 @@ nameGate ::
170
166
Map (Gate String ) String ->
171
167
Int ->
172
168
Gate (Either Int VarBit ) ->
173
- LogicT ( StateT NameState Maybe ) ()
169
+ StateT NameState [] ()
174
170
nameGate avail ng g0 = do
175
171
NS {.. } <- get
176
172
let gate = either (nsNames IM. ! ) showVarBit <$> g0
177
173
case applySwaps nsRenames <$> M. lookup gate avail of
178
174
Nothing -> empty
179
175
Just here ->
180
176
(# nsNames %= IM. insert ng here)
181
- `interleave` foldr
182
- interleave
183
- empty
177
+ <|> asum
184
178
[ put (NS renames (IM. insert ng there nsNames) True )
185
179
| not nsFound
186
- , here `M.notMember` nsRenames
187
- , here `notElem` nsNames
188
180
, there <- toList avail
189
181
, here /= there
190
- , there `M.notMember` nsRenames
191
- , there `notElem` nsNames
192
182
, let renames = M. fromList [(here, there), (there, here)] <> nsRenames
193
183
]
194
184
where
@@ -197,8 +187,8 @@ nameGate avail ng g0 = do
197
187
198
188
nameTree ::
199
189
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
202
192
where
203
193
s0 = NS M. empty IM. empty False
204
194
(outGates, gates) = unrollAdderTree 44
@@ -212,5 +202,5 @@ day24b =
212
202
MkSol
213
203
{ sParse = fmap snd . sParse day24a
214
204
, sShow = intercalate " ,"
215
- , sSolve = fmap M. keys . nameTree . M. fromList
205
+ , sSolve = fmap M. keys . listToMaybe . nameTree . M. fromList
216
206
}
0 commit comments