Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $inputs = explode(", ","R4, R1, L2, R1, L1, L1, R1, L5, R1, R5, L2, R3, L3, L4, R4, R4, R3, L5, L1, R5, R3, L4, R1, R5, L1, R3, L2, R3, R1, L4, L1, R1, L1, L5, R1, L2, R2, L3, L5, R1, R5, L1, R188, L3, R2, R52, R5, L3, R79, L1, R5, R186, R2, R1, L3, L5, L2, R2, R4, R5, R5, L5, L4, R5, R3, L4, R4, L4, L4, R5, L4, L3, L1, L4, R1, R2, L5, R3, L4, R3, L3, L5, R1, R1, L3, R2, R1, R2, R2, L4, R5, R1, R3, R2, L2, L2, L1, R2, L1, L3, R5, R1, R4, R5, R2, R2, R4, R4, R1, L3, R4, L2, R2, R1, R3, L5, R5, R2, R5, L1, R2, R4, L1, R5, L3, L3, R1, L4, R2, L2, R1, L1, R4, R3, L2, L3, R3, L2, R1, L4, R5, L1, R5, L2, L1, L5, L2, L5, L2, L4, L2, R3");
- $directions = ['N', 'E', 'S', 'W'];
- $path = [];
- $cursor = [
- "x" => 0,
- "y" => 0
- ];
- $direction = 0;
- foreach ($inputs as $value) {
- $rotation = substr($value, 0 , 1);
- $distance = (int)substr($value, 1);
- switch ($rotation) {
- case 'R':
- $direction++;
- break;
- case 'L':
- $direction--;
- break;
- }
- switch($directions[$direction % 4]) {
- case 'N':
- for ($i=0; $i < $distance; $i++) {
- $cursor["y"]++;
- if (hasBeenHereBefore($cursor, $path)) return false;
- array_push($path, $cursor);
- }
- break;
- case 'E':
- for ($i=0; $i < $distance; $i++) {
- $cursor["x"] ++;
- if (hasBeenHereBefore($cursor, $path)) return false;
- array_push($path, $cursor);
- }
- break;
- case 'S':
- for ($i=0; $i < $distance; $i++) {
- $cursor["y"] --;
- if (hasBeenHereBefore($cursor, $path)) return false;
- array_push($path, $cursor);
- }
- break;
- case 'W':
- for ($i=0; $i < $distance; $i++) {
- $cursor["x"] --;
- if (hasBeenHereBefore($cursor, $path)) return false;
- array_push($path, $cursor);
- }
- break;
- }
- }
- function hasBeenHereBefore($cursor, $path) {
- foreach ($path as $value) {
- if($cursor['x'] === $value['x']){
- if($cursor['y'] === $value['y']){
- var_dump($value['x']);
- var_dump($value['y']);
- return true;
- }
- }
- }
- return false;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement