Skip to content

Commit 0b4243c

Browse files
committed
more day 24 cleanup, from 1.7s to 10ms
1 parent 7e3f0b0 commit 0b4243c

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

2024/AOC2024/Day24.hs

+15-25
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,23 @@
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+
)
1013
where
1114

12-
-- (
13-
-- day24a,
14-
-- day24b,
15-
-- )
16-
17-
import AOC.Common (asString, loopEither, parseBinary)
15+
import AOC.Common (asString, parseBinary)
1816
import AOC.Common.Parser (CharParser, pAlphaNumWord, parseMaybe', sepByLines, tokenAssoc)
1917
import AOC.Solver (noFail, type (:~>) (..))
20-
import Control.Applicative (Alternative (empty, many))
18+
import Control.Applicative (Alternative (empty, many, (<|>)), asum)
2119
import Control.DeepSeq (NFData)
22-
import Control.Lens
20+
import Control.Lens ((%=), (.=))
2321
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)
2623
import Data.Bifunctor (Bifunctor (second))
27-
import Data.Either
24+
import Data.Either (lefts)
2825
import Data.Foldable (Foldable (toList))
29-
import Data.Functor
3026
import Data.Generics.Labels ()
3127
import Data.IntMap (IntMap)
3228
import qualified Data.IntMap as IM
@@ -35,8 +31,8 @@ import Data.List.NonEmpty (NonEmpty (..))
3531
import qualified Data.List.NonEmpty as NE
3632
import Data.Map (Map)
3733
import qualified Data.Map as M
34+
import Data.Maybe (listToMaybe)
3835
import Data.Tuple (swap)
39-
import Debug.Trace
4036
import GHC.Generics (Generic)
4137
import qualified Text.Megaparsec as P
4238
import 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 [] ()
174170
nameGate 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

198188
nameTree ::
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

Comments
 (0)