71 lines
3.0 KiB
Haskell
71 lines
3.0 KiB
Haskell
import Data.List.Split
|
|
import qualified Data.List as L
|
|
import Data.Vector as V
|
|
import Data.Char
|
|
import System.Exit
|
|
|
|
main = do
|
|
content <- readFile "input"
|
|
let tape = fromList $ L.concatMap (L.map read . splitOn ",") (lines content)
|
|
print $ find_max tape [0..4]
|
|
print $ find_max tape [5..9]
|
|
|
|
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) 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 tape range = L.maximum [run_amps_feedback tape xs | xs <- L.permutations range]
|
|
|
|
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 || cond == Halt then ((t_new,p_new),input_new,val_new,cond)
|
|
else exec_steps ((t_new,p_new),input_new,val_new,cond)
|
|
|
|
|