Browse Source

Refactor Day 1 + Done day 2

master
Graeme Dyas 8 months ago
parent
commit
f6a6f85fad
  1. 92
      Day 12/Part_1.php
  2. 96
      Day 12/Part_2.php
  3. 2
      README.md

92
Day 12/Part_1.php

@ -46,76 +46,72 @@ function rotate($diretion, $times, $startig)
}
$in = file("input.txt", FILE_IGNORE_NEW_LINES);
function res_pos($pos)
{
if ($pos["N"] > $pos["S"]) {
$pos["N"] = $pos["N"] - $pos["S"];
$pos["S"] = 0;
}
if ($pos["S"] > $pos["N"]) {
$pos["S"] = $pos["S"] - $pos["N"];
$pos["N"] = 0;
}
if ($pos["N"] == $pos["S"]) {
$pos["N"] = 0;
$pos["S"] = 0;
}
if ($pos["E"] > $pos["W"]) {
$pos["E"] = $pos["E"] - $pos["W"];
$pos["W"] = 0;
}
if ($pos["W"] > $pos["E"]) {
$pos["W"] = $pos["W"] - $pos["E"];
$pos["E"] = 0;
}
if ($pos["E"] == $pos["W"]) {
$pos["E"] = 0;
$pos["W"] = 0;
}
return ($pos);
}
$n = 0;
$s = 0;
$e = 0;
$w = 0;
$d = "E";
$in = file("input.txt", FILE_IGNORE_NEW_LINES);
$pos = array("N" => 0, "S" => 0, "E" => 0, "W" => 0, "D" => "E");
foreach ($in as $stepk => $stepv) {
$dir = $stepv[0];
$v = (int)ltrim($stepv, "NSEWLRF");
switch ($dir) {
case "N":
$n = $n + $v;
break;
case "S":
$s = $s + $v;
break;
case "E":
$e = $e + $v;
break;
case "W":
$w = $w + $v;
$pos[$dir] = $pos[$dir] + $v;
$pos = res_pos($pos);
break;
case "R":
$d = rotate("R", $v / 90, $d);
$pos["D"] = rotate("R", $v / 90, $pos["D"]);
break;
case "L":
$d = rotate("L", $v / 90, $d);
$pos["D"] = rotate("L", $v / 90, $pos["D"]);
break;
case "F":
switch ($d) {
case "N":
$n = $n + $v;
break;
case "S":
$s = $s + $v;
break;
case "E":
$e = $e + $v;
break;
case "W":
$w = $w + $v;
break;
}
$pos[$pos["D"]] = $pos[$pos["D"]] + $v;
$pos = res_pos($pos);
break;
}
}
if ($n > $s) {
$n = $n - $s;
$s = 0;
}
if ($s > $n) {
$s = $s - $n;
$n = 0;
}
if ($e > $w) {
$e = $e - $w;
$w = 0;
}
if ($w > $e) {
$w = $w - $e;
$e = 0;
}
echo $n + $s + $e + $w . PHP_EOL;
echo $pos["N"] + $pos["S"] + $pos["E"] + $pos["W"] . PHP_EOL;

96
Day 12/Part_2.php

@ -0,0 +1,96 @@
<?php
function rotate($diretion, $times, $pos)
{
$x = 0;
while ($x < $times) {
$posnew = array("N" => 0, "S" => 0, "E" => 0, "W" => 0);
if ($diretion == "R") {
$posnew["N"] = $pos["W"];
$posnew["E"] = $pos["N"];
$posnew["S"] = $pos["E"];
$posnew["W"] = $pos["S"];
$pos = $posnew;
}
if ($diretion == "L") {
$posnew["N"] = $pos["E"];
$posnew["E"] = $pos["S"];
$posnew["S"] = $pos["W"];
$posnew["W"] = $pos["N"];
$pos = $posnew;
}
$x++;
}
return $pos;
}
function res_pos($pos)
{
if ($pos["N"] > $pos["S"]) {
$pos["N"] = $pos["N"] - $pos["S"];
$pos["S"] = 0;
}
if ($pos["S"] > $pos["N"]) {
$pos["S"] = $pos["S"] - $pos["N"];
$pos["N"] = 0;
}
if ($pos["N"] == $pos["S"]) {
$pos["N"] = 0;
$pos["S"] = 0;
}
if ($pos["E"] > $pos["W"]) {
$pos["E"] = $pos["E"] - $pos["W"];
$pos["W"] = 0;
}
if ($pos["W"] > $pos["E"]) {
$pos["W"] = $pos["W"] - $pos["E"];
$pos["E"] = 0;
}
if ($pos["E"] == $pos["W"]) {
$pos["E"] = 0;
$pos["W"] = 0;
}
return ($pos);
}
$in = file("input.txt", FILE_IGNORE_NEW_LINES);
$pos = array("N" => 0, "S" => 0, "E" => 0, "W" => 0);
$wap = array("N" => 1, "S" => 0, "E" => 10, "W" => 0);
foreach ($in as $stepk => $stepv) {
$dir = $stepv[0];
$v = (int)ltrim($stepv, "NSEWLRF");
switch ($dir) {
case "N":
case "S":
case "E":
case "W":
$wap[$dir] = $wap[$dir] + $v;
$wap = res_pos($wap);
break;
case "R":
case "L":
$wap = rotate($dir, $v / 90, $wap);
break;
case "F":
$pos["N"] = $pos["N"] + $wap["N"] * $v;
$pos["S"] = $pos["S"] + $wap["S"] * $v;
$pos["E"] = $pos["E"] + $wap["E"] * $v;
$pos["W"] = $pos["W"] + $wap["W"] * $v;
$pos = res_pos($pos);
break;
}
}
echo $pos["N"] + $pos["S"] + $pos["E"] + $pos["W"] . PHP_EOL;

2
README.md

@ -12,4 +12,4 @@
|9|:heavy_check_mark:|:heavy_check_mark:|
|10|:heavy_check_mark:|:heavy_check_mark:|
|11|:heavy_check_mark:|:heavy_check_mark:|
|12|:heavy_check_mark:|:x:|
|12|:heavy_check_mark:|:heavy_check_mark:|

Loading…
Cancel
Save