Day 7: Almost there

This commit is contained in:
shu 2019-12-07 16:02:16 +01:00
parent c25714bb48
commit 1d26a5e2f8

View File

@ -15,34 +15,38 @@ main = do
-- 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 input = fromList $ L.concatMap (L.map read . splitOn ",") (lines content)
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 input intseq [0]
let ret = run_amps tape intseq [0]
-- print $ "Final result:" L.++ (show ret)
print $ find_max input
-- print $ find_max tape
-- print $ find_max2 input
print $ run_amps_feedback input intseq [0] [0,0,0,0,0]
print $ run_amps_feedback tape intseq
run_amps tm [] prev = prev
run_amps tm (x:xs) prev = run_amps tm xs next
where (_,_,_,next,_) = exec_steps (tm,0,x:prev,[],Continue)
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 tm [] prev pointer = prev
run_amps_feedback tm (x:xs) prev (p:ps) =
if halt == Halt then traceShow "Output!" tm_out else
run_amps_feedback tm (xs L.++ [x]) tm_out (ps L.++ [p_new])
where (_,p_new,tm_in,tm_out,halt) = exec_steps (tm,p,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]]
-- 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
@ -81,13 +85,13 @@ step opvec (t,p,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) =
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) =
traceShowId (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)
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)