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 )
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-07 17:27:35 +01:00
print $ find_max tape [ 0 .. 4 ]
print $ find_max tape [ 5 .. 9 ]
2019-12-07 10:06:47 +01:00
2019-12-07 22:45:18 +01:00
find_max :: Tape -> [ Int ] -> Int
find_max tape range = L . maximum [ run_amps_feedback tape xs | xs <- L . permutations range ]
2019-12-07 20:11:38 +01:00
run_amps_feedback :: Tape -> [ Int ] -> Int
2019-12-07 17:27:35 +01:00
run_amps_feedback tape intseq = L . head $ run_amps_feedback' 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
run_amps_feedback' :: [ TuringMachine ] -> [ Int ] -> [ Int ] -> [ Int ]
2019-12-07 17:27:35 +01:00
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
2019-12-07 10:06:47 +01:00
2019-12-07 20:11:38 +01:00
opl :: Int -> Int
2019-12-07 10:06:47 +01:00
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
2019-12-07 20:11:38 +01:00
paramch :: [ Bool ] -> TapeSection -> Tape -> TapeSection
2019-12-07 10:06:47 +01:00
paramch 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 20:11:38 +01:00
getopmodes :: TapeSection -> ( String , [ Bool ] )
2019-12-07 10:06:47 +01:00
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
2019-12-07 20:11:38 +01:00
step :: TapeSection -> ( TuringMachine , [ Int ] , [ Int ] ) -> ( TuringMachine , OutAction , [ Int ] , [ Int ] )
step opvec ( ( t , p ) , input , val )
| op == " 1 " = ( 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 == " 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 " = ( new_tm t ( if ( params ! 0 ) < ( params ! 1 )
then 1 else 0 ) , Continue , input , val )
| op == " 8 " = ( new_tm t ( if ( params ! 0 ) == ( params ! 1 )
then 1 else 0 ) , Continue , input , val )
| op == " 99 " = ( ( t , p ) , Halt , input , val )
2019-12-07 10:06:47 +01:00
where ( op , m ) = getopmodes opvec
params = paramch m opvec t
2019-12-07 20:11:38 +01:00
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 ) =
2019-12-07 10:06:47 +01:00
let command_length = opl $ t ! p
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 )
else exec_steps ( ( t_new , p_new ) , input_new , output_new , cond )