diff --git a/2019/day7/day7.hs b/2019/day7/day7.hs index 85bf052..248c380 100644 --- a/2019/day7/day7.hs +++ b/2019/day7/day7.hs @@ -2,51 +2,24 @@ 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 + print $ find_max tape [0..4] + print $ find_max tape [5..9] -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] +run_amps_feedback tape intseq = L.head $ 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) +run_amps_feedback' ((t,p):tms) intseq prev = + if halt == Halt then prev else + run_amps_feedback' (tms L.++ [tm_new]) tailseq tm_out + where (tm_new,tm_in,tm_out,halt) = exec_steps ((t,p),xprev,[],Continue) + tailseq = if L.null intseq then [] else L.tail intseq + xprev = if L.null intseq then prev else (L.head intseq):prev -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]] +find_max tape range = L.maximum [run_amps_feedback tape xs | xs <- L.permutations range] opl x | n`L.elem`"1278"=4 @@ -91,7 +64,7 @@ exec_steps ((t,p),input,val,halt) = 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) + if cond == Output || cond == Halt then ((t_new,p_new),input_new,val_new,cond) else exec_steps ((t_new,p_new),input_new,val_new,cond) diff --git a/2019/day7/input b/2019/day7/input new file mode 100644 index 0000000..c4d880f --- /dev/null +++ b/2019/day7/input @@ -0,0 +1 @@ +3,8,1001,8,10,8,105,1,0,0,21,38,63,76,93,118,199,280,361,442,99999,3,9,101,3,9,9,102,3,9,9,101,4,9,9,4,9,99,3,9,1002,9,2,9,101,5,9,9,1002,9,5,9,101,5,9,9,1002,9,4,9,4,9,99,3,9,101,2,9,9,102,3,9,9,4,9,99,3,9,101,2,9,9,102,5,9,9,1001,9,5,9,4,9,99,3,9,102,4,9,9,1001,9,3,9,1002,9,5,9,101,2,9,9,1002,9,2,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,1,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,1,9,4,9,3,9,101,2,9,9,4,9,99,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,99,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,2,9,9,4,9,3,9,1001,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,1001,9,1,9,4,9,3,9,1002,9,2,9,4,9,99,3,9,1001,9,1,9,4,9,3,9,102,2,9,9,4,9,3,9,102,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,101,1,9,9,4,9,3,9,1001,9,2,9,4,9,99,3,9,1002,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,1002,9,2,9,4,9,3,9,1001,9,2,9,4,9,3,9,102,2,9,9,4,9,3,9,101,1,9,9,4,9,3,9,101,2,9,9,4,9,3,9,1002,9,2,9,4,9,99