import Data.List.Split import qualified Data.List as L import Data.Vector as V import Data.Char import Data.Ord import Debug.Trace import System.Exit main = do content <- readFile "input" -- let content = "3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0" --let content = "3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0" -- let content = "3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0" --day5 example: -- let content = "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99" -- part2: let content = "3,26,1001,26,-4,26,3,27,1002,27,2,27,1,27,26,27,4,27,1001,28,-1,28,1005,28,6,99,0,0,5" let tape = fromList $ L.concatMap (L.map read . splitOn ",") (lines content) -- let intseq = [0,1,2,3,4] let intseq = [9,8,7,6,5] let ret = run_amps tape intseq [0] -- print $ "Final result:" L.++ (show ret) -- print $ find_max tape -- print $ find_max2 input print $ run_amps_feedback tape intseq run_amps t [] prev = prev run_amps t (x:xs) prev = run_amps t xs next where (_,_,next,_) = exec_steps ((t,0),x:prev,[],Continue) run_amps_feedback tape intseq = run_amps_feedback' tms intseq [0] where tms = L.replicate 5 (tape,0) run_amps_feedback' ((t,p):tms) (x:xs) prev = if halt == Halt then tm_out else run_amps_feedback' (tms L.++ [tm_new]) xs tm_out where (tm_new,tm_in,tm_out,halt) = exec_steps ((t,p),x:prev,[],Continue) --todo: [x] -> x run_amps_feedback' ((t,p):tms) [] prev = if halt == Halt then tm_out else run_amps_feedback' (tms L.++ [tm_new]) [] tm_out where (tm_new,tm_in,tm_out,halt) = exec_steps ((t,p),prev,[],Continue) find_max input = L.maximum [(L.head $ run_amps input [a,b,c,d,e] [0], [a,b,c,d,e]) | a<-[0..4], b<-[0..4], c<-[0..4], d<-[0..4], e<-[0..4], L.nub [a,b,c,d,e] == [a,b,c,d,e]] -- find_max2 input = L.maximum [(L.head $ run_amps_feedback input [a,b,c,d,e] [0] [0,0,0,0,0], [a,b,c,d,e]) -- | a<-[5..9], b<-[5..9], c<-[5..9], d<-[5..9], e<-[5..9], -- L.nub [a,b,c,d,e] == [a,b,c,d,e]] opl x | n`L.elem`"1278"=4 | n`L.elem`"56"=3 | n`L.elem`"34"=2 | otherwise=1 where n = L.last $ show x parsemodes :: String -> [Bool] parsemodes m = L.replicate (3 - L.length l) False L.++ l where l = L.map (toEnum . digitToInt) m paramch m opvec t = imap f (V.tail opvec) where f i a = if (L.reverse m) !! i then a else t ! a getopmodes opvec = (op_dedup,parsed_modes) where (op,modes) = L.splitAt 2 $ L.reverse $ show $ opvec ! 0 parsed_modes = parsemodes $ L.reverse modes op_dedup = if L.last op == '0' then [L.head op] else op data OutAction = Continue | Output | Halt deriving (Enum, Eq, Show) step :: Vector Int -> (Vector Int, Int, [Int], [Int]) -> (Vector Int, Int, OutAction, [Int], [Int]) step opvec (t,p,input,val) | op=="1" = ((t // [((V.last opvec), (params ! 0) + (params ! 1))]),p,Continue,input,val) | op=="2" = ((t // [((V.last opvec), (params ! 0) * (params ! 1))]),p,Continue,input,val) | op=="3" = ((t // [((V.last opvec), L.head input)]),p,Continue,L.tail input,val) | op=="4" = (t,p,Output,input,(V.last $ params):val) | op=="5" = (t, if (params ! 0)/=0 then (params ! 1) else p,Continue,input,val) | op=="6" = (t, if (params ! 0)==0 then (params ! 1) else p,Continue,input,val) | op=="7" = ((t // [((V.last opvec), if (params ! 0)<(params ! 1) then 1 else 0)]),p,Continue,input,val) | op=="8" = ((t // [((V.last opvec), if (params ! 0)==(params ! 1) then 1 else 0)]),p,Continue,input,val) | op=="99" = (t,p,Halt,input,val) where (op,m) = getopmodes opvec params = paramch m opvec t exec_steps :: ((Vector Int, Int), [Int], [Int], OutAction) -> ((Vector Int, Int), [Int], [Int], OutAction) exec_steps ((t,p),input,val,halt) = let command_length = opl $ t ! p opvec = slice p command_length t (t_new,p_new,cond,input_new,val_new) = step opvec (t,p+command_length,input,val) in if cond == Output then ((t_new,p_new),input_new,val_new,cond) else exec_steps ((t_new,p_new),input_new,val_new,cond)