1000){
echo "KILLED due to 1000 executions.";
break;
}
//INSTRUCTIOnS
$instr = array_reverse( str_split( substr($input0[$batchPos],0,-2) ) );
$mode1 = @$instr[0];
$mode2 = @$instr[1];
$mode3 = @$instr[2];
$opcode = substr($input1[$batchPos],-2);
if($opcode == 99){
$debug[] = $batchPos.": ".$input1[$batchPos].",".$input1[$batchPos+1].", ".$input1[$batchPos+2].", ".$input1[$batchPos+3];
$debug[] = "Opcode $opcode: halts."."
\n";
$batchPos += 9999999; //PLEASE HALT DAMMIT
break;
}
elseif($opcode == 1){
$debug[] = $batchPos.": ".$input1[$batchPos].",".$input1[$batchPos+1].", ".$input1[$batchPos+2].", ".$input1[$batchPos+3];
$newVal = (getValue($input1, $batchPos+1, $mode1) + getValue($input1, $batchPos+2, $mode2));
$debug[] = "Set new value to ".$newVal." (".getValue($input1, $batchPos+1, $mode1)."+".getValue($input1, $batchPos+2, $mode2).")";
$input1 = setValue($input1, $batchPos+3, $newVal, $mode3);
$batchPos += 4;
}
elseif($opcode == 2){
$debug[] = $batchPos.": ".$input1[$batchPos].", ".$input1[$batchPos+1].", ".$input1[$batchPos+2].", ".$input1[$batchPos+3];
$newVal = (getValue($input1, $batchPos+1, $mode1) * (getValue($input1, $batchPos+2, $mode2)));
$input1 = setValue($input1, $batchPos+3, $newVal, $mode3);
$debug[] = "Set new value to ".$newVal;
$batchPos += 4;
}
// ADDED for day 5
elseif($opcode == 3){ // Requires some input.
$debug[] = $batchPos.": ".$input1[$batchPos].", ".$input1[$batchPos+1];
if(!empty($inputParam)){
$newValue = array_shift($inputParam);
$input1 = setValue($input1, $batchPos+1, $newValue, $mode1);
$debug[] = "Set ".$input1[$batchPos+1]."'s value to: ".$newValue;
// array_shift($inputParam);
}
else {
echo "Wait for number!"."
";
$debug[] = "Wait for number...";
break;
}
$batchPos += 2;
}
elseif($opcode == 4){
$debug[] = $batchPos.": ".$input1[$batchPos].", ".$input1[$batchPos+1].", ".$input1[$batchPos+2];
// $output[] = "Opcode 4: ".$input1[ ($mode1 == 1?$batchPos+1:$input1[$batchPos+1]) ];
$output[] = getValue($input1, $batchPos+1, $mode1);
$debug[] = "Outputs: ".getValue($input1, $batchPos+1, $mode1);
$batchPos += 2;
}
// DAY 5, part 2:
elseif($opcode == 5){ //jump-if-true
$debug[] = $batchPos.": ".$input1[$batchPos].", ".$input1[$batchPos+1].", ".$input1[$batchPos+2];
if(getValue($input1, $batchPos+1, $mode1) != 0){
// if($input1[$batchPos+1] != 0 && $jumps < 10){
// $batchPos = $input1[ ($mode2 == 1?$batchPos+2:$input1[$batchPos+2]) ];
$newBatchPos = getValue($input1, $batchPos+2, $mode2);
$debug[] = "Jumped to ".$newBatchPos." (first value: ".getValue($input1, $batchPos+1, $mode1).")";
// $output[] = "Jumped to ".$newBatchPos;
$batchPos = $newBatchPos;
}
else {
$batchPos += 3;
}
}
elseif($opcode == 6){ //jump-if-false
$debug[] = $batchPos.": ".$input1[$batchPos].", ".$input1[$batchPos+1].", ".$input1[$batchPos+2];
if(getValue($input1, $batchPos+1, $mode1) == 0){
$newBatchPos = getValue($input1, $batchPos+2, $mode2);
$debug[] = "Jumped to ".$newBatchPos." (first value: ".getValue($input1, $batchPos+1, $mode1).")";
// $output[] = "Jumped to ".$newBatchPos;
$batchPos = $newBatchPos;
}
else {
$batchPos += 3;
}
}
elseif($opcode == 7){ //less than
$debug[] = $batchPos.": ".$input1[$batchPos].", ".$input1[$batchPos+1].", ".$input1[$batchPos+2].", ".$input1[$batchPos+3];
if(getValue($input1, $batchPos+1, $mode1) < getValue($input1, $batchPos+2, $mode2)){
$debug[] = "Value: ".getValue($input1, $batchPos+1, $mode1)." < ".getValue($input1, $batchPos+2, $mode2);
// $input1[($mode3 == 1?$batchPos+3:$input1[$batchPos+3])] = 1;
$input1 = setValue($input1, $batchPos+3, 1, $mode3);
}
else {
$debug[] = "Value: ".getValue($input1, $batchPos+1, $mode1)." !< ".getValue($input1, $batchPos+2, $mode2);
// $input1[($mode3 == 1?$batchPos+3:$input1[$batchPos+3])] = 0;
$input1 = setValue($input1, $batchPos+3, 0, $mode3);
}
$batchPos += 4;
}
elseif($opcode == 8){
$debug[] = $batchPos.": ".$input1[$batchPos].", ".$input1[$batchPos+1].", ".$input1[$batchPos+2].", ".$input1[$batchPos+3];
if($input1[($mode1 == 1?$batchPos+1:$input1[$batchPos+1])] == $input1[($mode2 == 1?$batchPos+2:$input1[$batchPos+2])]){
$input1[($mode3 == 1?$batchPos+3:$input1[$batchPos+3])] = 1;
}
else {
$input1[($mode3 == 1?$batchPos+3:$input1[$batchPos+3])] = 0;
}
$batchPos += 4;
}
else {
$debug[] = $batchPos.": Unknown opcode: $input1[$batchPos]";
$output[] = "Unknown opcode: $input1[$batchPos]";
break;
}
}
if(empty($output)){ $output[] = "No outputs. Here is first value: ".$input1[0]; }
if($printDebug && !empty($debug)){
echo "
\nDebug: (Intcode(".'$input'.", ".implode(",",$orgInputs)."))
".implode("
\n", $debug)."
\n"."