Created
December 5, 2019 16:13
-
-
Save 5quinque/e7d0181e09d8eafcb4768a5b89869ba9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function readInput() | |
{ | |
$wires = []; | |
if ($file = fopen("input/day3.txt", "r")) { | |
while ($line = fgets($file)) { | |
$wires[] = explode(",", $line); | |
} | |
fclose($file); | |
} | |
return $wires; | |
} | |
function plotGrid($wires) | |
{ | |
$grid = []; | |
$wireLength = []; | |
$wireID = 0; | |
foreach ($wires as $w) { | |
$posX = 0; | |
$posY = 0; | |
$wl = 0; | |
$wireLength[$wireID][$posX][$posY] = $wl; | |
foreach ($w as $ins) { | |
$direction = str_split($ins)[0]; | |
$length = trim(substr($ins, 1)); | |
switch ($direction) { | |
case 'U': | |
for ($i = $posY; $posY < $i + $length;) { | |
$grid[$posX][++$posY][] = $wireID; | |
$wl++; | |
//echo "U - id $wireID X $posX Y $posY L $wl\n"; | |
$wireLength[$wireID][$posX][$posY] = $wl; | |
} | |
break; | |
Case 'D': | |
for ($i = $posY; $posY > $i - $length;) { | |
$grid[$posX][--$posY][] = $wireID; | |
$wl++; | |
$wireLength[$wireID][$posX][$posY] = $wl; | |
} | |
break; | |
Case 'L': | |
for ($i = $posX; $posX > $i - $length;) { | |
$grid[--$posX][$posY][] = $wireID; | |
$wl++; | |
$wireLength[$wireID][$posX][$posY] = $wl; | |
} | |
break; | |
Case 'R': | |
for ($i = $posX; $posX < $i + $length;) { | |
$grid[++$posX][$posY][] = $wireID; | |
$wl++; | |
//echo "R - id $wireID X $posX Y $posY L $wl\n"; | |
$wireLength[$wireID][$posX][$posY] = $wl; | |
} | |
break; | |
} | |
} | |
$wireID++; | |
} | |
//print_r($wireLength); | |
return [$grid, $wireLength]; | |
} | |
function part1() | |
{ | |
$wires = readInput(); | |
$grid = plotGrid($wires)[0]; | |
$mdClosestToPort = null; | |
foreach ($grid as $X => $arrY) { | |
foreach ($arrY as $Y => $wireIDs) { | |
// Find intersection | |
if (count($wireIDs) > 1) { | |
// Calculate Manhattan distance | |
if ($X < 0) { | |
$X = -$X; | |
} | |
if ($Y < 0) { | |
$Y = -$Y; | |
} | |
$i = $X + $Y; | |
if ($i < $mdClosestToPort && $i != 0 || $mdClosestToPort == null) { | |
$mdClosestToPort = $i; | |
} | |
} | |
} | |
} | |
return $mdClosestToPort; | |
} | |
function part2() | |
{ | |
$wires = readInput(); | |
//$wires = [["U7", "R4"], ["R2", "U3", "L4"]]; | |
//$wires = [["R2", "U3"]]; | |
//$wires = [["U3", "R3", "D3", "L3"]]; | |
/* | |
| | |
| | |
| | |
| | |
--+-| | |
| | | |
| | | |
-- | |
*/ | |
$grid = plotGrid($wires); | |
$wireLength = $grid[1]; | |
//print_r($wireLength); | |
$lClosestToPort = null; | |
foreach ($grid[0] as $X => $arrY) { | |
foreach ($arrY as $Y => $wireIDs) { | |
// Find intersection | |
if (count($wireIDs) > 1) { | |
// Calculate wirelength from this point. | |
$lengthFromO1 = $wireLength[$wireIDs[0]][$X][$Y]; | |
$lengthFromO2 = $wireLength[$wireIDs[1]][$X][$Y]; | |
$totalLength = $lengthFromO1 + $lengthFromO2; | |
if ($totalLength < $lClosestToPort || $lClosestToPort == null) { | |
echo "WireID 0: {$wireIDs[0]} WireID 1: {$wireIDs[1]}\n"; | |
echo "Line1 Length: $lengthFromO1 Line2 Length: $lengthFromO2 Total $totalLength\n"; | |
echo "X: $X Y: $Y\n"; | |
$lClosestToPort = $totalLength; | |
} | |
} | |
} | |
} | |
return $lClosestToPort; | |
} | |
echo "Part 1 " . part1() . "\n"; | |
echo "Part 2 " . part2() . "\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment