import Data.List.Split
import Data.Sequence as S
import Data.Foldable

main = do
    content <- readFile "input"
    let input = concatMap (map read . splitOn ",") (lines content)
    let input2 = in_replace input 12 2
    print (day2a input2 (fromList input2) 0)
    print (day2b input)


in_replace input a b = (head input):a:b:(Prelude.drop 3 input)

day2a :: [Int] -> Seq Int -> Int -> Int
day2a (op:a:b:out:xs) acc n
    | op==99=index acc 0
    | otherwise=let newacc = update out ((index acc a)+*(index acc b)) acc in
        day2a (toList $ S.drop m newacc) newacc (m)
    where (+*) = if op==1 then (+) else (*)
          m = n+4
day2a xs acc n = index acc 0

day2b :: [Int] -> Int
day2b input = sum [ let repl = in_replace input x y in
    if day2a repl (fromList repl) 0 == 19690720 then 100*x+y
        else 0 | x<-[0..99], y<-[0..99]]
