AoC/2019/day9/day9.hs

33 lines
887 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 Intcode
import Data.List.Split
import qualified Data.Vector as V
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)
let run x =
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:43:55 +01:00
}) x
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
runIntcode tm x = case state tmNew of
Continue -> runIntcode tmNew x
AwaitInput -> runIntcode (tmNew{input = x}) x
_ -> tmNew
where tmNew = execSteps tm