-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparking.rb
70 lines (63 loc) · 1.41 KB
/
parking.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
# There are three kinds of possible vehicles: regular cars, small cars, and motorcycles.
# Regular cars can only park in R spots.
# Small cars can park in R or S spots.
# Motorcycles can park in R, S, or M spots.
def size_of(vehicle)
case vehicle
when 'regular'
'R'
when 'small'
'RS'
when 'motorcycle'
'RSM'
else
'X'
end
end
def is_available(spot)
spot == spot.downcase ? false : true
end
def where_can_I_park(spots, vehicle)
spots.map { |row|
row.map { |spot|
if is_available(spot)
&& size_of(vehicle).include?(spot)
return [row.index(spot), spots.index(row)]
end
}
}
[]
end
puts where_can_I_park(
[
# COLUMNS ARE X
# 0 1 2 3 4 5
["s", "s", "s", "S", "R", "M"], # 0 ROWS ARE Y
["s", "M", "s", "S", "r", "M"], # 1
["s", "M", "s", "S", "r", "m"], # 2
["S", "r", "s", "m", "r", "M"], # 3
["S", "r", "s", "m", "r", "M"], # 4
["S", "r", "S", "M", "M", "S"] # 5
],
"regular"
).inspect
puts where_can_I_park(
[
["M", "M", "M", "M"],
["M", "s", "M", "M"],
["M", "M", "M", "M"],
["M", "M", "r", "M"]
],
"small"
).inspect
puts where_can_I_park(
[
["s", "s", "s", "s", "s", "s"],
["s", "m", "s", "S", "r", "s"],
["s", "m", "s", "S", "r", "s"],
["S", "r", "s", "m", "r", "s"],
["S", "r", "s", "m", "R", "s"],
["S", "r", "S", "M", "m", "S"]
],
"motorcycle"
).inspect