AoC/2019/day7/day7.hs

79 lines
3.2 KiB
Haskell
Raw Normal View History

2019-12-07 10:06:47 +01:00
import Data.List.Split
import qualified Data.List as L
import Data.Vector as V
import Data.Char
2019-12-07 22:45:18 +01:00
data OutAction = Continue | Output | Halt deriving (Enum, Eq, Show)
data Mode = Position | Immediate | Relative deriving (Enum, Eq, Show)
2019-12-07 22:45:18 +01:00
type Tape = Vector Int
type TapeSection = Vector Int
type TuringMachine = (Tape,Int)
2019-12-07 10:06:47 +01:00
main = do
content <- readFile "input"
2019-12-07 16:02:16 +01:00
let tape = fromList $ L.concatMap (L.map read . splitOn ",") (lines content)
2019-12-08 19:07:16 +01:00
print $ findMax tape [0..4]
print $ findMax tape [5..9]
2019-12-07 10:06:47 +01:00
2019-12-08 19:07:16 +01:00
findMax :: Tape -> [Int] -> Int
findMax tape range = L.maximum [runAmps tape xs | xs <- L.permutations range]
2019-12-07 20:11:38 +01:00
2019-12-08 19:07:16 +01:00
runAmps :: Tape -> [Int] -> Int
runAmps tape intseq = L.head $ runAmps' tms intseq [0]
2019-12-07 16:02:16 +01:00
where tms = L.replicate 5 (tape,0)
2019-12-07 20:11:38 +01:00
2019-12-08 19:07:16 +01:00
runAmps' :: [TuringMachine] -> [Int] -> [Int] -> [Int]
runAmps' ((t,p):tms) intseq prev =
2019-12-07 17:27:35 +01:00
if halt == Halt then prev else
2019-12-08 19:07:16 +01:00
runAmps' (tms L.++ [tm_new]) tailseq tm_out
where (tm_new,tm_in,tm_out,halt) = execSteps ((t,p),xprev,[],Continue)
2019-12-07 17:27:35 +01:00
tailseq = if L.null intseq then [] else L.tail intseq
2019-12-08 19:07:16 +01:00
xprev = if L.null intseq then prev else L.head intseq:prev
2019-12-07 10:06:47 +01:00
2019-12-08 19:07:16 +01:00
opLength :: Int -> Int
opLength x
2019-12-07 10:06:47 +01:00
| n`L.elem`"1278"=4
| n`L.elem`"56"=3
| n`L.elem`"34"=2
| otherwise=1
where n = L.last $ show x
2019-12-08 19:07:16 +01:00
parseModes :: String -> [Bool]
parseModes m = L.replicate (3 - L.length l) False L.++ l
2019-12-07 10:06:47 +01:00
where l = L.map (toEnum . digitToInt) m
2019-12-08 19:07:16 +01:00
paramChange :: [Bool] -> TapeSection -> Tape -> TapeSection
paramChange m opvec t = imap f (V.tail opvec)
where f i a = if L.reverse m !! i then a else t ! a
2019-12-07 10:06:47 +01:00
2019-12-08 19:07:16 +01:00
getOpModes :: TapeSection -> (String, [Bool])
getOpModes opvec = (op_dedup,parsed_modes)
2019-12-07 10:06:47 +01:00
where (op,modes) = L.splitAt 2 $ L.reverse $ show $ opvec ! 0
2019-12-08 19:07:16 +01:00
parsed_modes = parseModes $ L.reverse modes
2019-12-07 10:06:47 +01:00
op_dedup = if L.last op == '0' then [L.head op] else op
2019-12-07 20:11:38 +01:00
step :: TapeSection -> (TuringMachine, [Int], [Int]) -> (TuringMachine, OutAction, [Int], [Int])
step opvec ((t,p),input,output) = case op of
"1" -> (tm_binop (+),Continue,input,output)
"2" -> (tm_binop (*),Continue,input,output)
"3" -> (new_tm t $ L.head input,Continue,L.tail input,output)
"4" -> ((t,p),Output,input,V.last params:output)
"5" -> ((t, if params ! 0/=0 then params ! 1 else p),Continue,input,output)
"6" -> ((t, if params ! 0==0 then params ! 1 else p),Continue,input,output)
"7" -> (tm_binop (\x y->if x<y then 1 else 0),Continue,input,output)
"8" -> (tm_binop (\x y->if x==y then 1 else 0),Continue,input,output)
"99" -> ((t,p),Halt,input,output)
where (op,m) = getOpModes opvec
params = paramChange m opvec t
tm_binop x = new_tm t ((params ! 0) `x` (params ! 1))
new_tm t x = (t // [(V.last opvec, x)],p)
2019-12-07 20:11:38 +01:00
2019-12-08 19:07:16 +01:00
execSteps :: (TuringMachine, [Int], [Int], OutAction) -> (TuringMachine, [Int], [Int], OutAction)
execSteps ((t,p),input,output,halt) =
let command_length = opLength $ t ! p
2019-12-07 10:06:47 +01:00
opvec = slice p command_length t
2019-12-07 20:11:38 +01:00
((t_new,p_new),cond,input_new,output_new) =
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)
2019-12-08 19:07:16 +01:00
else execSteps ((t_new,p_new),input_new,output_new,cond)