Day 9: Even more refactor
This commit is contained in:
parent
84bd9e7c08
commit
b651afdb29
|
@ -3,7 +3,7 @@ module Intcode
|
||||||
state)
|
state)
|
||||||
, step
|
, step
|
||||||
, tapePreprocess
|
, tapePreprocess
|
||||||
, TMAction(Continue, Output, Halt)
|
, TMOutState(Continue, AwaitInput, Halt)
|
||||||
, execSteps
|
, execSteps
|
||||||
) where
|
) where
|
||||||
|
|
||||||
|
@ -23,9 +23,9 @@ import Data.Vector as V hiding
|
||||||
)
|
)
|
||||||
import qualified Data.Vector as V ((++), last)
|
import qualified Data.Vector as V ((++), last)
|
||||||
|
|
||||||
data TMAction
|
data TMOutState
|
||||||
= Continue
|
= Continue
|
||||||
| Output
|
| AwaitInput
|
||||||
| Halt
|
| Halt
|
||||||
deriving (Enum, Eq, Show)
|
deriving (Enum, Eq, Show)
|
||||||
|
|
||||||
|
@ -41,8 +41,8 @@ data TuringMachine =
|
||||||
, pointer :: Integer
|
, pointer :: Integer
|
||||||
, pointerOffset :: Integer
|
, pointerOffset :: Integer
|
||||||
, output :: [Integer]
|
, output :: [Integer]
|
||||||
, input :: [Integer]
|
, input :: Maybe Integer
|
||||||
, state :: TMAction
|
, state :: TMOutState
|
||||||
}
|
}
|
||||||
deriving (Show)
|
deriving (Show)
|
||||||
|
|
||||||
|
@ -88,7 +88,7 @@ step tm =
|
||||||
case op of
|
case op of
|
||||||
"1" -> tmBinop (+)
|
"1" -> tmBinop (+)
|
||||||
"2" -> tmBinop (*)
|
"2" -> tmBinop (*)
|
||||||
"3" -> (getNewTM $ head $ input tm) {input = L.tail $ input tm}
|
"3" -> maybe tm{state = AwaitInput} getNewTM (input tm)
|
||||||
"4" -> tmn {output = V.last params : output tm}
|
"4" -> tmn {output = V.last params : output tm}
|
||||||
"5" ->
|
"5" ->
|
||||||
tm
|
tm
|
||||||
|
@ -133,7 +133,7 @@ step tm =
|
||||||
{-without the following DeepSeq call, thunks build up eternally and
|
{-without the following DeepSeq call, thunks build up eternally and
|
||||||
the vectors won’t be garbage collected: >4GB RAM usage, god knows
|
the vectors won’t be garbage collected: >4GB RAM usage, god knows
|
||||||
how much with larger tapes (my laptop crashed), now it’s a cozy ~20mB-}
|
how much with larger tapes (my laptop crashed), now it’s a cozy ~20mB-}
|
||||||
getNewTM x = tmn {tape = DeepSeq.force (tape tm // [(target, x)])}
|
getNewTM x = tmn {tape = DeepSeq.force (tape tm // [(target, x)]), state = Continue}
|
||||||
target =
|
target =
|
||||||
fromInteger $
|
fromInteger $
|
||||||
case m !! (length params - 1) of
|
case m !! (length params - 1) of
|
||||||
|
|
|
@ -15,14 +15,18 @@ main = do
|
||||||
, pointer = 0
|
, pointer = 0
|
||||||
, pointerOffset = 0
|
, pointerOffset = 0
|
||||||
, output = []
|
, output = []
|
||||||
, input = [x]
|
, input = Nothing
|
||||||
, state = Continue
|
, state = Continue
|
||||||
})
|
}) x
|
||||||
let out1 = output $ run 1
|
let out1 = output $ run $ Just 1
|
||||||
let out2 = output $ run 2
|
let out2 = output $ run $ Just 2
|
||||||
print $
|
print $
|
||||||
concat
|
concat
|
||||||
["Part 1: " ++ show a ++ ", Part 2: " ++ show b | a <- out1, b <- out2]
|
["Part 1: " ++ show a ++ ", Part 2: " ++ show b | a <- out1, b <- out2]
|
||||||
|
|
||||||
runIntcode :: TuringMachine -> TuringMachine
|
runIntcode :: TuringMachine -> Maybe Integer -> TuringMachine
|
||||||
runIntcode = execSteps
|
runIntcode tm x = case state tmNew of
|
||||||
|
Continue -> runIntcode tmNew x
|
||||||
|
AwaitInput -> runIntcode (tmNew{input = x}) x
|
||||||
|
_ -> tmNew
|
||||||
|
where tmNew = execSteps tm
|
||||||
|
|
Loading…
Reference in New Issue
Block a user