Skip to content

Commit af7381b

Browse files
author
neal.carr
committed
Added some more methods to the Camel and began to build a round_trip World method
1 parent 18142cb commit af7381b

File tree

1 file changed

+55
-5
lines changed

1 file changed

+55
-5
lines changed

puzzles/do_camels_eat_bananas/bananas.py

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,23 @@ def __init__(self, length, initial_bananas, step):
77
self.bananastores = {0: BananaStore(initial_bananas, initial_bananas)}
88
self.step = step
99
self.initial_bananas = initial_bananas
10-
10+
self.camel = None
11+
12+
def spawn_camel(self, camel_carrying_capacity):
13+
"""
14+
Create a camel in the world.
15+
"""
16+
if self.camel is None:
17+
self.camel = Camel(camel_carrying_capacity)
18+
else:
19+
raise AttributeError("There is only one camel.")
20+
21+
def kill_camel(self):
22+
"""
23+
>:)
24+
"""
25+
self.camel = None
26+
1127
def get_final_bananas(self, camel_carrying_capacity):
1228
my_camel = Camel(camel_carrying_capacity)
1329
if my_camel.location in self.bananastores:
@@ -18,20 +34,53 @@ def get_final_bananas(self, camel_carrying_capacity):
1834
# Camel needs to drop bananas, but keep enough to get back
1935
# Camel goes back to get move bananas
2036
return "Hello!"
37+
38+
def round_trip(self, end_point):
39+
"""
40+
Perform a round trip - update all banana stores and camel's banana store.
41+
"""
42+
# Camel picks up bananas
43+
bananas_at_start = self.bananastores[self.camel.location]
44+
bananas_to_pick_up = max(self.camel.banana_store.remaining_capacity(), bananas_at_start.bananas)
45+
self.camel.banana_store.add(bananas_to_pick_up)
46+
47+
# Camel walks to destination and drops bananas
48+
distance = end_point - self.camel.location
49+
self.camel.move(distance)
50+
number_of_bananas_to_drop = self.camel.banana_store.bananas - self.camel.bananas_required(distance)
51+
52+
53+
2154

2255

2356
class Camel:
2457

25-
def __init__(self, max_bananas):
26-
self.max_bananas = max_bananas
27-
self.banana_store = BananaStore(0, self.max_bananas)
58+
def __init__(self, camel_carrying_capacity):
59+
self.camel_carrying_capacity = camel_carrying_capacity
60+
self.banana_consumption_rate = 1
61+
self.banana_store = BananaStore(0, self.camel_carrying_capacity)
2862
self.location = 0
2963

3064
def move(self, distance):
3165
"""Positive distance moves towards destination
3266
negative distance moves towards the beginning"""
3367
self.location += distance
34-
self.banana_store.remove(abs(distance))
68+
self.banana_store.remove(self.bananas_required(distance))
69+
70+
def bananas_required(self, distance):
71+
return self.banana_consumption_rate * abs(distance)
72+
73+
def possible_round_trip(self):
74+
"""
75+
Return the maximum possible round trip distance, assuming the camel drops all its bananas.
76+
"""
77+
return self.banana_store.bananas // 2
78+
79+
def drop_bananas(self, box):
80+
"""
81+
82+
"""
83+
3584

3685

3786
class BananaStore:
@@ -72,5 +121,6 @@ def remaining_capacity(self):
72121
for x in range(1000):
73122
step = x
74123
world = World(length, initial_bananas, step)
124+
world.spawn_camel(camel_carrying_capacity)
75125
results = world.get_final_bananas(camel_carrying_capacity)
76126
print(results)

0 commit comments

Comments
 (0)