Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8b14362

Browse files
committed
explicit imports for day 18
1 parent aecdda8 commit 8b14362

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

‎src/AOC2017/Day18.hs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,18 @@
88
module AOC2017.Day18 (day18a, day18b) where
99

1010
import AOC2017.Types (Challenge)
11-
import AOC2017.Util.Accum
11+
import AOC2017.Util.Accum (AccumT(..), execAccumT, look, add)
1212
import AOC2017.Util.Prompt ()
1313
import AOC2017.Util.Tape (Tape(..), HasTape(..), move, unsafeTape)
1414
import Control.Applicative (many, empty)
1515
import Control.Lens (makeClassy, use, at, non, (%=), use, (.=), (<>=), zoom)
1616
import Control.Monad (guard, when)
17-
import Control.Monad.Prompt
18-
import Control.Monad.State
19-
import Control.Monad.Trans.Class (lift)
17+
import Control.Monad.Prompt (MonadPrompt(..), runPromptM)
18+
import Control.Monad.State (MonadState(..), execStateT, State, evalState)
2019
import Control.Monad.Trans.Maybe (MaybeT(..))
21-
import Control.Monad.Writer
20+
import Control.Monad.Writer (First(..), Last(..), MonadWriter(..), WriterT(..), Writer, execWriter)
2221
import Data.Char (isAlpha)
23-
import Data.Foldable
22+
import Data.Coerce (coerce)
2423
import Data.Kind (Type)
2524
import Data.Maybe (fromJust)
2625
import qualified Data.Map as M
@@ -42,7 +41,6 @@ data Op = OSnd Addr
4241
| OBin (Int -> Int -> Int) Char Addr
4342
| ORcv Char
4443
| OJgz Addr Addr
45-
instance Show Op where show _ = "Op"
4644

4745
parseOp :: String -> Op
4846
parseOp inp = case words inp of
@@ -78,7 +76,6 @@ sndMachine = prompt . CSnd
7876
data ProgState = PS { _psTape :: Tape Op
7977
, _psRegs :: M.Map Char Int
8078
}
81-
deriving Show
8279
makeClassy ''ProgState
8380

8481
-- | Single step through program tape.
@@ -116,23 +113,20 @@ stepTape = use (psTape . tFocus) >>= \case
116113

117114
-- | Context in which to interpret Command for Part A
118115
--
119-
-- State parameter is the most recent sent item. Writer parameter is all
120-
-- of the Rcv'd items.
121-
--
122-
-- State should probably be Accum instead, but Accum is in any usable
123-
-- version of transformers yet.
124-
type PartA = AccumT (Last Int) (Writer [Int])
116+
-- Accum parameter is the most recent sent item. Writer parameter is the
117+
-- first Rcv'd item.
118+
type PartA = AccumT (Last Int) (Writer (First Int))
125119
execPartA :: PartA a -> Int
126-
execPartA = head . snd . runWriter . flip execAccumT mempty
120+
execPartA = fromJust . getFirst . execWriter . flip execAccumT mempty
127121

128122
-- | Interpet Command for Part A
129123
interpretA :: Command a -> PartA a
130124
interpretA = \case
131125
CRcv x -> do
132126
when (x /= 0) $
133-
lift .tell . toList =<< look
127+
tell . coerce =<< look
134128
return x
135-
CSnd x -> add (Last (Just x))
129+
CSnd x -> add (pure x)
136130

137131
day18a :: Challenge
138132
day18a = show
@@ -164,6 +158,9 @@ interpretB = \case
164158
[] -> empty
165159
x:xs -> put xs >> return x
166160

161+
-- runTapeB :: ProgState -> PartB Thread (ProgState, [Int])
162+
-- runTapeB = zoom tBuffer . runWriterT . runPromptM interpretB . execStateT stepTape
163+
167164
-- | Single step through a thread. Nothing = either the thread terminates,
168165
-- or requires extra input.
169166
stepThread :: PartB Thread [Int]

‎src/AOC2017/Util/Accum.hs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
{-# LANGUAGE CPP #-}
22
{-# LANGUAGE AutoDeriveTypeable #-}
3+
{-# LANGUAGE FlexibleContexts #-}
4+
{-# LANGUAGE UndecidableInstances #-}
5+
{-# LANGUAGE FlexibleInstances #-}
6+
{-# LANGUAGE MultiParamTypeClasses #-}
37
-----------------------------------------------------------------------------
48
-- |
59
-- Module : Control.Monad.Trans.Accum
@@ -66,6 +70,11 @@ import Control.Monad.Signatures
6670
import Data.Monoid
6771
#endif
6872

73+
import qualified Control.Monad.Writer as MTL
74+
75+
instance (Monoid v, MTL.MonadWriter w m) => MTL.MonadWriter w (AccumT v m) where
76+
tell = lift . MTL.tell
77+
6978
-- ---------------------------------------------------------------------------
7079
-- | An accumulation monad parameterized by the type @w@ of output to accumulate.
7180
--

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /