-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaxo.rb
32 lines (26 loc) · 974 Bytes
/
naxo.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
# frozen_string_literal: true
def route(initial_station, final_station, metro)
return metro[initial_station][:name] if initial_station == final_station
visited_stations = []
combinatined_station = []
current_station = initial_station
while current_station != final_station
puts "Current station #{current_station}"
next_station = metro[current_station][:next]
puts "Next station #{next_station}"
current_station = metro.map { |k, v| k if v[:previous].to_i == next_station.to_i }.compact.first
end
puts "Arrived #{current_station}"
end
metro = {}
File.open('stations.csv') do |file|
file.each_line do |line|
information = line.split(',')
next if information[0] == 'id'
metro[information[0]] = { line: information[1], name: information[2],
previous: information[9], next: information[10] }
end
end
initial_station = '101'
final_station = '123'
puts route(initial_station, final_station, metro)