diff --git a/2019/day7/day7.hs b/2019/day7/day7.hs index 7403ac1..0ccab52 100644 --- a/2019/day7/day7.hs +++ b/2019/day7/day7.hs @@ -3,16 +3,19 @@ import qualified Data.List as L import Data.Vector as V import Data.Char +data OutAction = Continue | Output | Halt deriving (Enum, Eq, Show) +type Tape = Vector Int +type TapeSection = Vector Int +type TuringMachine = (Tape,Int) + 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] -data OutAction = Continue | Output | Halt deriving (Enum, Eq, Show) -type Tape = Vector Int -type TapeSection = Vector Int -type TuringMachine = (Tape,Int) +find_max :: Tape -> [Int] -> Int +find_max tape range = L.maximum [run_amps_feedback tape xs | xs <- L.permutations range] run_amps_feedback :: Tape -> [Int] -> Int run_amps_feedback tape intseq = L.head $ run_amps_feedback' tms intseq [0] @@ -26,9 +29,6 @@ run_amps_feedback' ((t,p):tms) intseq prev = 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 -> [Int] -> Int -find_max tape range = L.maximum [run_amps_feedback tape xs | xs <- L.permutations range] - opl :: Int -> Int opl x | n`L.elem`"1278"=4 @@ -51,7 +51,6 @@ getopmodes opvec = (op_dedup,parsed_modes) parsed_modes = parsemodes $ L.reverse modes op_dedup = if L.last op == '0' then [L.head op] else op - step :: TapeSection -> (TuringMachine, [Int], [Int]) -> (TuringMachine, OutAction, [Int], [Int]) step opvec ((t,p),input,val) | op=="1" = (tm_binop (+),Continue,input,val) @@ -70,7 +69,6 @@ step opvec ((t,p),input,val) tm_binop x = new_tm t ((params ! 0) `x` (params ! 1)) new_tm t x = ((t // [((V.last opvec), x)]),p) - exec_steps :: (TuringMachine, [Int], [Int], OutAction) -> (TuringMachine, [Int], [Int], OutAction) exec_steps ((t,p),input,output,halt) = let command_length = opl $ t ! p @@ -79,5 +77,3 @@ exec_steps ((t,p),input,output,halt) = 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) else exec_steps ((t_new,p_new),input_new,output_new,cond) - -