Day 7: Work through Linter spam
This commit is contained in:
parent
fb1f3e7d04
commit
02a14fcfb2
|
@ -11,44 +11,44 @@ type TuringMachine = (Tape,Int)
|
||||||
main = do
|
main = do
|
||||||
content <- readFile "input"
|
content <- readFile "input"
|
||||||
let tape = fromList $ L.concatMap (L.map read . splitOn ",") (lines content)
|
let tape = fromList $ L.concatMap (L.map read . splitOn ",") (lines content)
|
||||||
print $ find_max tape [0..4]
|
print $ findMax tape [0..4]
|
||||||
print $ find_max tape [5..9]
|
print $ findMax tape [5..9]
|
||||||
|
|
||||||
find_max :: Tape -> [Int] -> Int
|
findMax :: Tape -> [Int] -> Int
|
||||||
find_max tape range = L.maximum [run_amps_feedback tape xs | xs <- L.permutations range]
|
findMax tape range = L.maximum [runAmps tape xs | xs <- L.permutations range]
|
||||||
|
|
||||||
run_amps_feedback :: Tape -> [Int] -> Int
|
runAmps :: Tape -> [Int] -> Int
|
||||||
run_amps_feedback tape intseq = L.head $ run_amps_feedback' tms intseq [0]
|
runAmps tape intseq = L.head $ runAmps' tms intseq [0]
|
||||||
where tms = L.replicate 5 (tape,0)
|
where tms = L.replicate 5 (tape,0)
|
||||||
|
|
||||||
run_amps_feedback' :: [TuringMachine] -> [Int] -> [Int] -> [Int]
|
runAmps' :: [TuringMachine] -> [Int] -> [Int] -> [Int]
|
||||||
run_amps_feedback' ((t,p):tms) intseq prev =
|
runAmps' ((t,p):tms) intseq prev =
|
||||||
if halt == Halt then prev else
|
if halt == Halt then prev else
|
||||||
run_amps_feedback' (tms L.++ [tm_new]) tailseq tm_out
|
runAmps' (tms L.++ [tm_new]) tailseq tm_out
|
||||||
where (tm_new,tm_in,tm_out,halt) = exec_steps ((t,p),xprev,[],Continue)
|
where (tm_new,tm_in,tm_out,halt) = execSteps ((t,p),xprev,[],Continue)
|
||||||
tailseq = if L.null intseq then [] else L.tail intseq
|
tailseq = if L.null intseq then [] else L.tail intseq
|
||||||
xprev = if L.null intseq then prev else (L.head intseq):prev
|
xprev = if L.null intseq then prev else L.head intseq:prev
|
||||||
|
|
||||||
opl :: Int -> Int
|
opLength :: Int -> Int
|
||||||
opl x
|
opLength x
|
||||||
| n`L.elem`"1278"=4
|
| n`L.elem`"1278"=4
|
||||||
| n`L.elem`"56"=3
|
| n`L.elem`"56"=3
|
||||||
| n`L.elem`"34"=2
|
| n`L.elem`"34"=2
|
||||||
| otherwise=1
|
| otherwise=1
|
||||||
where n = L.last $ show x
|
where n = L.last $ show x
|
||||||
|
|
||||||
parsemodes :: String -> [Bool]
|
parseModes :: String -> [Bool]
|
||||||
parsemodes m = L.replicate (3 - L.length l) False L.++ l
|
parseModes m = L.replicate (3 - L.length l) False L.++ l
|
||||||
where l = L.map (toEnum . digitToInt) m
|
where l = L.map (toEnum . digitToInt) m
|
||||||
|
|
||||||
paramch :: [Bool] -> TapeSection -> Tape -> TapeSection
|
paramChange :: [Bool] -> TapeSection -> Tape -> TapeSection
|
||||||
paramch m opvec t = imap f (V.tail opvec)
|
paramChange m opvec t = imap f (V.tail opvec)
|
||||||
where f i a = if (L.reverse m) !! i then a else t ! a
|
where f i a = if L.reverse m !! i then a else t ! a
|
||||||
|
|
||||||
getopmodes :: TapeSection -> (String, [Bool])
|
getOpModes :: TapeSection -> (String, [Bool])
|
||||||
getopmodes opvec = (op_dedup,parsed_modes)
|
getOpModes opvec = (op_dedup,parsed_modes)
|
||||||
where (op,modes) = L.splitAt 2 $ L.reverse $ show $ opvec ! 0
|
where (op,modes) = L.splitAt 2 $ L.reverse $ show $ opvec ! 0
|
||||||
parsed_modes = parsemodes $ L.reverse modes
|
parsed_modes = parseModes $ L.reverse modes
|
||||||
op_dedup = if L.last op == '0' then [L.head op] else op
|
op_dedup = if L.last op == '0' then [L.head op] else op
|
||||||
|
|
||||||
step :: TapeSection -> (TuringMachine, [Int], [Int]) -> (TuringMachine, OutAction, [Int], [Int])
|
step :: TapeSection -> (TuringMachine, [Int], [Int]) -> (TuringMachine, OutAction, [Int], [Int])
|
||||||
|
@ -56,24 +56,24 @@ step opvec ((t,p),input,val)
|
||||||
| op=="1" = (tm_binop (+),Continue,input,val)
|
| op=="1" = (tm_binop (+),Continue,input,val)
|
||||||
| op=="2" = (tm_binop (*),Continue,input,val)
|
| op=="2" = (tm_binop (*),Continue,input,val)
|
||||||
| op=="3" = (new_tm t (L.head input),Continue,L.tail input,val)
|
| op=="3" = (new_tm t (L.head input),Continue,L.tail input,val)
|
||||||
| op=="4" = ((t,p),Output,input,(V.last $ params):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=="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=="6" = ((t, if params ! 0==0 then params ! 1 else p),Continue,input,val)
|
||||||
| op=="7" = (new_tm t (if (params ! 0)<(params ! 1)
|
| op=="7" = (new_tm t (if (params ! 0)<(params ! 1)
|
||||||
then 1 else 0),Continue,input,val)
|
then 1 else 0),Continue,input,val)
|
||||||
| op=="8" = (new_tm t (if (params ! 0)==(params ! 1)
|
| op=="8" = (new_tm t (if (params ! 0)==(params ! 1)
|
||||||
then 1 else 0),Continue,input,val)
|
then 1 else 0),Continue,input,val)
|
||||||
| op=="99" = ((t,p),Halt,input,val)
|
| op=="99" = ((t,p),Halt,input,val)
|
||||||
where (op,m) = getopmodes opvec
|
where (op,m) = getOpModes opvec
|
||||||
params = paramch m opvec t
|
params = paramChange m opvec t
|
||||||
tm_binop x = new_tm t ((params ! 0) `x` (params ! 1))
|
tm_binop x = new_tm t ((params ! 0) `x` (params ! 1))
|
||||||
new_tm t x = ((t // [((V.last opvec), x)]),p)
|
new_tm t x = (t // [(V.last opvec, x)],p)
|
||||||
|
|
||||||
exec_steps :: (TuringMachine, [Int], [Int], OutAction) -> (TuringMachine, [Int], [Int], OutAction)
|
execSteps :: (TuringMachine, [Int], [Int], OutAction) -> (TuringMachine, [Int], [Int], OutAction)
|
||||||
exec_steps ((t,p),input,output,halt) =
|
execSteps ((t,p),input,output,halt) =
|
||||||
let command_length = opl $ t ! p
|
let command_length = opLength $ t ! p
|
||||||
opvec = slice p command_length t
|
opvec = slice p command_length t
|
||||||
((t_new,p_new),cond,input_new,output_new) =
|
((t_new,p_new),cond,input_new,output_new) =
|
||||||
step opvec ((t,p+command_length),input,output) in
|
step opvec ((t,p+command_length),input,output) in
|
||||||
if cond `L.elem` [Output, Halt] then ((t_new,p_new),input_new,output_new,cond)
|
if cond `L.elem` [Output, Halt] then ((t_new,p_new),input_new,output_new,cond)
|
||||||
else exec_steps ((t_new,p_new),input_new,output_new,cond)
|
else execSteps ((t_new,p_new),input_new,output_new,cond)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user