-
Notifications
You must be signed in to change notification settings - Fork 3
/
parcel.rb
115 lines (100 loc) · 3.35 KB
/
parcel.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
@main_hash = {}
@next_key = 1.0
@next_slot_number = 1
@max_slot_number = 6
@max_key = @max_slot_number/2 + 0.1
@next_slot = []
@next_key_slot = []
def park(parcel_code, parcel_weight)
return "Sorry, parcel slot is full" if (@next_key > @max_key && @next_key_slot.empty?)
if @next_key_slot.length > 0
@main_hash[@next_key_slot.first] = { slot_number: @next_slot.first, parcel_code: parcel_code, parcel_weight: parcel_weight }
puts "Allocated slot number: #{@next_slot.first}"
@next_key_slot.shift
@next_slot.shift
else
@main_hash[@next_key] = { slot_number: @next_slot_number, parcel_code: parcel_code, parcel_weight: parcel_weight }
puts "Allocated slot number: #{@next_slot_number}"
calculate_next_key
calculate_next_slot_number
end
end
def calculate_next_key
if @next_key%1 == 0
@next_key += 0.1
else
@next_key = @next_key + 1 - 0.1
end
end
def calculate_next_slot_number
@next_slot_number = if @next_key%1 == 0
@next_key.to_i
else
@max_slot_number + 1 - @next_key.to_i
end
end
def create_parcel_lot(number)
puts "Created a parcel slot with #{number} slots"
end
def leave_for_delivery(slot_number)
@main_hash.select {|k, v| v[:parcel_code], v[:parcel_weight], v[:slot_number] = nil if v[:slot_number] == slot_number && !v[:parcel_code].nil? }
@next_slot << slot_number
@main_hash.select{ |h, k| @next_key_slot << h if k[:slot_number].nil? }
@next_key_slot.uniq!
puts "slot #{slot_number} is free"
end
def status
hash = @main_hash.reject { |k, v| v[:slot_number].nil? }
@m = hash.values
puts "slot_number parcel_code parcel_weight"
puts @m.collect { |p| "#{p[:slot_number]} #{p[:parcel_code]} #{p[:parcel_weight]}" }
end
def slot_number_for_registration_number(regis_no)
slots = []
@main_hash.each do |k, v|
if v[:parcel_code] == regis_no
slots << v[:slot_number]
end
end
slots.length > 0 ? puts(slots.join(' ')) : "Not Found"
end
def parcel_code_for_parcels_with_weight(weight)
slots = []
@main_hash.each do |k, v|
if v[:parcel_weight] == weight
slots << v[:parcel_code]
end
end
slots.length > 0 ? puts(slots.join(' ')) : "Not Found"
end
def slot_numbers_for_parcels_with_weight(weight)
slots = []
@main_hash.each do |k, v|
if v[:parcel_weight] == weight
slots << v[:slot_number]
end
end
slots.length > 0 ? puts(slots.join(' ')) : "Not Found"
end
File.open("input.txt", "r") do |f|
f.each_line do |line|
string_array = line.split(' ')
method_name = string_array[0]
case method_name
when "create_parcel_lot"
send(:create_parcel_lot, string_array[1])
when "park"
send(:park, string_array[1], string_array[2])
when "status"
send(:status)
when "leave_for_delivery"
send(:leave_for_delivery, string_array[1].to_i)
when "parcel_code_for_parcels_with_weight"
send(:parcel_code_for_parcels_with_weight, string_array[1])
when "slot_numbers_for_parcels_with_weight"
send(:slot_numbers_for_parcels_with_weight, string_array[1])
when "slot_number_for_registration_number"
send(:slot_number_for_registration_number, string_array[1])
end
end
end