This one was quite easy - just do calculations until all the values are closest together (i.e. check for min height) and then print them to the console. Didn't event have to do anything for part 2 ;)
<?php$input=require_once'readFile.php';$array=array_map(function($str){$re="/position=<\s?(-?\d+),\s+(-?\d+)>\svelocity=<\s?(-?\d+),\s+(-?\d+).+/";returnpreg_split($re,$str,NULL,PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE);},$input);functioncaclcMinHeight($array){$min=INF;$max=0;foreach($arrayas$value){if($value[1]<$min){$min=$value[1];}if($value[1]>$max){$max=$value[1];}}return$max-$min;}functioncalcPosition($array,$seconds){returnarray_map(function($data)use($seconds){$posX=$data[0]+$data[2]*$seconds;$posY=$data[1]+$data[3]*$seconds;return[$posX,$posY];},$array);}$positions=[];$height=INF;$seconds=1;while(true){$positions=calcPosition($array,$seconds);$newHeight=caclcMinHeight($positions,$height);if($newHeight>$height){$seconds--;$positions=calcPosition($array,$seconds);break;}else{$height=$newHeight;$seconds++;}}$pixels=[];foreach($positionsas$value){$pixels[$value[0]][$value[1]]=true;}$minY=INF;$maxY=0;$minX=INF;$maxX=0;foreach($positionsas$value){if($value[0]>$maxX){$maxX=$value[0];}if($value[0]<$minX){$minX=$value[0];}if($value[1]>$maxY){$maxY=$value[1];}if($value[1]<$minY){$minY=$value[1];}}for($y=$minY-1;$y<=$maxY+1;$y++){for($x=$minX-1;$x<=$maxX+1;$x++){echo!empty($pixels[$x][$y])?"#":" ";}echo"\n";}echo"Done in ".$seconds." \"seconds\"\n\n";?>
readFile.php
<?php$file=fopen("input.txt","r")orexit("Unable to open file");while(!feof($file)){$array[]=fgets($file);}fclose($file);returnarray_filter($array);?>
I also stopped when I noticed that the stars started getting further apart. I wish that had been part of the problem description though, since it's possible for to come up with an input that breaks that assumption.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This one was quite easy - just do calculations until all the values are closest together (i.e. check for min height) and then print them to the console. Didn't event have to do anything for part 2 ;)
readFile.php
I also stopped when I noticed that the stars started getting further apart. I wish that had been part of the problem description though, since it's possible for to come up with an input that breaks that assumption.