Created
December 2, 2016 19:55
-
-
Save sylvainfilteau/96ce2f9ed70c0e2c98f559ebda7f8dd4 to your computer and use it in GitHub Desktop.
This file contains 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 | |
class Position { | |
private $compass; | |
private $x; | |
private $y; | |
public function __construct() { | |
$this->compass = Compass::facingNorth(); | |
$this->x = 0; | |
$this->y = 0; | |
} | |
public function walk($direction, $number_of_blocks) { | |
$this->compass->turn($direction); | |
$orientation = $this->compass->readOrientation(); | |
$vertical = [ | |
Compass::ORI_NORTH, | |
Compass::ORI_SOUTH | |
]; | |
$negative = [ | |
Compass::ORI_SOUTH, | |
Compass::ORI_WEST | |
]; | |
$inc = $number_of_blocks; | |
if (in_array($orientation, $negative)) { | |
$inc = -$inc; | |
} | |
if (in_array($orientation, $vertical)) { | |
$this->y += $inc; | |
} else { | |
$this->x += $inc; | |
} | |
} | |
public function toString() { | |
return sprintf("x: %s, y: %s", $this->x, $this->y); | |
} | |
public function distanceInBlockFromOrigin() { | |
return abs($this->x) + abs($this->y); | |
} | |
} | |
class Compass { | |
const ORI_NORTH = "N"; | |
const ORI_SOUTH = "S"; | |
const ORI_EAST = "E"; | |
const ORI_WEST = "W"; | |
const DIR_LEFT = "L"; | |
const DIR_RIGHT = "R"; | |
private $orientation; | |
private function __construct($orientation) { | |
$this->orientation = $orientation; | |
} | |
public function turn($direction) { | |
if (is_null($direction)) { | |
return; | |
} | |
$orientation_list = [ | |
self::ORI_NORTH, | |
self::ORI_EAST, | |
self::ORI_SOUTH, | |
self::ORI_WEST | |
]; | |
$orientation_position = [ | |
self::ORI_NORTH => 0, | |
self::ORI_EAST => 1, | |
self::ORI_SOUTH => 2, | |
self::ORI_WEST => 3 | |
]; | |
$current_pos = $orientation_position[$this->orientation]; | |
$inc = 1; | |
if ($direction == self::DIR_LEFT) { | |
$inc = -1; | |
} | |
$new_pos = $current_pos + $inc; | |
if ($new_pos < 0) { | |
$new_pos = 3; | |
} elseif ($new_pos > 3) { | |
$new_pos = 0; | |
} | |
$this->orientation = $orientation_list[$new_pos]; | |
} | |
public function readOrientation() { | |
return $this->orientation; | |
} | |
public static function facingNorth() { | |
return new self(self::ORI_NORTH); | |
} | |
} | |
$input = explode(", ", "R1, R1, R3, R1, R1, L2, R5, L2, R5, R1, R4, L2, R3, L3, R4, L5, R4, R4, R1, L5, L4, R5, R3, L1, R4, R3, L2, L1, R3, L4, R3, L2, R5, R190, R3, R5, L5, L1, R54, L3, L4, L1, R4, R1, R3, L1, L1, R2, L2, R2, R5, L3, R4, R76, L3, R4, R191, R5, R5, L5, L4, L5, L3, R1, R3, R2, L2, L2, L4, L5, L4, R5, R4, R4, R2, R3, R4, L3, L2, R5, R3, L2, L1, R2, L3, R2, L1, L1, R1, L3, R5, L5, L1, L2, R5, R3, L3, R3, R5, R2, R5, R5, L5, L5, R2, L3, L5, L2, L1, R2, R2, L2, R2, L3, L2, R3, L5, R4, L4, L5, R3, L4, R1, R3, R2, R4, L2, L3, R2, L5, R5, R4, L2, R4, L1, L3, L1, L3, R1, R2, R1, L5, R5, R3, L3, L3, L2, R4, R2, L5, L1, L1, L5, L4, L1, L1, R1"); | |
//$input = ["R8", "R4", "R4", "R8"]; | |
$position = new Position(); | |
$positions = []; | |
$first_position_visited_twice_found = false; | |
foreach ($input as $walk) { | |
$direction = substr($walk, 0, 1); | |
$number = substr($walk, 1); | |
for ($i = 0; $i < $number; $i++) { | |
$position->walk($direction, 1); | |
$direction = null; | |
if (!$first_position_visited_twice_found && in_array($position->toString(), $positions)) { | |
var_dump($positions); | |
echo sprintf('Coordinate of first place visited twice: %s, distance: %s', $position->toString(), $position->distanceInBlockFromOrigin()); | |
echo "\n"; | |
$first_position_visited_twice_found = true; | |
} | |
$positions[] = $position->toString(); | |
} | |
} | |
echo $position->toString(); | |
echo "\n"; | |
echo sprintf("Distance: %s", $position->distanceInBlockFromOrigin()); | |
echo "\n"; |
This file contains 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 | |
$current_pos = 5; | |
$upper_edges = [1, 2, 3]; | |
$lower_edges = [7, 8, 9]; | |
$left_edges = [1, 4, 7]; | |
$right_edge = [3, 6, 9]; | |
// 1 | 2 | 3 | |
// 4 | 5 | 6 | |
// 7 | 8 | 9 | |
$inc_list = [ | |
'U' => -3, | |
'D' => 3, | |
'L' => -1, | |
'R' => 1 | |
]; | |
$code = []; | |
$inputs = explode("\n", "UDRLRRRUULUUDULRULUDRDRURLLDUUDURLUUUDRRRLUUDRUUDDDRRRLRURLLLDDDRDDRUDDULUULDDUDRUUUDLRLLRLDUDUUUUDLDULLLDRLRLRULDDDDDLULURUDURDDLLRDLUDRRULDURDDLUDLLRRUDRUDDDLLURULRDDDRDRRLLUUDDLLLLRLRUULRDRURRRLLLLDULDDLRRRRUDRDULLLDDRRRDLRLRRRLDRULDUDDLDLUULRDDULRDRURRURLDULRUUDUUURDRLDDDURLDURLDUDURRLLLLRDDLDRUURURRRRDRRDLUULLURRDLLLDLDUUUDRDRULULRULUUDDULDUURRLRLRRDULDULDRUUDLLUDLLLLUDDULDLLDLLURLLLRUDRDLRUDLULDLLLUDRLRLUDLDRDURDDULDURLLRRRDUUDLRDDRUUDLUURLDRRRRRLDDUUDRURUDLLLRRULLRLDRUURRRRRLRLLUDDRLUDRRDUDUUUDRUDULRRULRDRRRDDRLUUUDRLLURURRLLDUDRUURDLRURLLRDUDUUDLLLUULLRULRLDLRDDDU | |
DRRRDRUDRLDUUDLLLRLULLLUURLLRLDRLURDRDRDRLDUUULDRDDLDDDURURUDRUUURDRDURLRLUDRRRDURDRRRDULLRDRRLUUUURLRUULRRDUDDDDUURLDULUDLLLRULUDUURRDUULRRDDURLURRUDRDRLDLRLLULULURLRDLRRRUUURDDUUURDRDRUURUDLULDRDDULLLLLRLRLLUDDLULLUDDLRLRDLDULURDUDULRDDRLUDUUDUDRLLDRRLLDULLRLDURUDRLRRRDULUUUULRRLUDDDLDUUDULLUUURDRLLULRLDLLUUDLLUULUULUDLRRDDRLUUULDDRULDRLURUURDLURDDRULLLLDUDULUDURRDRLDDRRLRURLLRLLLLDURDLUULDLDDLULLLRDRRRDLLLUUDDDLDRRLUUUUUULDRULLLDUDLDLURLDUDULRRRULDLRRDRUUUUUURRDRUURLDDURDUURURULULLURLLLLUURDUDRRLRRLRLRRRRRULLDLLLRURRDULLDLLULLRDUULDUDUDULDURLRDLDRUUURLLDLLUUDURURUD | |
UDUUUUURUDLLLRRRDRDRUDDRLLDRRLDRLLUURRULUULULRLLRUDDRLDRLUURDUDLURUULLLULLRRRULRLURRDDULLULULRUDDDUURDRLUDUURRRRUUULLRULLLDLURUDLDDLLRRRULDLLUURDRRRDRDURURLRUDLDLURDDRLLLUUDRUULLDLLLLUUDRRURLDDUDULUDLDURDLURUURDUUUURDLLLRUUURDUUUDLDUDDLUDDUDUDUDLDUDUUULDULUURDDLRRRULLUDRRDLUDULDURUURULLLLUDDDLURURLRLRDLRULRLULURRLLRDUDUDRULLRULRUDLURUDLLDUDLRDRLRDURURRULLDDLRLDDRLRDRRDLRDDLLLLDUURRULLRLLDDLDLURLRLLDULRURRRRDULRLRURURRULULDUURRDLURRDDLDLLLRULRLLURLRLLDDLRUDDDULDLDLRLURRULRRLULUDLDUDUDDLLUURDDDLULURRULDRRDDDUUURLLDRDURUDRUDLLDRUD | |
ULRDULURRDDLULLDDLDDDRLDUURDLLDRRRDLLURDRUDDLDURUDRULRULRULULUULLLLDRLRLDRLLLLLRLRRLRLRRRDDULRRLUDLURLLRLLURDDRRDRUUUDLDLDRRRUDLRUDDRURRDUUUDUUULRLDDRDRDRULRLLDLDDLLRLUDLLLLUURLDLRUDRLRDRDRLRULRDDURRLRUDLRLRLDRUDURLRDLDULLUUULDRLRDDRDUDLLRUDDUDURRRRDLDURRUURDUULLDLRDUDDLUDDDRRRULRLULDRLDDRUURURLRRRURDURDRULLUUDURUDRDRLDLURDDDUDDURUDLRULULURRUULDRLDULRRRRDUULLRRRRLUDLRDDRLRUDLURRRDRDRLLLULLUULRDULRDLDUURRDULLRULRLRRURDDLDLLRUUDLRLDLRUUDLDDLLULDLUURRRLRDULRLRLDRLDUDURRRLLRUUDLUURRDLDDULDLULUUUUDRRULLLLLLUULDRULDLRUDDDRDRDDURUURLURRDLDDRUURULLULUUUDDLRDULDDLULDUDRU | |
LRLRLRLLLRRLUULDDUUUURDULLLRURLDLDRURRRUUDDDULURDRRDURLRLUDLLULDRULLRRRDUUDDRDRULLDDULLLUURDLRLRUURRRLRDLDUDLLRLLURLRLLLDDDULUDUDRDLRRLUDDLRDDURRDRDUUULLUURURLRRDUURLRDLLUDURLRDRLURUURDRLULLUUUURRDDULDDDRULURUULLUDDDDLRURDLLDRURDUDRRLRLDLRRDDRRDDRUDRDLUDDDLUDLUDLRUDDUDRUDLLRURDLRUULRUURULUURLRDULDLDLLRDRDUDDDULRLDDDRDUDDRRRLRRLLRRRUUURRLDLLDRRDLULUUURUDLULDULLLDLULRLRDLDDDDDDDLRDRDUDLDLRLUDRRDRRDRUURDUDLDDLUDDDDDDRUURURUURLURLDULUDDLDDLRUUUULRDRLUDLDDLLLRLLDRRULULRLRDURRRLDDRDDRLU"); | |
foreach ($inputs as $line) { | |
for ($i = 0; $i < strlen($line); $i++) { | |
$char = $line[$i]; | |
if ($char == 'U' && in_array($current_pos, $upper_edges)) { | |
continue; | |
} | |
if ($char == 'D' && in_array($current_pos, $lower_edges)) { | |
continue; | |
} | |
if ($char == 'L' && in_array($current_pos, $left_edges)) { | |
continue; | |
} | |
if ($char == 'R' && in_array($current_pos, $right_edge)) { | |
continue; | |
} | |
$inc = $inc_list[$char]; | |
$current_pos += $inc; | |
} | |
$code[] = $current_pos; | |
} | |
var_dump($code); |
This file contains 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 | |
class Keypad { | |
private $keys = []; | |
private function __construct(array $keys) { | |
$this->keys = $keys; | |
} | |
public static function createDiamondShapedKeypad() { | |
return new self([ | |
[null, null, 1, null, null], | |
[null, 2, 3, 4, null], | |
[ 5, 6, 7, 8, 9 ], | |
[null, 'A', 'B', 'C', null], | |
[null, null, 'D', null, null] | |
]); | |
} | |
public function move(Position $position, $direction) { | |
$new_position = $position->move($direction); | |
$x = $new_position->x(); | |
$y = $new_position->y(); | |
if ($x < 0 || $y < 0 || $x > 4 || $y > 4) { | |
return $position; | |
} | |
if (is_null($this->keys[$x][$y])) { | |
return $position; | |
} | |
return $new_position; | |
} | |
public function getAt(Position $position) { | |
return $this->keys[$position->x()][$position->y()]; | |
} | |
public function createInitialPosition() { | |
return new Position(0, 2); | |
} | |
} | |
class Position { | |
const DIR_UP = "U"; | |
const DIR_DOWN = "D"; | |
const DIR_LEFT = "L"; | |
const DIR_RIGHT = "R"; | |
private $x; | |
private $y; | |
public function __construct($x, $y) { | |
$this->x = $x; | |
$this->y = $y; | |
} | |
public function move($direction) { | |
if (self::DIR_DOWN == $direction) { | |
return new static($this->x + 1, $this->y); | |
} | |
if (self::DIR_UP == $direction) { | |
return new static($this->x - 1, $this->y); | |
} | |
if (self::DIR_LEFT == $direction) { | |
return new static($this->x, $this->y - 1); | |
} | |
if (self::DIR_RIGHT == $direction) { | |
return new static($this->x, $this->y + 1); | |
} | |
} | |
public function x() { | |
return $this->x; | |
} | |
public function y() { | |
return $this->y; | |
} | |
} | |
$keypad = Keypad::createDiamondShapedKeypad(); | |
$position = $keypad->createInitialPosition(); | |
$code = []; | |
$inputs = explode("\n", "UDRLRRRUULUUDULRULUDRDRURLLDUUDURLUUUDRRRLUUDRUUDDDRRRLRURLLLDDDRDDRUDDULUULDDUDRUUUDLRLLRLDUDUUUUDLDULLLDRLRLRULDDDDDLULURUDURDDLLRDLUDRRULDURDDLUDLLRRUDRUDDDLLURULRDDDRDRRLLUUDDLLLLRLRUULRDRURRRLLLLDULDDLRRRRUDRDULLLDDRRRDLRLRRRLDRULDUDDLDLUULRDDULRDRURRURLDULRUUDUUURDRLDDDURLDURLDUDURRLLLLRDDLDRUURURRRRDRRDLUULLURRDLLLDLDUUUDRDRULULRULUUDDULDUURRLRLRRDULDULDRUUDLLUDLLLLUDDULDLLDLLURLLLRUDRDLRUDLULDLLLUDRLRLUDLDRDURDDULDURLLRRRDUUDLRDDRUUDLUURLDRRRRRLDDUUDRURUDLLLRRULLRLDRUURRRRRLRLLUDDRLUDRRDUDUUUDRUDULRRULRDRRRDDRLUUUDRLLURURRLLDUDRUURDLRURLLRDUDUUDLLLUULLRULRLDLRDDDU | |
DRRRDRUDRLDUUDLLLRLULLLUURLLRLDRLURDRDRDRLDUUULDRDDLDDDURURUDRUUURDRDURLRLUDRRRDURDRRRDULLRDRRLUUUURLRUULRRDUDDDDUURLDULUDLLLRULUDUURRDUULRRDDURLURRUDRDRLDLRLLULULURLRDLRRRUUURDDUUURDRDRUURUDLULDRDDULLLLLRLRLLUDDLULLUDDLRLRDLDULURDUDULRDDRLUDUUDUDRLLDRRLLDULLRLDURUDRLRRRDULUUUULRRLUDDDLDUUDULLUUURDRLLULRLDLLUUDLLUULUULUDLRRDDRLUUULDDRULDRLURUURDLURDDRULLLLDUDULUDURRDRLDDRRLRURLLRLLLLDURDLUULDLDDLULLLRDRRRDLLLUUDDDLDRRLUUUUUULDRULLLDUDLDLURLDUDULRRRULDLRRDRUUUUUURRDRUURLDDURDUURURULULLURLLLLUURDUDRRLRRLRLRRRRRULLDLLLRURRDULLDLLULLRDUULDUDUDULDURLRDLDRUUURLLDLLUUDURURUD | |
UDUUUUURUDLLLRRRDRDRUDDRLLDRRLDRLLUURRULUULULRLLRUDDRLDRLUURDUDLURUULLLULLRRRULRLURRDDULLULULRUDDDUURDRLUDUURRRRUUULLRULLLDLURUDLDDLLRRRULDLLUURDRRRDRDURURLRUDLDLURDDRLLLUUDRUULLDLLLLUUDRRURLDDUDULUDLDURDLURUURDUUUURDLLLRUUURDUUUDLDUDDLUDDUDUDUDLDUDUUULDULUURDDLRRRULLUDRRDLUDULDURUURULLLLUDDDLURURLRLRDLRULRLULURRLLRDUDUDRULLRULRUDLURUDLLDUDLRDRLRDURURRULLDDLRLDDRLRDRRDLRDDLLLLDUURRULLRLLDDLDLURLRLLDULRURRRRDULRLRURURRULULDUURRDLURRDDLDLLLRULRLLURLRLLDDLRUDDDULDLDLRLURRULRRLULUDLDUDUDDLLUURDDDLULURRULDRRDDDUUURLLDRDURUDRUDLLDRUD | |
ULRDULURRDDLULLDDLDDDRLDUURDLLDRRRDLLURDRUDDLDURUDRULRULRULULUULLLLDRLRLDRLLLLLRLRRLRLRRRDDULRRLUDLURLLRLLURDDRRDRUUUDLDLDRRRUDLRUDDRURRDUUUDUUULRLDDRDRDRULRLLDLDDLLRLUDLLLLUURLDLRUDRLRDRDRLRULRDDURRLRUDLRLRLDRUDURLRDLDULLUUULDRLRDDRDUDLLRUDDUDURRRRDLDURRUURDUULLDLRDUDDLUDDDRRRULRLULDRLDDRUURURLRRRURDURDRULLUUDURUDRDRLDLURDDDUDDURUDLRULULURRUULDRLDULRRRRDUULLRRRRLUDLRDDRLRUDLURRRDRDRLLLULLUULRDULRDLDUURRDULLRULRLRRURDDLDLLRUUDLRLDLRUUDLDDLLULDLUURRRLRDULRLRLDRLDUDURRRLLRUUDLUURRDLDDULDLULUUUUDRRULLLLLLUULDRULDLRUDDDRDRDDURUURLURRDLDDRUURULLULUUUDDLRDULDDLULDUDRU | |
LRLRLRLLLRRLUULDDUUUURDULLLRURLDLDRURRRUUDDDULURDRRDURLRLUDLLULDRULLRRRDUUDDRDRULLDDULLLUURDLRLRUURRRLRDLDUDLLRLLURLRLLLDDDULUDUDRDLRRLUDDLRDDURRDRDUUULLUURURLRRDUURLRDLLUDURLRDRLURUURDRLULLUUUURRDDULDDDRULURUULLUDDDDLRURDLLDRURDUDRRLRLDLRRDDRRDDRUDRDLUDDDLUDLUDLRUDDUDRUDLLRURDLRUULRUURULUURLRDULDLDLLRDRDUDDDULRLDDDRDUDDRRRLRRLLRRRUUURRLDLLDRRDLULUUURUDLULDULLLDLULRLRDLDDDDDDDLRDRDUDLDLRLUDRRDRRDRUURDUDLDDLUDDDDDDRUURURUURLURLDULUDDLDDLRUUUULRDRLUDLDDLLLRLLDRRULULRLRDURRRLDDRDDRLU"); | |
foreach ($inputs as $line) { | |
for ($i = 0; $i < strlen($line); $i++) { | |
$char = $line[$i]; | |
$position = $keypad->move($position, $char); | |
} | |
$code[] = $keypad->getAt($position); | |
} | |
var_dump($code); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You day 1 and Day2-part 2 ressemble my implementation