module Main where

import Data.List.Split
import qualified Data.Vector as V
import Intcode

main :: IO ()
main = do
  content <- readFile "input"
  let program = V.fromList $ concatMap (map read . splitOn ",") (lines content)
  let run =
        runIntcode
          (TM
             { tape = tapePreprocess program
             , pointer = 0
             , pointerOffset = 0
             , output = []
             , input = Nothing
             , state = Continue
             })
  let out1 = output $ run $ Just 1
  let out2 = output $ run $ Just 2
  print $
    concat
      ["Part 1: " ++ show a ++ ", Part 2: " ++ show b | a <- out1, b <- out2]

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
