slice_pizzabot 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/pizzabot +1 -1
- data/lib/pizzabot.rb +25 -34
- data/lib/pizzabot/address_error.rb +1 -1
- data/lib/pizzabot/grid_error.rb +2 -2
- data/lib/pizzabot/route_service.rb +16 -12
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 96524b57c59a4e83a476b2c8ea5805bfe14a83b775be76379e45b63e462833bf
|
4
|
+
data.tar.gz: 7726b461068299e3a68c137e09e096661d2ebaf1708a980c340115830327a72a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cba7baac7f3ceef0122c8fdae9f2119879a8954b0cf649544a372cf04dcb0733385b325c55f0c25d9e48f9a6c14a93dd029651649804b84b3a5cf0c7c0a4e1fd
|
7
|
+
data.tar.gz: 33c7d8059a3944ca84891df680e298df1a6edf3411933fc01d591a615410cb71218be368638eaddbeef9c4708c67eae83c1c871790766afd0108763c2b1a4bbc
|
data/bin/pizzabot
CHANGED
data/lib/pizzabot.rb
CHANGED
@@ -1,20 +1,22 @@
|
|
1
|
-
require_relative 'pizzabot/route_service
|
1
|
+
require_relative 'pizzabot/route_service'
|
2
2
|
|
3
3
|
class Pizzabot
|
4
|
+
NO_ARGUMENT_MESSAGE = 'You should provide delivery string to pizzabot,'\
|
5
|
+
' example: pizzabot "2x2 (0,1) (0,2)"'
|
6
|
+
INITIAL_POSITION = [0, 0]
|
4
7
|
|
5
|
-
|
8
|
+
attr_reader :route
|
9
|
+
attr_accessor :current_position, :delivery_process
|
6
10
|
|
7
|
-
|
8
|
-
attr_accessor :current_position, :delivery_process
|
9
|
-
|
10
|
-
def initialize(delivery_string)
|
11
|
+
def initialize(delivery_string)
|
11
12
|
raise ArgumentError, NO_ARGUMENT_MESSAGE unless delivery_string
|
12
|
-
@current_position = [0, 0]
|
13
|
-
@delivery_process = ''
|
14
|
-
@route = RouteService.new(delivery_string).prepare_route
|
15
|
-
end
|
16
13
|
|
17
|
-
|
14
|
+
@current_position = INITIAL_POSITION
|
15
|
+
@delivery_process = ''
|
16
|
+
@route = RouteService.new(delivery_string).prepare_route
|
17
|
+
end
|
18
|
+
|
19
|
+
def deliver
|
18
20
|
route.each do |address|
|
19
21
|
move_to(address)
|
20
22
|
drop_pizza
|
@@ -27,43 +29,32 @@ class Pizzabot
|
|
27
29
|
private
|
28
30
|
|
29
31
|
def move_to(address)
|
30
|
-
until current_position[0] == address[0]
|
31
|
-
|
32
|
-
|
33
|
-
else
|
34
|
-
move_west
|
35
|
-
end
|
36
|
-
end
|
37
|
-
until current_position[1] == address[1] do
|
38
|
-
if current_position[1] < address[1]
|
39
|
-
move_north
|
40
|
-
else
|
41
|
-
move_south
|
42
|
-
end
|
43
|
-
end
|
32
|
+
current_position[0] < address[0] ? move_east : move_west until current_position[0] == address[0]
|
33
|
+
|
34
|
+
current_position[1] < address[1] ? move_north : move_south until current_position[1] == address[1]
|
44
35
|
end
|
45
36
|
|
46
37
|
def drop_pizza
|
47
|
-
delivery_process <<
|
38
|
+
delivery_process << 'D'
|
48
39
|
end
|
49
40
|
|
50
41
|
def move_north
|
51
|
-
|
52
|
-
delivery_process <<
|
42
|
+
current_position[1] += 1
|
43
|
+
delivery_process << 'N'
|
53
44
|
end
|
54
45
|
|
55
46
|
def move_south
|
56
|
-
|
57
|
-
delivery_process <<
|
47
|
+
current_position[1] -= 1
|
48
|
+
delivery_process << 'S'
|
58
49
|
end
|
59
50
|
|
60
51
|
def move_west
|
61
|
-
|
62
|
-
delivery_process <<
|
52
|
+
current_position[0] -= 1
|
53
|
+
delivery_process << 'W'
|
63
54
|
end
|
64
55
|
|
65
56
|
def move_east
|
66
|
-
|
67
|
-
delivery_process <<
|
57
|
+
current_position[0] += 1
|
58
|
+
delivery_process << 'E'
|
68
59
|
end
|
69
60
|
end
|
data/lib/pizzabot/grid_error.rb
CHANGED
@@ -1,17 +1,19 @@
|
|
1
|
-
require_relative 'grid_error
|
2
|
-
require_relative 'address_error
|
1
|
+
require_relative 'grid_error'
|
2
|
+
require_relative 'address_error'
|
3
3
|
|
4
4
|
class RouteService
|
5
|
-
|
6
|
-
GRID_REGEX = /^\d+x\d+/
|
5
|
+
GRID_REGEX = /^\s*\d+x\d+/
|
7
6
|
ADDRESSES_REGEX = /\((.*?)\)/
|
8
7
|
DIGITS_EXTRACT_REGEX = /\d+/
|
9
8
|
|
10
|
-
|
9
|
+
NEGATIVE_NUMBERS_MESSAGE = 'Address should not have negative numbers'
|
10
|
+
NOT_IN_RANGE_MESSAGE = 'Address is not in delivery range'
|
11
|
+
|
12
|
+
attr_reader :grid
|
11
13
|
attr_accessor :addresses
|
12
14
|
|
13
15
|
def initialize(delivery_string)
|
14
|
-
@grid = delivery_string[GRID_REGEX]
|
16
|
+
@grid = delivery_string[GRID_REGEX]&.split('x')&.map(&:to_i)
|
15
17
|
@addresses = delivery_string.scan(ADDRESSES_REGEX).flatten
|
16
18
|
end
|
17
19
|
|
@@ -23,15 +25,17 @@ class RouteService
|
|
23
25
|
private
|
24
26
|
|
25
27
|
def validate_route
|
26
|
-
|
27
|
-
grid_values = grid.split('x').map(&:to_i)
|
28
|
+
raise GridError unless grid
|
28
29
|
|
29
30
|
addresses.map! do |address|
|
31
|
+
raise AddressError, NEGATIVE_NUMBERS_MESSAGE if address.include?('-')
|
32
|
+
|
30
33
|
extracted_array = address.scan(DIGITS_EXTRACT_REGEX).map(&:to_i)
|
31
|
-
raise AddressError
|
32
|
-
raise AddressError
|
33
|
-
|
34
|
-
|
34
|
+
raise AddressError, "Wrong format address: #{address}" unless extracted_array.length == 2
|
35
|
+
raise AddressError, NOT_IN_RANGE_MESSAGE if extracted_array[0] > grid[0] ||
|
36
|
+
extracted_array[1] > grid[1]
|
37
|
+
|
38
|
+
extracted_array
|
35
39
|
end
|
36
40
|
end
|
37
41
|
|