mars_rovers 0.0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README +33 -0
- data/Rakefile +1 -0
- data/bin/mars_rovers +4 -0
- data/features/rover.feature +9 -0
- data/features/support/setup.rb +1 -0
- data/lib/mars_rovers.rb +3 -0
- data/lib/mars_rovers/cli.rb +14 -0
- data/lib/mars_rovers/coordinates +5 -0
- data/lib/mars_rovers/rover.rb +174 -0
- data/lib/mars_rovers/version.rb +3 -0
- data/mars_rovers.gemspec +30 -0
- data/spec/mars_rovers_spec.rb +9 -0
- metadata +115 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
== MARS ROVERS
|
2
|
+
|
3
|
+
Plots the position of the squad of robotic rovers to be landed on a plateau on Mars by NASA
|
4
|
+
|
5
|
+
== INPUT
|
6
|
+
The first line of input is the upper-right coordinates of the plateau, the lower-left coordinates are assumed to be 0,0.
|
7
|
+
The rest of the input is information pertaining to the rovers that have been deployed. Each rover has two lines of input.
|
8
|
+
The first line gives the rover's position, and the second line is a series of instructions telling the rover how to explore the plateau.
|
9
|
+
The position is made up of two integers and a letter separated by spaces, corresponding to the x and y c o-ordinates and the
|
10
|
+
rover's orientation.
|
11
|
+
Each rover will be finished sequentially, which means that the second rover won't start to move until the first one has finished moving.
|
12
|
+
|
13
|
+
== OUTPUT
|
14
|
+
The output for each rover should be its final co-ordinates and heading.
|
15
|
+
|
16
|
+
== INPUT AND OUTPUT
|
17
|
+
|
18
|
+
Test Input:
|
19
|
+
5 5
|
20
|
+
1 2 N
|
21
|
+
LMLMLMLMM
|
22
|
+
3 3 E
|
23
|
+
MMRMMRMRRM
|
24
|
+
|
25
|
+
Expected Output:
|
26
|
+
1 3 N
|
27
|
+
5 1 E
|
28
|
+
|
29
|
+
== INSTALLATION:
|
30
|
+
gem install mars_rovers
|
31
|
+
|
32
|
+
== COMMAND LINE:
|
33
|
+
mars_rovers plot -f [filename]
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/mars_rovers
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'aruba/cucumber'
|
data/lib/mars_rovers.rb
ADDED
@@ -0,0 +1,174 @@
|
|
1
|
+
module MarsRovers
|
2
|
+
|
3
|
+
class Rover
|
4
|
+
|
5
|
+
def self.get_rover_mov(results)
|
6
|
+
rover_mov = []
|
7
|
+
|
8
|
+
line_count = results.count
|
9
|
+
|
10
|
+
(1..line_count).each do |index|
|
11
|
+
if index % 2 == 0
|
12
|
+
rover_mov << results[index]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
return rover_mov.compact
|
17
|
+
end # get_rover_mov
|
18
|
+
|
19
|
+
def self.get_rover_pos(results)
|
20
|
+
rover_pos = []
|
21
|
+
|
22
|
+
line_count = results.count
|
23
|
+
|
24
|
+
(1..line_count).each do |index|
|
25
|
+
unless index % 2 == 0
|
26
|
+
rover_pos << results[index]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
return rover_pos.compact
|
31
|
+
end # get_rover_pos
|
32
|
+
|
33
|
+
def self.move_one_step_horizontal(coor,x)
|
34
|
+
case coor
|
35
|
+
when 'E'
|
36
|
+
return x + 1
|
37
|
+
when 'W'
|
38
|
+
return x - 1
|
39
|
+
else
|
40
|
+
# do nothing
|
41
|
+
end
|
42
|
+
end # move_one_step_horizontal
|
43
|
+
|
44
|
+
def self.move_one_step_vertical(coor,y)
|
45
|
+
case coor
|
46
|
+
when 'N'
|
47
|
+
return y + 1
|
48
|
+
when 'S'
|
49
|
+
return y - 1
|
50
|
+
else
|
51
|
+
# do nothing
|
52
|
+
end
|
53
|
+
end # move_one_step_vertical
|
54
|
+
|
55
|
+
def self.plot(filename)
|
56
|
+
|
57
|
+
results = read_file(filename)
|
58
|
+
|
59
|
+
@upper_right_coor = results[0]
|
60
|
+
|
61
|
+
@rover_pos = get_rover_pos(results)
|
62
|
+
@rover_mov = get_rover_mov(results)
|
63
|
+
|
64
|
+
output = process_rover_pos_and_mov(@rover_pos,@rover_mov)
|
65
|
+
|
66
|
+
return output.compact
|
67
|
+
|
68
|
+
end # plot
|
69
|
+
|
70
|
+
def self.process_rover_mov(mov,x,y,coor)
|
71
|
+
@mov = mov
|
72
|
+
@x = x
|
73
|
+
@y = y
|
74
|
+
@coor = coor
|
75
|
+
|
76
|
+
(0..@mov.strip.length - 1).each do |index|
|
77
|
+
case @mov[index]
|
78
|
+
when 'L'
|
79
|
+
@coor = switch_left(@coor)
|
80
|
+
when 'R'
|
81
|
+
@coor = switch_right(@coor)
|
82
|
+
when 'M'
|
83
|
+
case @coor
|
84
|
+
when 'N'
|
85
|
+
@y = move_one_step_vertical(@coor,@y)
|
86
|
+
when 'S'
|
87
|
+
@y = move_one_step_vertical(@coor,@y)
|
88
|
+
when 'E'
|
89
|
+
@x = move_one_step_horizontal(@coor,@x)
|
90
|
+
when 'W'
|
91
|
+
@x = move_one_step_horizontal(@coor,@x)
|
92
|
+
else
|
93
|
+
# do nothing
|
94
|
+
end
|
95
|
+
else
|
96
|
+
# do nothing
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
output = [@x,@y,@coor]
|
101
|
+
|
102
|
+
return output
|
103
|
+
|
104
|
+
end # process_rover_mov
|
105
|
+
|
106
|
+
def self.process_rover_pos_and_mov(rover_pos,rover_mov)
|
107
|
+
rover_pos_size = rover_pos.size
|
108
|
+
output = []
|
109
|
+
|
110
|
+
(0..rover_pos_size - 1).each do |index|
|
111
|
+
pos = rover_pos[index].split
|
112
|
+
x = pos[0].to_i
|
113
|
+
y = pos[1].to_i
|
114
|
+
coor = pos[2]
|
115
|
+
mov = rover_mov[index]
|
116
|
+
|
117
|
+
x,y,coor = process_rover_mov(mov,x,y,coor)
|
118
|
+
|
119
|
+
output << [x,y,coor].join(" ")
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
return output
|
124
|
+
end # process_rover_pos_and_mov
|
125
|
+
|
126
|
+
def self.read_file(filename)
|
127
|
+
results = []
|
128
|
+
|
129
|
+
filename = File.exists?(filename) ? filename : File.join(File.expand_path(File.dirname(__FILE__)),'coordinates')
|
130
|
+
|
131
|
+
File.open(filename, "r").each do |line|
|
132
|
+
results << line
|
133
|
+
end
|
134
|
+
|
135
|
+
return results
|
136
|
+
end # read_file
|
137
|
+
|
138
|
+
def self.switch_left(coor)
|
139
|
+
|
140
|
+
case coor
|
141
|
+
when 'N'
|
142
|
+
return 'W'
|
143
|
+
when 'E'
|
144
|
+
return 'N'
|
145
|
+
when 'W'
|
146
|
+
return 'S'
|
147
|
+
when 'S'
|
148
|
+
return 'E'
|
149
|
+
else
|
150
|
+
return ''
|
151
|
+
end
|
152
|
+
|
153
|
+
end # switch_left
|
154
|
+
|
155
|
+
def self.switch_right(coor)
|
156
|
+
|
157
|
+
case coor
|
158
|
+
when 'N'
|
159
|
+
return 'E'
|
160
|
+
when 'E'
|
161
|
+
return 'S'
|
162
|
+
when 'W'
|
163
|
+
return 'N'
|
164
|
+
when 'S'
|
165
|
+
return 'W'
|
166
|
+
else
|
167
|
+
return ''
|
168
|
+
end
|
169
|
+
|
170
|
+
end # switch_right
|
171
|
+
|
172
|
+
end # class Rover
|
173
|
+
|
174
|
+
end
|
data/mars_rovers.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "mars_rovers/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "mars_rovers"
|
7
|
+
s.version = MarsRovers::VERSION
|
8
|
+
s.authors = ["Cecille Ann Manalang"]
|
9
|
+
s.email = ["cecilleann@sharkbait.local"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{ Plots the position of the squad of robotic rovers to be landed on a plateau on Mars by NASA }
|
12
|
+
s.description = %q{ }
|
13
|
+
|
14
|
+
s.rubyforge_project = "mars_rovers"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
|
23
|
+
s.add_development_dependency "rspec", "~> 2.6"
|
24
|
+
s.add_development_dependency "cucumber"
|
25
|
+
s.add_development_dependency "aruba"
|
26
|
+
|
27
|
+
s.add_dependency "thor"
|
28
|
+
|
29
|
+
# s.add_runtime_dependency "rest-client"
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mars_rovers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Cecille Ann Manalang
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-12-17 00:00:00 +08:00
|
14
|
+
default_executable:
|
15
|
+
dependencies:
|
16
|
+
- !ruby/object:Gem::Dependency
|
17
|
+
name: rspec
|
18
|
+
prerelease: false
|
19
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
|
+
none: false
|
21
|
+
requirements:
|
22
|
+
- - ~>
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: "2.6"
|
25
|
+
type: :development
|
26
|
+
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: cucumber
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: "0"
|
36
|
+
type: :development
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: aruba
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id003
|
49
|
+
- !ruby/object:Gem::Dependency
|
50
|
+
name: thor
|
51
|
+
prerelease: false
|
52
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
type: :runtime
|
59
|
+
version_requirements: *id004
|
60
|
+
description: " "
|
61
|
+
email:
|
62
|
+
- cecilleann@sharkbait.local
|
63
|
+
executables:
|
64
|
+
- mars_rovers
|
65
|
+
extensions: []
|
66
|
+
|
67
|
+
extra_rdoc_files: []
|
68
|
+
|
69
|
+
files:
|
70
|
+
- .gitignore
|
71
|
+
- Gemfile
|
72
|
+
- README
|
73
|
+
- Rakefile
|
74
|
+
- bin/mars_rovers
|
75
|
+
- features/rover.feature
|
76
|
+
- features/support/setup.rb
|
77
|
+
- lib/mars_rovers.rb
|
78
|
+
- lib/mars_rovers/cli.rb
|
79
|
+
- lib/mars_rovers/coordinates
|
80
|
+
- lib/mars_rovers/rover.rb
|
81
|
+
- lib/mars_rovers/version.rb
|
82
|
+
- mars_rovers.gemspec
|
83
|
+
- spec/mars_rovers_spec.rb
|
84
|
+
has_rdoc: true
|
85
|
+
homepage: ""
|
86
|
+
licenses: []
|
87
|
+
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: "0"
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ">="
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: "0"
|
105
|
+
requirements: []
|
106
|
+
|
107
|
+
rubyforge_project: mars_rovers
|
108
|
+
rubygems_version: 1.6.2
|
109
|
+
signing_key:
|
110
|
+
specification_version: 3
|
111
|
+
summary: Plots the position of the squad of robotic rovers to be landed on a plateau on Mars by NASA
|
112
|
+
test_files:
|
113
|
+
- features/rover.feature
|
114
|
+
- features/support/setup.rb
|
115
|
+
- spec/mars_rovers_spec.rb
|