Day 7, part 1\nI used way to much time to figure out a bug in the intcode..
parent
198aeba7b4
commit
a283cb0459
|
@ -0,0 +1,251 @@
|
|||
<?php
|
||||
|
||||
$printDebug = false;
|
||||
$debug = array();
|
||||
|
||||
function setValue($data, $pos, $newVal, $mode){
|
||||
if($mode == 1){
|
||||
$data[ $pos ] = $newVal;
|
||||
// return $pos;
|
||||
}
|
||||
else {
|
||||
$data[ $data[$pos] ] = $newVal;
|
||||
// return $data[$pos];
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
function getValue($data, $val, $mode){
|
||||
if($mode == 1){
|
||||
return $data[ $val ];
|
||||
}
|
||||
else {
|
||||
return $data[ $data[ $val ] ];
|
||||
}
|
||||
}
|
||||
|
||||
function Intcode($input, $inputParam = []){
|
||||
global $printDebug;
|
||||
$orgInputs = $inputParam;
|
||||
$output = array();
|
||||
$input0 = explode(",", trim($input));
|
||||
$input1 = $input0;
|
||||
|
||||
$batchPos = 0;
|
||||
$jumps = 0;
|
||||
$executions = 0;
|
||||
|
||||
/*** OPCODE LOOP ***/
|
||||
while($batchPos < count($input1)){
|
||||
$executions++;
|
||||
if($executions > 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."."<br>\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!"."<br>";
|
||||
$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 "<br><br>\n<b>Debug:</b> (Intcode(".'$input'.", <u><i>".implode(",",$orgInputs)."</i></u>))<br>".implode("<br>\n", $debug)."<br>\n"."<hr>";
|
||||
$debug = [];
|
||||
}
|
||||
|
||||
return implode("<br>\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<br>";
|
||||
$output = "";
|
||||
$lastOutput = 0;
|
||||
foreach(array($a,$b,$c,$d,$e) as $key => $phase){
|
||||
// echo Intcode($input, $phase);
|
||||
// echo "Last output: ".$lastOutput."<br>";
|
||||
$temp = Intcode($input, [$phase, $lastOutput]);
|
||||
// echo "<hr>";
|
||||
// $output .= $temp.",";
|
||||
$output = $temp;
|
||||
$lastOutput = $temp;
|
||||
if(!is_numeric($lastOutput)){
|
||||
echo "Trying to do ".'Intcode($input, '.$lastOutput.', '."$phase);"."<br>";
|
||||
echo "Couldn't continue. lastOutput is not a number: <i>".$lastOutput."</i>";
|
||||
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)<br>";
|
||||
if($output > $maxThrust){
|
||||
$maxThrust = $output;
|
||||
}
|
||||
// echo "<br><br>\nDebug: <br>".implode("<br>\n", $debug)."<br>\n"."<hr>";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// print_r($out);
|
||||
echo "<h1>Max thruster signal: ".$maxThrust."</h1>";
|
||||
// 4848201312 : TOO HIGH
|
||||
// 43012 : TOO LOW
|
||||
|
||||
|
||||
|
||||
?>
|
Loading…
Reference in New Issue