-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.hs
190 lines (158 loc) · 6.39 KB
/
run.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
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NamedFieldPuns #-}
module Main (main) where
import Control.Monad.State ( StateT(..)
, gets
, modify
, lift
, get
, MonadState )
import Data.Bool (bool)
import Data.HashMap.Strict (HashMap)
import qualified Data.HashMap.Strict as HashMap
import Data.Maybe (maybe)
import Pipes
import qualified Pipes.Prelude as P
type Program = HashMap Integer Integer
data ExecInfo = ExecInfo { pc :: Integer
, halted :: Bool
, memory :: Program
, relativeBase :: Integer }
deriving Show
data Address = Absolute Integer | Relative Integer
deriving Show
data Instruction = Add Param Param Address
| Mul Param Param Address
| Input Address
| Output Param
| JumpNonZero Param Param
| JumpZero Param Param
| LessThan Param Param Address
| Equals Param Param Address
| ModifyRelativeBase Param
| Exit
deriving Show
data Param = Position Address | Immediate Integer
deriving Show
execInfo :: Program -> ExecInfo
execInfo program = ExecInfo { pc = 0
, memory = program
, relativeBase = 0
, halted = False }
resolveAddress :: MonadState ExecInfo m => Address -> m Integer
resolveAddress (Absolute a) = pure a
resolveAddress (Relative v) = (v +) <$> gets relativeBase
rawValAt :: MonadState ExecInfo m => Address -> m Integer
rawValAt addr =
maybe 0 id <$> (HashMap.lookup <$> resolveAddress addr <*> gets memory)
modifyPc :: MonadState ExecInfo m => (Integer -> Integer) -> m ()
modifyPc f = modify (\s@ExecInfo { pc } -> s { pc = f pc })
modifyRelativeBase :: MonadState ExecInfo m => (Integer -> Integer) -> m ()
modifyRelativeBase f =
modify (\s@ExecInfo { relativeBase } -> s { relativeBase = f relativeBase })
halt :: MonadState ExecInfo m => m ()
halt = modify (\s -> s { halted = True })
paramValue :: MonadState ExecInfo m => Param -> m Integer
paramValue (Immediate x) = pure x
paramValue (Position x) = rawValAt x
writeMemory :: MonadState ExecInfo m => Address -> Integer -> m ()
writeMemory addr val = do
addr' <- resolveAddress addr
modify (\s@ExecInfo { memory } -> s { memory = HashMap.insert addr' val memory })
parseAll :: String -> Program
parseAll input = HashMap.fromList $ zip [0..] . read $ '[' : input ++ "]"
consumeInstruction :: MonadState ExecInfo m => m Instruction
consumeInstruction = do
rawInstruction <- rawValAt . Absolute =<< gets pc
modifyPc (+ 1)
case parseFullOpCode rawInstruction of
(1, [pmx, pmy, pmz]) -> Add <$> consumeParam pmx
<*> consumeParam pmy
<*> consumeAddress pmz
(2, [pmx, pmy, pmz]) -> Mul <$> consumeParam pmx
<*> consumeParam pmy
<*> consumeAddress pmz
(3, pmx:_) -> Input <$> consumeAddress pmx
(4, pmx:_) -> Output <$> consumeParam pmx
(5, pmx:pmy:_) -> JumpNonZero <$> consumeParam pmx <*> consumeParam pmy
(6, pmx:pmy:_) -> JumpZero <$> consumeParam pmx <*> consumeParam pmy
(7, [pmx, pmy, pmz]) -> LessThan <$> consumeParam pmx
<*> consumeParam pmy
<*> consumeAddress pmz
(8, [pmx, pmy, pmz]) -> Equals <$> consumeParam pmx
<*> consumeParam pmy
<*> consumeAddress pmz
(9, pmx:_) -> ModifyRelativeBase <$> consumeParam pmx
(99, _) -> pure Exit
_ -> do
state <- get
error $ unlines $
[ "Invalid opcode (instr: " ++ show rawInstruction ++ "),"
, " parsed instruction: " ++ show (parseFullOpCode rawInstruction)
, " state is: " ++ show state ]
parseFullOpCode :: Integer -> (Integer, [Integer])
parseFullOpCode x =
let opCode = x `mod` 100
paramModes = map (\d -> (x `div` d) `mod` 10) [100, 1000, 10000]
in
(opCode, paramModes)
exec :: Monad m => Program -> Pipe Integer Integer m ()
exec program = runStateT go (execInfo program) *> pure ()
where go = do
execNext
h <- gets halted
case h of
True -> pure ()
False -> go
execNext :: Monad m => StateT ExecInfo (Pipe Integer Integer m) ()
execNext = do
instruction <- consumeInstruction
case instruction of
Add px py outAddr -> binOp (+) px py >>= writeMemory outAddr
Mul px py outAddr -> binOp (*) px py >>= writeMemory outAddr
Input outAddr -> lift await >>= writeMemory outAddr
Output px -> paramValue px >>= lift . yield
JumpNonZero px pjump -> do
jumpAddr <- paramValue pjump
(/= 0) <$> paramValue px >>= branch jumpAddr
JumpZero px pjump ->do
jumpAddr <- paramValue pjump
(== 0) <$> paramValue px >>= branch jumpAddr
LessThan px py outAddr ->
comparison (<) px py >>= writeMemory outAddr
Equals px py outAddr ->
comparison (==) px py >>= writeMemory outAddr
ModifyRelativeBase px -> paramValue px >>= modifyRelativeBase . (+)
Exit -> halt
branch :: MonadState ExecInfo m => Integer -> Bool -> m ()
branch jumpAddr = bool (pure ()) (modifyPc (const jumpAddr))
comparison :: MonadState ExecInfo m
=> (Integer -> Integer -> Bool)
-> Param
-> Param
-> m Integer
comparison comp = binOp (\x y -> bool 0 1 (comp x y))
binOp :: MonadState ExecInfo m
=> (Integer -> Integer -> a)
-> Param
-> Param
-> m a
binOp op px py = op <$> paramValue px <*> paramValue py
consumeValue :: MonadState ExecInfo m => m Integer
consumeValue = (gets pc >>= rawValAt . Absolute) <* modifyPc (+ 1)
consumeAddress :: MonadState ExecInfo m => Integer -> m Address
consumeAddress 0 = Absolute <$> consumeValue
consumeAddress 2 = Relative <$> consumeValue
consumeAddress _ = error "Impossible"
consumeParam :: MonadState ExecInfo m => Integer -> m Param
consumeParam 1 = Immediate <$> consumeValue
consumeParam x = Position <$> consumeAddress x
part1 :: Program -> Integer
part1 program = head . P.toList $ yield 1 >-> exec program
part2 :: Program -> Integer
part2 program = head . P.toList $ yield 2 >-> exec program
main :: IO ()
main = do
input <- parseAll <$> readFile "input.txt"
print (part1 input)
print (part2 input)