Adding already completed files
commit
7dc1e65291
|
@ -0,0 +1,3 @@
|
|||
*.txt
|
||||
header.php
|
||||
.ht*
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
$input = file_get_contents("day1.txt");
|
||||
|
||||
$sum = 0;
|
||||
$sum2 = 0;
|
||||
foreach(explode("\n",$input) as $value){
|
||||
if(is_numeric($value)){
|
||||
$sum = $sum + (floor( $value/3 )-2);
|
||||
echo $value.": ".(floor( ($value / 3) )-2) ."<br />";
|
||||
|
||||
// Part 2
|
||||
$sum3 = (floor( $value/3 )-2);
|
||||
// die(intval($sum3));
|
||||
while( $sum3 > 0 ){
|
||||
$sum2 = $sum2 + $sum3;
|
||||
$sum3 = floor($sum3/3)-2;
|
||||
}
|
||||
// break;
|
||||
}
|
||||
}
|
||||
echo "Total: ".$sum."<br /><hr>\n";
|
||||
|
||||
// PART 2
|
||||
echo "Total with fuel: ".$sum2."<br />\n";
|
||||
|
||||
?>
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
// $input = "1,9,10,3,2,3,11,0,99,30,40,50";
|
||||
$input = trim(file_get_contents("day2.txt"));
|
||||
|
||||
// echo $input."<hr>\n";
|
||||
$input1 = explode(",", $input);
|
||||
// var_dump($input1); die();
|
||||
|
||||
|
||||
for($i = 0; $i < 100; $i++){
|
||||
for($j = 0; $j < 100; $j++){
|
||||
$input1 = explode(",", $input);
|
||||
$input1[1] = $i;
|
||||
$input1[2] = $j;
|
||||
// die("n".count($input1));
|
||||
$batchPos = 0;
|
||||
while($batchPos < count($input1)){
|
||||
if($input1[$batchPos] == 1){
|
||||
$input1[$input1[$batchPos+3]] = $input1[$input1[$batchPos+1]] + $input1[$input1[$batchPos+2]];
|
||||
}
|
||||
if($input1[$batchPos] == 2){
|
||||
$input1[$input1[$batchPos+3]] = $input1[$input1[$batchPos+1]] * $input1[$input1[$batchPos+2]];
|
||||
}
|
||||
elseif($input1[0] == 99){
|
||||
break;
|
||||
}
|
||||
$batchPos += 4;
|
||||
}
|
||||
|
||||
if($i == 12 && $j == 2){ echo "Part 1 output: ".$input1[0]."\n"; }
|
||||
if($input1[0] == 19690720){ echo "Part 2 output: ". (100 * $i + $j); break; }
|
||||
// if($i > 1){ break; }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
die();
|
||||
|
||||
|
||||
foreach($input1 as $key => $value){
|
||||
echo $value;
|
||||
if(!(($key+1) % 4) && $key != 0){ echo "\n"; }
|
||||
else{ echo ","; }
|
||||
}
|
||||
die();
|
||||
|
||||
die(implode(",",$input1));
|
||||
print_r($input1);
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
set_time_limit(60*10);
|
||||
|
||||
$wire_x = array();
|
||||
$wire_y = array();
|
||||
$central_x = 0;
|
||||
$central_y = 0;
|
||||
|
||||
// $wires = ["R8,U5,L5,D3", "U7,R6,D4,L4"]; // FIRST EXAMPLE
|
||||
// $wires = ["R75,D30,R83,U83,L12,D49,R71,U7,L72", "U62,R66,U55,R34,D71,R55,D58,R83"];
|
||||
// $wires = ["R98,U47,R26,D63,R33,U87,L62,D20,R33,U53,R51", "U98,R91,D20,R16,D67,R40,U7,R15,U6,R7"];
|
||||
|
||||
$input = file_get_contents("day3.txt");
|
||||
$wires = explode("\n",$input, 2);
|
||||
|
||||
// var_dump($wires);
|
||||
// die();
|
||||
foreach($wires as $key => $wire){
|
||||
$wire_x[$key][0] = 0;
|
||||
$wire_y[$key][0] = 0;
|
||||
$wire_steps[$key] = []; //PART 2
|
||||
|
||||
foreach(explode(",",$wire) as $inst){
|
||||
$dir = substr($inst, 0, 1);
|
||||
$num = substr($inst, 1);
|
||||
|
||||
$wire_steps[$key][] = $num; //PART 2
|
||||
|
||||
if(is_numeric($num)){
|
||||
|
||||
// echo $dir."-".$num."<br>\n";
|
||||
|
||||
for($i = 0; $i < $num; $i++){
|
||||
if($dir == "L"){
|
||||
$wire_x[$key][] = $wire_x[$key][array_key_last($wire_x[$key])]-1;
|
||||
$wire_y[$key][] = $wire_y[$key][array_key_last($wire_y[$key])];
|
||||
}
|
||||
elseif($dir == "D"){
|
||||
$wire_x[$key][] = $wire_x[$key][array_key_last($wire_x[$key])];
|
||||
$wire_y[$key][] = $wire_y[$key][array_key_last($wire_y[$key])]-1;
|
||||
}
|
||||
elseif($dir == "R"){
|
||||
$wire_x[$key][] = $wire_x[$key][array_key_last($wire_x[$key])]+1;
|
||||
$wire_y[$key][] = $wire_y[$key][array_key_last($wire_y[$key])];
|
||||
}
|
||||
elseif($dir == "U"){
|
||||
$wire_x[$key][] = $wire_x[$key][array_key_last($wire_x[$key])];
|
||||
$wire_y[$key][] = $wire_y[$key][array_key_last($wire_y[$key])]+1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach($wire_x[$key] as $pos => $dummy){
|
||||
$wireStrArr[$key][$pos] = $wire_x[$key][$pos].",".$wire_y[$key][$pos];
|
||||
}
|
||||
}
|
||||
|
||||
// print_r($wire_steps);
|
||||
// die();
|
||||
|
||||
$newArr = array_flip($wireStrArr[1]);
|
||||
$smallestDistance = 999999999;
|
||||
$bestSteps = 999999999;
|
||||
foreach($wireStrArr[0] as $pos => $value){
|
||||
|
||||
if(isset($newArr[$value]) && $pos > 0){
|
||||
echo "Meetings at: x$pos (".$wire_x[0][$pos].",".$wire_y[0][$pos].") - ";
|
||||
$distance = ((sqrt( pow($wire_x[0][$pos],2) ))+(sqrt( pow($wire_y[0][$pos],2) )));
|
||||
//PART 1
|
||||
echo "Distance: ".$distance." - ";
|
||||
if($distance < $smallestDistance){ $smallestDistance = $distance; }
|
||||
|
||||
//PART 2
|
||||
// wire1's position: $pos
|
||||
// wire2's: $newArr[$value]
|
||||
$steps = $pos+$newArr[$value];
|
||||
echo "Steps: ".$steps."<br>\n";
|
||||
if($steps < $bestSteps){ $bestSteps = $steps; }
|
||||
}
|
||||
}
|
||||
|
||||
echo "<h3>Smallest distance: $smallestDistance</h3>";
|
||||
echo "<h3>Best steps: $bestSteps</h3>"; // PART 2
|
||||
|
||||
die();
|
||||
echo "<br/><pre style='float:left;'>";
|
||||
print_r($wire_x);
|
||||
echo "</pre>";
|
||||
echo "<pre style='float:left;'>";
|
||||
print_r($wire_y);
|
||||
echo "</pre>";
|
||||
|
||||
|
||||
?>
|
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
/*
|
||||
- 6 digit number
|
||||
- Within puzzle input range
|
||||
- Two adjacent numbers are equal
|
||||
- Number has increasing digits
|
||||
*/
|
||||
|
||||
// Puzzle Input:
|
||||
$input = "153517-630395";
|
||||
// $input = "111111-630395";
|
||||
$totalPossible = 0;
|
||||
$totalPossiblePart2 = 0;
|
||||
|
||||
list($pwdStart, $pwdEnd) = explode("-",$input);
|
||||
|
||||
for ($i=$pwdStart; $i < $pwdEnd; $i++) {
|
||||
// for ($i=$pwdStart; $i < $pwdStart+10000; $i++) {
|
||||
$lastVal = 0;
|
||||
$valid = false;
|
||||
$validP2 = false;
|
||||
|
||||
//HAS SAME VALUE
|
||||
if(strstr($i, "00") || strstr($i, "11") || strstr($i, "22") || strstr($i, "33") || strstr($i, "44") ||
|
||||
strstr($i, "55") || strstr($i, "66") || strstr($i, "77") || strstr($i, "88") || strstr($i, "99")){
|
||||
$valid = true;
|
||||
}
|
||||
|
||||
if($valid){
|
||||
//Increasing
|
||||
foreach(str_split($i) as $key => $val){
|
||||
// echo $key.": ".$val."<br>";
|
||||
if($val < $lastVal){ $valid = false; }
|
||||
$lastVal = $val;
|
||||
}
|
||||
}
|
||||
|
||||
if($valid){
|
||||
// echo "P1-valid: ".$i."<br>\n";
|
||||
$totalPossible++;
|
||||
|
||||
// PART 2
|
||||
$validP2 = false;
|
||||
for($j = 9; $j >= 0; $j--){
|
||||
if(substr_count($i,$j) == 2){
|
||||
$validP2 = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if($validP2){
|
||||
// echo "P2-valid: ".$i."<br>\n";
|
||||
$totalPossiblePart2++;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
echo "<h2>Part 1 - possibilities: ".$totalPossible."</h2>";
|
||||
echo "<h2>Part 2 - possibilities: ".$totalPossiblePart2."</h2>";
|
||||
|
||||
?>
|
|
@ -0,0 +1,189 @@
|
|||
<?php
|
||||
|
||||
set_time_limit(60);
|
||||
$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 ] ];
|
||||
}
|
||||
}
|
||||
|
||||
// FROM day2.php:
|
||||
function Intcode($input, $inputParam = "", $i = "", $j = ""){
|
||||
global $printDebug, $debug;
|
||||
$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];
|
||||
|
||||
if(substr($input1[$batchPos],-2) == 99){
|
||||
$debug[] = "Opcode 99: halts.";
|
||||
break;
|
||||
}
|
||||
elseif(substr($input1[$batchPos],-1) == 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));
|
||||
$input1 = setValue($input1, $batchPos+3, $newVal, $mode3);
|
||||
$debug[] = "Set new value to ".$newVal;
|
||||
|
||||
$batchPos += 4;
|
||||
}
|
||||
elseif(substr($input1[$batchPos],-1) == 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(substr($input1[$batchPos],-1) == 3){ // Requires some input.
|
||||
$debug[] = $batchPos.": ".$input1[$batchPos].", ".$input1[$batchPos+1];
|
||||
$input1[$input1[$batchPos+1]] = $inputParam;
|
||||
$debug[] = "Set value of ".$input1[$batchPos+1]." to: ".$inputParam;
|
||||
$batchPos += 2;
|
||||
}
|
||||
elseif(substr($input1[$batchPos],-1) == 4){
|
||||
$debug[] = $batchPos.": ".$input1[$batchPos].", ".$input1[$batchPos+1].", ".$input1[$batchPos+2];
|
||||
$output[] = "Opcode 4: ".$input1[ ($mode1 == 1?$batchPos+1:$input1[$batchPos+1]) ];
|
||||
$batchPos += 2;
|
||||
}
|
||||
// DAY 5, part 2:
|
||||
elseif(substr($input1[$batchPos],-1) == 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(substr($input1[$batchPos],-1) == 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(substr($input1[$batchPos],-1) == 7){ //less than
|
||||
$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;
|
||||
}
|
||||
elseif(substr($input1[$batchPos],-1) == 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;
|
||||
// $batchPos++;
|
||||
}
|
||||
// $batchPos += 4;
|
||||
// if($printDebug){
|
||||
// echo implode("<br>\n", $debug)."<br><br>\n";
|
||||
// $debug = array();
|
||||
// }
|
||||
}
|
||||
|
||||
if(empty($output)){ $output[] = "No outputs. Here is first value: ".$input1[0]; }
|
||||
|
||||
if($printDebug && !empty($debug)){ echo "<br><br>\nDebug: <br>".implode("<br>\n", $debug)."<br>\n"."<hr>"; }
|
||||
|
||||
return implode("<br>\n", $output);
|
||||
// else { return implode(",",$input1); }
|
||||
// return $input1[0];
|
||||
// if($i == 12 && $j == 2){ echo "Part 1 output: ".$input1[0]."\n"; }
|
||||
// if($input1[0] == 19690720){ echo "Part 2 output: ". (100 * $i + $j); break; }
|
||||
// if($i > 1){ break; }
|
||||
}
|
||||
|
||||
$input = "2,3,0,3,99";
|
||||
$input = "2,4,4,5,99,0";
|
||||
$input = "1,1,1,4,99,5,6,0,99";
|
||||
$input = "1002,4,3,4,33";
|
||||
$input = "1101,100,-1,4,0";
|
||||
$input = "3,9,8,9,10,9,4,9,99,-1,8";
|
||||
// $input = "3,21,1008,21,8,20,1005,20,22,107,8,21,20,1006,20,31,1106,0,36,98,0,0,1002,21,125,20,4,20,1105,1,46,104,999,1105,1,46,1101,1000,1,20,4,20,1105,1,46,98,99";
|
||||
// $input = "3,9,7,9,10,9,4,9,99,-1,8";
|
||||
// $input = "3,3,1108,-1,8,3,4,3,99";
|
||||
$input = file_get_contents("day5.txt");
|
||||
|
||||
// header("Content-Type: application/json");
|
||||
// echo json_encode( explode(",",$input) );
|
||||
// die();
|
||||
|
||||
|
||||
/* * /
|
||||
foreach( explode(",",$input) as $key => $value ){
|
||||
echo $value.",";
|
||||
if(!(($key+1) % 4) && $key > 1){ echo "<br>\n"; }
|
||||
}
|
||||
die();
|
||||
/* */
|
||||
// echo $input."<br>\n";
|
||||
// echo Intcode($input, 5)."<br>\n";
|
||||
// echo Intcode($input, 7)."<br>\n";
|
||||
// echo Intcode($input, 8)."<br>\n";
|
||||
echo Intcode($input, 5);
|
||||
// echo "<script>console.log( JSON.parse('".json_encode( explode(",",$input) )."'));</script>";
|
||||
?>
|
|
@ -0,0 +1,5 @@
|
|||
<?php
|
||||
|
||||
|
||||
|
||||
?>
|
Loading…
Reference in New Issue