mars_rover 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e5d0c5c645902f854ba20db6dc8bac53920a4205
4
+ data.tar.gz: a1a945343446ab3c411064c926667b7c2c85c745
5
+ SHA512:
6
+ metadata.gz: fa0d477c2d7ea033a9728bd9889fda4e3c71f71218ddf9d1b2f4153ae9a52f73a97b8e0b38c2791f18f1d1e80cb86a760a272ac4f27a832711ad9f702ea263ad
7
+ data.tar.gz: fe618d478cf9d26bcc6f6bfa0a3f574268c74926e90b7f1ed1034ba6d3c41746fecac49316bc1bbbb75cadaca7e267407e69126a6f5f2e49de9d569f32527012
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mars_rover.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # MarsRover
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mars_rover'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mars_rover
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/[my-github-username]/mars_rover/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = "test/*_test.rb"
7
+ end
8
+
data/bin/mars_rover ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'mars_rover'
4
+ MarsRover::Runner.new(ARGV[0])
@@ -0,0 +1,29 @@
1
+ module MarsRover
2
+ class Direction
3
+ def self.turn_right(rover)
4
+ case rover.direction
5
+ when 'N'
6
+ rover.direction = 'E'
7
+ when 'E'
8
+ rover.direction = 'S'
9
+ when 'S'
10
+ rover.direction = 'W'
11
+ when 'W'
12
+ rover.direction = 'N'
13
+ end
14
+ end
15
+
16
+ def self.turn_left(rover)
17
+ case rover.direction
18
+ when 'N'
19
+ rover.direction = 'W'
20
+ when 'W'
21
+ rover.direction = 'S'
22
+ when 'S'
23
+ rover.direction = 'E'
24
+ when 'E'
25
+ rover.direction = 'N'
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,14 @@
1
+ module MarsRover
2
+ class Plateau
3
+
4
+ attr_accessor :x_upper_limit, :y_upper_limit, :x_lower_limit, :y_lower_limit
5
+
6
+ def initialize(x_upper_limit, y_upper_limit)
7
+ self.x_upper_limit = x_upper_limit
8
+ self.y_upper_limit = y_upper_limit
9
+ self.x_lower_limit = 0
10
+ self.y_lower_limit = 0
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,11 @@
1
+ module MarsRover
2
+ class Position
3
+ def self.move(rover)
4
+ direction = rover.direction
5
+ rover.x += 1 if direction == 'E'
6
+ rover.y += 1 if direction == 'N'
7
+ rover.x -= 1 if direction == 'W'
8
+ rover.y -= 1 if direction == 'S'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,53 @@
1
+ require_relative './direction'
2
+ require_relative './position'
3
+
4
+ module MarsRover
5
+
6
+ class RoverError < StandardError; end
7
+
8
+ class Rover
9
+
10
+ attr_accessor :x, :y, :direction, :plateau
11
+
12
+ def initialize(x, y, direction, plateau)
13
+ self.x = x
14
+ self.y = y
15
+ self.direction = direction
16
+ self.plateau = plateau
17
+
18
+ raise RoverError unless valid_position?
19
+ end
20
+
21
+ def turn_right
22
+ MarsRover::Direction.turn_right(self)
23
+ end
24
+
25
+ def turn_left
26
+ MarsRover::Direction.turn_left(self)
27
+ end
28
+
29
+ def move
30
+ MarsRover::Position.move(self)
31
+ end
32
+
33
+ def run_command(command)
34
+ case command
35
+ when 'L'
36
+ turn_left
37
+ when 'R'
38
+ turn_right
39
+ when 'M'
40
+ move
41
+ end
42
+ end
43
+
44
+ private
45
+ def valid_position?
46
+ x <= plateau.x_upper_limit &&
47
+ x >= plateau.x_lower_limit &&
48
+ y <= plateau.y_upper_limit &&
49
+ y >= plateau.y_lower_limit
50
+ end
51
+ end
52
+
53
+ end
@@ -0,0 +1,30 @@
1
+ module MarsRover
2
+ class Runner
3
+
4
+ attr_accessor :plateau, :rovers
5
+
6
+ def initialize(file_path)
7
+ file = File.open(File.expand_path(file_path), 'r')
8
+ output_file = File.open('ouput.txt', 'w')
9
+ plateu_coordinates = file.readline.split(' ')
10
+ @rovers = []
11
+ @plateau = MarsRover::Plateau.new(plateu_coordinates[0].to_i, plateu_coordinates[1].to_i)
12
+
13
+ while !file.eof?
14
+ rover_position_line = file.readline.split(' ')
15
+ rover_command_line = file.readline.strip
16
+ rover = MarsRover::Rover.new(rover_position_line[0].to_i,
17
+ rover_position_line[1].to_i,
18
+ rover_position_line[2], plateau)
19
+ rover_command_line.each_char do |command|
20
+ rover.run_command(command)
21
+ end
22
+
23
+ @rovers << rover
24
+ output = "#{rover.x} #{rover.y} #{rover.direction}\n"
25
+ output_file.write output
26
+ puts output
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,3 @@
1
+ module MarsRover
2
+ VERSION = "0.0.1"
3
+ end
data/lib/mars_rover.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "mars_rover/version"
2
+ require "mars_rover/rover"
3
+ require "mars_rover/plateau"
4
+ require "mars_rover/runner"
5
+
6
+ module MarsRover
7
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mars_rover/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mars_rover"
8
+ spec.version = MarsRover::VERSION
9
+ spec.authors = ["Paulo Diniz"]
10
+ spec.email = ["paulodiniz@gmail.com"]
11
+ spec.summary = %q{Mars Landing Rover solution}
12
+ spec.description = %q{Solving the Thoughtworks Mars Landing Rover problem}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "minitest"
24
+ spec.add_development_dependency "activesupport"
25
+ spec.add_development_dependency "pry"
26
+ end
data/test/.DS_Store ADDED
Binary file
@@ -0,0 +1,5 @@
1
+ 5 5
2
+ 1 2 N
3
+ LMLMLMLMM
4
+ 3 3 E
5
+ MMRMMRMRRM
@@ -0,0 +1,11 @@
1
+ require_relative './test_helper'
2
+
3
+ class PlateauTest < ActiveSupport::TestCase
4
+ test 'initialzing with' do
5
+ plateau = MarsRover::Plateau.new(45, 76)
6
+ assert_equal 45, plateau.x_upper_limit
7
+ assert_equal 76, plateau.y_upper_limit
8
+ assert_equal 0, plateau.x_lower_limit
9
+ assert_equal 0, plateau.y_lower_limit
10
+ end
11
+ end
@@ -0,0 +1,123 @@
1
+ require_relative './test_helper'
2
+
3
+ class RoverTest < ActiveSupport::TestCase
4
+
5
+ test 'initializing' do
6
+ plateu = MarsRover::Plateau.new(50, 50)
7
+
8
+ rover = MarsRover::Rover.new(27, 30, 'N', plateu)
9
+ assert_equal 27, rover.x
10
+ assert_equal 30, rover.y
11
+ assert_equal 'N', rover.direction
12
+
13
+ assert_equal plateu, rover.plateau
14
+ end
15
+
16
+ test 'cannot intialize a rover on a wrong position on plateau' do
17
+ plateau = MarsRover::Plateau.new(5, 5)
18
+
19
+ assert_raises MarsRover::RoverError do
20
+ rover = MarsRover::Rover.new(60, 60, 'S', plateau)
21
+ end
22
+ end
23
+
24
+ test 'spinnign rover to the right' do
25
+ plateau = MarsRover::Plateau.new(50, 50)
26
+ rover = MarsRover::Rover.new(27, 30, 'N', plateau)
27
+ rover.turn_right
28
+ assert_equal 'E', rover.direction
29
+ rover.turn_right
30
+ assert_equal 'S', rover.direction
31
+ rover.turn_right
32
+ assert_equal 'W', rover.direction
33
+ rover.turn_right
34
+ assert_equal 'N', rover.direction
35
+ end
36
+
37
+ test 'spinning rover to the left' do
38
+ plateau = MarsRover::Plateau.new(50, 50)
39
+ rover = MarsRover::Rover.new(27, 30, 'E', plateau)
40
+ rover.turn_left
41
+ assert_equal 'N', rover.direction
42
+ rover.turn_left
43
+ assert_equal 'W', rover.direction
44
+ rover.turn_left
45
+ assert_equal 'S', rover.direction
46
+ rover.turn_left
47
+ assert_equal 'E', rover.direction
48
+ end
49
+
50
+ test 'moving to East' do
51
+ plateau = MarsRover::Plateau.new(50, 50)
52
+ rover = MarsRover::Rover.new(27, 30, 'E', plateau)
53
+
54
+ rover.move
55
+ assert_equal 'E', rover.direction
56
+ assert_equal 28, rover.x
57
+ assert_equal 30, rover.y
58
+
59
+ rover.move
60
+ assert_equal 'E', rover.direction
61
+ assert_equal 29, rover.x
62
+ assert_equal 30, rover.y
63
+ end
64
+
65
+ test 'moving to North' do
66
+ plateau = MarsRover::Plateau.new(50, 50)
67
+ rover = MarsRover::Rover.new(5, 15, 'N', plateau)
68
+ rover.move
69
+
70
+ assert_equal 'N', rover.direction
71
+ assert_equal 5, rover.x
72
+ assert_equal 16, rover.y
73
+
74
+ rover.move
75
+
76
+ assert_equal 'N', rover.direction
77
+ assert_equal 5, rover.x
78
+ assert_equal 17, rover.y
79
+ end
80
+
81
+ test 'moving West' do
82
+ plateau = MarsRover::Plateau.new(50, 50)
83
+ rover = MarsRover::Rover.new(5, 15, 'W', plateau)
84
+ rover.move
85
+
86
+ assert_equal 'W', rover.direction
87
+ assert_equal 4, rover.x
88
+ assert_equal 15, rover.y
89
+ end
90
+
91
+ test 'moving South' do
92
+ plateau = MarsRover::Plateau.new(50, 50)
93
+ rover = MarsRover::Rover.new(5, 15, 'S', plateau)
94
+ rover.move
95
+
96
+ assert_equal 'S', rover.direction
97
+ assert_equal 5, rover.x
98
+ assert_equal 14, rover.y
99
+ end
100
+
101
+ test 'spinning and moving' do
102
+ plateau = MarsRover::Plateau.new(50, 50)
103
+ rover = MarsRover::Rover.new(5, 15, 'S', plateau)
104
+ rover.turn_right
105
+ rover.move
106
+
107
+ assert_equal 'W', rover.direction
108
+ assert_equal 4, rover.x
109
+ assert_equal 15, rover.y
110
+
111
+ rover.turn_left
112
+
113
+ assert_equal 'S', rover.direction
114
+ assert_equal 4, rover.x
115
+ assert_equal 15, rover.y
116
+
117
+ rover.move
118
+ assert_equal 'S', rover.direction
119
+ assert_equal 4, rover.x
120
+ assert_equal 14, rover.y
121
+ end
122
+
123
+ end
@@ -0,0 +1,18 @@
1
+ require_relative './test_helper'
2
+
3
+ class RunnerTest < ActiveSupport::TestCase
4
+
5
+ test 'initializing' do
6
+ runner = MarsRover::Runner.new('test/data/input.txt')
7
+ assert_equal 5, runner.plateau.x_upper_limit
8
+ assert_equal 5, runner.plateau.y_upper_limit
9
+
10
+ assert_equal 1, runner.rovers[0].x
11
+ assert_equal 3, runner.rovers[0].y
12
+ assert_equal 'N', runner.rovers[0].direction
13
+
14
+ assert_equal 5, runner.rovers[1].x
15
+ assert_equal 1, runner.rovers[1].y
16
+ assert_equal 'E', runner.rovers[1].direction
17
+ end
18
+ end
@@ -0,0 +1,8 @@
1
+ require 'mars_rover'
2
+ require 'minitest'
3
+
4
+ require 'minitest/unit'
5
+ require 'minitest/autorun'
6
+ require 'minitest/pride'
7
+
8
+ require 'active_support'
metadata ADDED
@@ -0,0 +1,142 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mars_rover
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Paulo Diniz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activesupport
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Solving the Thoughtworks Mars Landing Rover problem
84
+ email:
85
+ - paulodiniz@gmail.com
86
+ executables:
87
+ - mars_rover
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/mars_rover
97
+ - lib/mars_rover.rb
98
+ - lib/mars_rover/direction.rb
99
+ - lib/mars_rover/plateau.rb
100
+ - lib/mars_rover/position.rb
101
+ - lib/mars_rover/rover.rb
102
+ - lib/mars_rover/runner.rb
103
+ - lib/mars_rover/version.rb
104
+ - mars_rover.gemspec
105
+ - test/.DS_Store
106
+ - test/data/input.txt
107
+ - test/plateau_test.rb
108
+ - test/rover_test.rb
109
+ - test/runner_test.rb
110
+ - test/test_helper.rb
111
+ homepage: ''
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.2.2
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: Mars Landing Rover solution
135
+ test_files:
136
+ - test/.DS_Store
137
+ - test/data/input.txt
138
+ - test/plateau_test.rb
139
+ - test/rover_test.rb
140
+ - test/runner_test.rb
141
+ - test/test_helper.rb
142
+ has_rdoc: