-
Notifications
You must be signed in to change notification settings - Fork 5
/
Day18.hs
220 lines (192 loc) · 6 KB
/
Day18.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
module AOC2017.Day18 (day18a, day18b) where
import AOC2017.Types (Challenge)
import AOC2017.Util.Accum
import Control.Applicative
import Control.Lens
import Control.Monad
import Control.Monad.Fail
import Control.Monad.Operational
import Control.Monad.State (MonadState, StateT(..), State, execStateT, evalState)
import Control.Monad.Trans.Maybe (MaybeT(..))
import Control.Monad.Writer
import Data.Char (isAlpha)
import Data.Kind (Type)
import Data.Maybe (fromJust)
import Data.Monoid (First(..), Last(..))
import Data.Type.Disjunction
import qualified Data.List.PointedList as P
import qualified Data.Map as M
import qualified Data.Vector.Sized as V
{-
******************
* The Language *
******************
-}
type Addr = Either Char Int
addr :: String -> Addr
addr [c] | isAlpha c = Left c
addr str = Right (read str)
data Op = OSnd Addr
| OBin (Int -> Int -> Int) Char Addr
| ORcv Char
| OJgz Addr Addr
parseOp :: String -> Op
parseOp inp = case words inp of
"snd":(addr->c):_ -> OSnd c
"set":(x:_):(addr->y):_ -> OBin (const id) x y
"add":(x:_):(addr->y):_ -> OBin (+) x y
"mul":(x:_):(addr->y):_ -> OBin (*) x y
"mod":(x:_):(addr->y):_ -> OBin mod x y
"rcv":(x:_):_ -> ORcv x
"jgz":(addr->x):(addr->y):_ -> OJgz x y
_ -> error "Bad parse"
parse :: String -> P.PointedList Op
parse = fromJust . P.fromList . map parseOp . lines
{-
**************************
* The Abstract Machine *
**************************
-}
data Memory :: Type -> Type where
MGet :: Char -> Memory Int
MSet :: Char -> Int -> Memory ()
MMov :: Int -> Memory ()
MPk :: Memory Op
-- | Abstract data type describing "IO" available to the abstract machine
data Command :: Type -> Type where
CRcv :: Int -> Command Int -- ^ input is current value of buffer
CSnd :: Int -> Command () -- ^ input is thing being sent
type Machine = Program (Memory :|: Command)
cRcv :: Int -> Machine Int
cRcv = singleton . R . CRcv
cSnd :: Int -> Machine ()
cSnd = singleton . R . CSnd
cGet :: Char -> Machine Int
cGet = singleton . L . MGet
cSet :: Char -> Int -> Machine ()
cSet r = singleton . L . MSet r
cMov :: Int -> Machine ()
cMov = singleton . L . MMov
cPk :: Machine Op
cPk = singleton $ L MPk
data ProgState = PS { _psTape :: P.PointedList Op
, _psRegs :: M.Map Char Int
}
makeClassy ''ProgState
-- | Single step through program tape.
stepTape :: Machine ()
stepTape = cPk >>= \case
OSnd x -> do
cSnd =<< addrVal x
cMov 1
OBin f x y -> do
yVal <- addrVal y
cSet x . (`f` yVal) =<< cGet x
cMov 1
ORcv x -> do
y <- cRcv =<< cGet x
cSet x y
cMov 1
OJgz x y -> do
xVal <- addrVal x
cMov =<< if xVal > 0
then addrVal y
else return 1
where
addrVal (Left r ) = cGet r
addrVal (Right x) = return x
interpMem
:: (MonadState s m, MonadPlus m, HasProgState s)
=> Memory a
-> m a
interpMem = \case
MGet c -> use (psRegs . at c . non 0)
MSet c x -> psRegs . at c . non 0 .= x
MMov n -> do
Just t' <- P.moveN n <$> use psTape
psTape .= t'
MPk -> use (psTape . P.focus)
{-
************************
* Context for Part A *
************************
-}
execPartA
:: MaybeT (StateT ProgState (AccumT (Last Int) (Writer (First Int)))) a
-> ProgState
-> Int
execPartA p s = fromJust . getFirst . execWriter
. flip execAccumT mempty
. flip execStateT s
. runMaybeT
$ p
-- | Interpet Command for Part A
interpA
:: (MonadAccum (Last Int) m, MonadWriter (First Int) m)
=> Command a
-> m a
interpA = \case
CRcv x -> do
when (x /= 0) $
tell . First . getLast =<< look
return x
CSnd x ->
add (Last (Just x))
day18a :: Challenge
day18a = show
. execPartA (many . interpretWithMonad (interpMem >|< interpA) $ stepTape)
. (`PS` M.empty)
. parse
{-
************************
* Context for Part B *
************************
-}
data Thread = T { _tState :: ProgState
, _tBuffer :: [Int]
}
makeClassy ''Thread
instance HasProgState Thread where
progState = tState
-- | Interpet Command for Part B, with an [Int] writer side-channel
interpB
:: (MonadFail m, MonadState s m, HasThread s)
=> Command a
-> WriterT [Int] m a
interpB = \case
CSnd x -> tell [x]
CRcv _ -> do
x:xs <- use tBuffer
tBuffer .= xs
return x
type MultiState = V.Vector 2 Thread
-- | Single step through both threads. Nothing = both threads terminate
stepThreads
:: MaybeT (State MultiState) Int
stepThreads = do
outA <- execWriterT . zoom (V.ix 0) $
many $ interpretWithMonad (interpMem >|< interpB) stepTape
outB <- execWriterT . zoom (V.ix 1) $
many $ interpretWithMonad (interpMem >|< interpB) stepTape
V.ix 0 . tBuffer .= outB
V.ix 1 . tBuffer .= outA
guard . not $ null outA && null outB
return $ length outB
day18b :: Challenge
day18b (parse->t) = show . sum . concat
. evalState (runMaybeT (many stepThreads))
$ ms
where
Just ms = V.fromList [ T (PS t (M.singleton 'p' 0)) []
, T (PS t (M.singleton 'p' 1)) []
]