Skip to content

Instantly share code, notes, and snippets.

@simonhf
Created October 10, 2019 19:33
Show Gist options
  • Save simonhf/226ca99908a3c4d15dd19a5e5e8ecb13 to your computer and use it in GitHub Desktop.
Save simonhf/226ca99908a3c4d15dd19a5e5e8ecb13 to your computer and use it in GitHub Desktop.
You’re part of the team that explores Mars by sending remotely controlled vehicles to the surface of the planet. Develop an interface that translates the commands sent from earth to instructions that are understood by the rover.
use strict;
# Your Task
# You’re part of the team that explores Mars by sending remotely controlled vehicles to the surface of the planet. Develop an interface that translates the commands sent from earth to instructions that are understood by the rover.
# Requirements
# You are given the initial starting point (x,y) of a rover and the direction (N,S,E,W) it is facing.
# The rover receives a character array of commands as a string, for example "fffrfbrflb".
# Implement commands that move the rover forward/backward (f,b).
# Implement commands that turn the rover left/right (l,r).
# Examples
# Given a rover that starts at 0, 0 facing North, the command lffflbbb should result in a position of -3,3 facing South. Sending the command fff afterwards should end with the rover at -3,0 facing South.
# Given a rover that starts at 3, 4 facing East, the command fffrfbrflb should result in a position of 5, 5, facing South.
# Given a rover that starts at -5, 22 facing South, the command lrffbb should result in a position of -5, 22 facing South.
if ($ARGV[0] =~ m~test~i) {
require Test::More;
printf qq[- running rover tests\n];
my $in = 'starts at 0, 0 facing North\n' . 'lffflbbb\n' . 'quit\n'; my $out = `echo '$in' | perl rover.pl`; Test::More::like($out, qr~rover pos -3, 3 facing South after quit~, 'Rover at expected pos after test 1a');
my $in = 'starts at 0, 0 facing North\n' . 'lffflbbb\n' . 'fff\n' . 'quit\n'; my $out = `echo '$in' | perl rover.pl`; Test::More::like($out, qr~rover pos -3, 0 facing South after quit~, 'Rover at expected pos after test 1b');
my $in = 'starts at 3, 4 facing East\n' . 'fffrfbrflb\n' . 'quit\n'; my $out = `echo '$in' | perl rover.pl`; Test::More::like($out, qr~rover pos 5, 5 facing South after quit~, 'Rover at expected pos after test 2' );
my $in = 'starts at -5, 22 facing South\n' . 'lrffbb\n' . 'quit\n'; my $out = `echo '$in' | perl rover.pl`; Test::More::like($out, qr~rover pos -5, 22 facing South after quit~, 'Rover at expected pos after test 3' );
Test::More::done_testing(4);
exit;
}
my $pos_x;
my $pos_y;
my $pos_face;
my $pos_face_id;
my @pos_faces = (qw(North East South West)); # possible directions rover can face
my @pos_faces_x_inc = (qw( 0 1 0 -1)); # increments to move forward or backward in each direction
my @pos_faces_y_inc = (qw( 1 0 -1 0));
while (my $rover_command = <STDIN>) {
chomp $rover_command;
if ($rover_command =~ m~starts at \s*(-?\d+),\s*(-?\d+) facing (North|East|South|West)~i) {
($pos_x, $pos_y, $pos_face) = ($1, $2, $3);
$pos_face_id = 0; while (($pos_face_id <= $#pos_faces) && ($pos_faces[$pos_face_id] !~ m~$pos_face~i)) { $pos_face_id ++; } # find $pos_face_id
printf qq[- rover pos %2d, %2d facing %-5s after %-15s due to rover command '%s'\n], $pos_x, $pos_y, $pos_faces[$pos_face_id], 'hyper-space', $rover_command;
}
elsif ($rover_command =~ m~^[fblr]+$~i) {
# todo: add check for initial hyper-space
my $sub_rover_commands = length($rover_command);
foreach my $i (0..($sub_rover_commands - 1)) {
my $sub_rover_command = substr($rover_command, $i, 1);
my $operation;
if ($sub_rover_command =~ m~f~) { $operation = 'moving forward' ; $pos_x += $pos_faces_x_inc[$pos_face_id]; $pos_y += $pos_faces_y_inc[$pos_face_id]; }
elsif ($sub_rover_command =~ m~b~) { $operation = 'moving backward'; $pos_x -= $pos_faces_x_inc[$pos_face_id]; $pos_y -= $pos_faces_y_inc[$pos_face_id]; }
elsif ($sub_rover_command =~ m~l~) { $operation = 'turning left' ; $pos_face_id --; $pos_face_id = $pos_face_id & 3; }
elsif ($sub_rover_command =~ m~r~) { $operation = 'turning right' ; $pos_face_id ++; $pos_face_id = $pos_face_id & 3; }
printf qq[- rover pos %2d, %2d facing %-5s after %-15s due to rover command '%s' and sub rover command '%s'\n], $pos_x, $pos_y, $pos_faces[$pos_face_id], $operation, $rover_command, $sub_rover_command;
}
}
elsif ($rover_command =~ m~quit~i) { printf qq[- rover pos %2d, %2d facing %-5s after %-15s due to rover command '%s'\n], $pos_x, $pos_y, $pos_faces[$pos_face_id], 'quit' , $rover_command; exit(0); }
else { printf qq[- rover pos %2d, %2d facing %-5s after %-15s due to rover command '%s'\n], $pos_x, $pos_y, $pos_faces[$pos_face_id], 'syntax error', $rover_command; exit(1); }
}
# $ perl rover.pl test
# - running rover tests
# ok 1 - Rover at expected position after test 1a
# ok 2 - Rover at expected position after test 1b
# ok 3 - Rover at expected position after test 2
# ok 4 - Rover at expected position after test 3
# 1..4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment