项目作者: ijverig

项目描述 :
Mars Rover problem
高级语言: Elixir
项目地址: git://github.com/ijverig/MarsRover.git
创建时间: 2018-04-23T08:57:57Z
项目社区:https://github.com/ijverig/MarsRover

开源协议:

下载


Mars Rover Problem

This is a library to process rovers deployments.

It receives rovers initial positions, commands for each rover, a plateau specification and deploys each rover, simulating the plateau exploration and final positions.

Instalation

  1. mix deps.get
  2. mix test
  3. mix escript.build

Usage

It can both be used as a library:

  1. import MarsRover
  2. plateau = {2, 2}
  3. deployments = [
  4. {{0, 0, :north}, [:move, :move]},
  5. {{0, 0, :west}, [:left, :move]}
  6. ]
  7. MarsRover.deploy(plateau, deployments)
  8. # [
  9. # {:ok, {0, 2, :north}},
  10. # {:error, :off_plateau}
  11. # ]

or as a command line app:

  1. ./mars_rover --help
  2. Process rovers in a given grid.
  3. Usage:
  4. mars_rover --help # Displays this usage information
  5. mars_rover --version # Displays the version of this app
  6. mars_rover [options] # Process the standard input
  7. mars_rover [options] <file> # Process the input file
  8. Options:
  9. -d, --draw # Draw deployment results

Examples

"mars_rover" output

"mars_rover --draw" output

"mars_rover file" output

Details

Why elixir? Well, pattern matching makes it ridiculously easy to solve the problem…

Both parsing and processing commands are done in a very readable and straight way.

Development was done making input test cases. All input limits are handled as tests.

Tests have dozens of inputs. You can find several examples of how it all works in the tests.

++ concatenation is used when parsing for readability sake. If the input were to be really large then it would be better to use Tail Call Optimization instead.

Another approach to the problem would be to have it all as concurrent processes (rovers, CLI, plateau and parsing) interacting with each other.

Bonus

A WIP branch with the option to animate output results.

"mars_rover --animate file" output