AoC/2019/day9/day9.hs

35 lines
888 B
Haskell
Raw Normal View History

2019-12-11 20:21:55 +01:00
module Main where
2019-12-09 10:31:36 +01:00
2019-12-11 20:21:55 +01:00
import Data.List.Split
import qualified Data.Vector as V
2019-12-12 17:45:43 +01:00
import Intcode
2019-12-09 10:31:36 +01:00
2019-12-11 20:21:55 +01:00
main :: IO ()
2019-12-09 10:31:36 +01:00
main = do
2019-12-11 20:21:55 +01:00
content <- readFile "input"
let program = V.fromList $ concatMap (map read . splitOn ",") (lines content)
2019-12-12 17:45:43 +01:00
let run =
2019-12-11 20:21:55 +01:00
runIntcode
(TM
{ tape = tapePreprocess program
, pointer = 0
, pointerOffset = 0
, output = []
2019-12-12 17:43:55 +01:00
, input = Nothing
2019-12-11 20:21:55 +01:00
, state = Continue
2019-12-12 17:45:43 +01:00
})
2019-12-12 17:43:55 +01:00
let out1 = output $ run $ Just 1
let out2 = output $ run $ Just 2
2019-12-11 20:21:55 +01:00
print $
concat
["Part 1: " ++ show a ++ ", Part 2: " ++ show b | a <- out1, b <- out2]
2019-12-12 17:43:55 +01:00
runIntcode :: TuringMachine -> Maybe Integer -> TuringMachine
2019-12-12 17:45:43 +01:00
runIntcode tm x =
case state tmNew of
2019-12-12 17:43:55 +01:00
Continue -> runIntcode tmNew x
2019-12-12 17:45:43 +01:00
AwaitInput -> runIntcode (tmNew {input = x}) x
2019-12-12 17:43:55 +01:00
_ -> tmNew
2019-12-12 17:45:43 +01:00
where
tmNew = execSteps tm