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"."
"; $debug = []; } return implode("
\n", $output); } /* each amp runs an intcode phase settings, run exactly once */ $input = "3,15,3,16,1002,16,10,16,1,16,15,15,4,15,99,0,0"; $input = "3,23,3,24,1002,24,10,24,1002,23,-1,23,101,5,23,23,1,24,23,23,4,23,99,0,0"; $input = "3,31,3,32,1002,32,10,32,1001,31,-2,31,1007,31,0,33,1002,33,7,33,1,33,31,31,1,32,31,31,4,31,99,0,0,0"; $input = file_get_contents("day7.txt"); // echo Intcode($input,13,0); // die(); $maxThrust = 0; $lastOutput = 0; for($a=0;$a<=4;$a++){ for($b=0;$b<=4;$b++){ if($b!=$a){ for($c=0;$c<=4;$c++){ if($c!=$b && $c!=$a){ for($d=0;$d<=4;$d++){ if($d!=$c && $d!=$b && $d!=$a){ for($e=0;$e<=4;$e++){ if($e!=$d && $e!=$c && $e!=$b && $e!=$a){ // if("$a,$b,$c,$d,$e"==="1,0,2,3,4"){ $printDebug=true; } // else { $printDebug=false; } //// echo "$a,$b,$c,$d,$e
"; $output = ""; $lastOutput = 0; foreach(array($a,$b,$c,$d,$e) as $key => $phase){ // echo Intcode($input, $phase); // echo "Last output: ".$lastOutput."
"; $temp = Intcode($input, [$phase, $lastOutput]); // echo "
"; // $output .= $temp.","; $output = $temp; $lastOutput = $temp; if(!is_numeric($lastOutput)){ echo "Trying to do ".'Intcode($input, '.$lastOutput.', '."$phase);"."
"; echo "Couldn't continue. lastOutput is not a number: ".$lastOutput.""; break; } // $debug[] = "Output: ".$temp; // $lastOutput = Intcode($input, $lastOutput, $phase); // echo $lastOutput.","; // $output += $lastOutput; } // $printDebug=false; // if($a == 1){ // echo $temp; // break(5); // } echo $temp." ($a,$b,$c,$d,$e)
"; if($output > $maxThrust){ $maxThrust = $output; } // echo "

\nDebug:
".implode("
\n", $debug)."
\n"."
"; } } } } } } } } } // print_r($out); echo "

Max thruster signal: ".$maxThrust."

"; // 4848201312 : TOO HIGH // 43012 : TOO LOW ?>