-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
68 lines (51 loc) · 2.49 KB
/
main.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
require 'sinatra'
$lines = {
:n => ['Times Square', '34th','28th', '23rd', 'Union Square', '8th'],
:l => ['8th', '6th', 'Union Square', '3rd', '1st'],
:'6' => ['Grand Central', '33rd', '28th', '23rd', 'Union Square', 'Astor Place']
}
get '/' do
erb :start
end
get '/journey' do
@start_station, @start_line = params[:start_station].split(' : ')
@stop_station, @stop_line = params[:stop_station].split(' : ')
@start_station_index = $lines[@start_line.downcase.to_sym].index (@start_station)
@stop_station_index = $lines[@stop_line.downcase.to_sym].index (@stop_station)
@intersection1 = $lines[@start_line.downcase.intern].index 'Union Square'
@intersection2 = $lines[@stop_line.downcase.intern].index 'Union Square'
if @start_line == @stop_line && @start_station_index < @stop_station_index
@trip = $lines[@start_line.downcase.intern][@start_station_index..@stop_station_index]
@trip = @trip.length - 1
else
@trip = $lines[@start_line.downcase.intern][@stop_station_index..@start_station_index]
@trip = @trip.length - 1
end
if @start_line != @stop_line
if @start_station_index < @intersection1 && @stop_station_index > @intersection2
@trip1 = $lines[@start_line.downcase.intern][@start_station_index..@intersection1]
@trip2 = $lines[@stop_line.downcase.intern][@intersection2..@stop_station_index]
@trip = @trip1.length + @trip2.length - 2
end
if @start_station_index < @intersection1 && @stop_station_index < @intersection2
@trip1 = $lines[@start_line.downcase.intern][@start_station_index..@intersection1]
@trip2 = $lines[@stop_line.downcase.intern][@stop_station_index..@intersection2]
@trip2.reverse!
@trip = @trip1.length + @trip2.length - 2
end
if @start_station_index > @intersection1 && @stop_station_index > @intersection2
@trip1 = $lines[@start_line.downcase.intern][@intersection1..@start_station_index]
@trip2 = $lines[@stop_line.downcase.intern][@intersection2..@stop_station_index]
@trip = @trip1.length + @trip2.length - 2
end
if @start_station_index > @intersection1 && @stop_station_index < @intersection2
@trip1 = $lines[@start_line.downcase.intern][@intersection1..@start_station_index]
@trip2 = $lines[@stop_line.downcase.intern][@stop_station_index..@intersection2]
@trip = @trip1.length + @trip2.length - 2
end
if @start_station_index == @intersection1 && @stop_station_index == @intersection2
@trip = 0
end
end
erb :start
end