Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: gitlaura/cheap_tourist_fast_tourist
base: 5cb8f5ddd8
...
head fork: gitlaura/cheap_tourist_fast_tourist
compare: eecfe2a2d0
Checking mergeability… Don't worry, you can still create the pull request.
  • 8 commits
  • 15 files changed
  • 1 commit comment
  • 2 contributors
View
10 README.md
@@ -0,0 +1,10 @@
+Solution for PuzzleNode Challenge #2 - Cheap Tourist Fast Tourist.
+
+You can find the challenge details here: http://www.puzzlenode.com/puzzles/2-cheap-tourist-fast-tourist
+
+How to Run:
+
+1. clone this repo to your computer
+2. right now, the program is written to parse flight cases fom "input.txt". you can update the input file in start.rb
+3. run in the commmand line with $ ruby start.rb
+4. you should see a new file "output.txt" with the cheapest and fastest times
View
357 input.txt
@@ -0,0 +1,357 @@
+3
+
+25
+Z D 10:00 11:07 562.80
+D C 00:00 00:42 133.71
+C D 18:00 18:42 599.71
+A C 17:00 18:07 161.80
+C D 14:00 14:42 656.71
+D Z 21:00 22:07 312.80
+C D 01:00 01:42 90.71
+Z D 07:59 09:07 336.80
+C A 12:00 13:07 133.80
+D C 06:00 06:42 277.71
+C D 15:00 15:42 188.71
+Z D 07:59 09:07 195.80
+Z D 09:00 10:07 566.80
+D Z 18:00 19:07 355.80
+C A 01:59 03:07 234.80
+C A 12:00 13:07 636.80
+Z D 18:00 19:07 264.80
+D C 12:59 13:42 707.71
+Z D 15:59 17:07 494.80
+D Z 15:59 17:07 347.80
+D Z 05:00 06:07 244.80
+A C 06:00 07:07 525.80
+D Z 12:00 13:07 390.80
+C A 14:00 15:07 562.80
+Z D 07:59 09:07 207.80
+
+100
+D B 00:00 01:24 427.42
+F B 06:00 07:24 859.42
+B F 03:00 04:24 692.42
+A F 00:00 02:14 269.61
+C F 05:00 07:00 270.00
+Z I 09:00 11:14 906.61
+B F 14:00 15:24 451.42
+E B 10:00 11:07 277.80
+I Z 06:00 08:14 275.61
+B D 21:00 22:24 197.42
+G I 03:59 05:34 507.11
+E C 14:00 16:03 885.16
+D B 03:00 04:24 319.42
+Z G 17:00 18:34 760.11
+F I 10:00 12:07 767.13
+C F 07:59 09:59 458.00
+F D 20:00 22:00 232.00
+E A 10:00 12:30 577.00
+F D 03:59 05:59 761.00
+H G 12:59 14:29 707.00
+H F 07:59 09:48 504.28
+A F 20:00 22:14 315.61
+D I 09:00 10:34 464.11
+B C 18:00 19:24 317.42
+I F 00:00 02:07 606.13
+B A 01:00 03:14 560.61
+F B 11:00 12:24 299.42
+F D 11:00 12:59 698.00
+G I 12:59 14:34 851.11
+E F 11:00 11:29 132.00
+E F 12:59 13:30 123.00
+H D 03:00 04:07 182.80
+E H 14:00 15:24 651.42
+E A 01:59 04:30 423.00
+H G 10:00 11:30 707.00
+F A 10:00 12:14 624.61
+D B 17:00 18:24 570.42
+I D 03:59 05:34 643.11
+I Z 20:00 22:14 939.61
+H I 18:00 18:29 126.00
+A B 01:59 04:14 374.61
+B A 15:59 18:14 273.61
+H Z 03:59 06:29 306.00
+H F 18:00 19:48 293.28
+F A 09:00 11:14 694.61
+D G 12:00 13:24 686.42
+G Z 14:00 15:34 750.11
+I F 10:00 12:07 721.13
+H G 18:00 19:30 833.00
+H I 19:00 19:30 595.00
+E H 07:59 09:24 633.42
+E A 01:59 04:30 333.00
+A C 21:00 22:00 589.00
+H D 14:00 15:07 758.80
+G D 11:00 12:24 736.42
+I G 15:59 17:34 432.11
+C F 10:00 12:00 784.00
+B C 20:00 21:24 416.42
+G Z 01:00 02:34 413.11
+H E 03:00 04:24 605.42
+I Z 01:59 04:14 337.61
+E B 12:59 14:07 800.80
+E C 07:59 10:03 659.16
+F H 18:00 19:48 407.28
+G Z 19:00 20:34 362.11
+G Z 12:00 13:34 324.11
+C A 00:00 01:00 239.00
+F C 20:00 22:00 232.00
+Z I 01:59 04:14 567.61
+B A 01:00 03:14 806.61
+B E 09:00 10:07 716.80
+Z G 19:00 20:34 191.11
+B D 20:00 21:24 774.42
+A C 06:00 06:59 264.00
+H D 00:00 01:07 355.80
+E A 10:00 12:30 803.00
+I F 17:00 19:07 386.13
+B A 01:59 04:14 937.61
+D E 03:59 05:29 183.00
+G H 18:00 19:30 530.00
+E H 17:00 18:24 173.42
+H E 06:00 07:24 718.42
+Z G 12:59 14:34 592.11
+I D 20:00 21:34 740.11
+F B 15:00 16:24 709.42
+I H 12:00 12:30 512.00
+I H 03:00 03:29 249.00
+B F 05:00 06:24 173.42
+F C 11:00 12:59 308.00
+B C 07:59 09:24 829.42
+H B 11:00 13:03 761.16
+Z H 06:00 08:30 767.00
+D H 05:00 06:07 163.80
+B A 19:00 21:14 276.61
+B E 19:00 20:07 812.80
+H D 09:00 10:07 514.80
+E D 19:00 20:30 437.00
+B E 14:00 15:07 823.80
+F I 03:59 06:07 567.13
+I G 14:00 15:34 408.11
+
+225
+M G 20:00 21:07 732.80
+E N 05:00 08:21 1078.41
+A B 00:00 03:38 821.01
+N J 07:00 09:07 543.13
+B N 11:00 14:09 469.23
+J D 07:59 14:02 1229.15
+H M 11:00 14:21 382.41
+K M 07:59 14:15 727.50
+B H 06:00 07:30 899.00
+J E 12:00 13:30 439.00
+H N 06:00 09:54 436.51
+N E 00:00 03:21 537.41
+M G 20:00 21:07 577.80
+H L 11:00 14:48 834.79
+E C 00:00 02:14 645.61
+C Z 14:00 19:42 811.09
+K F 15:59 21:31 1247.27
+C M 12:59 15:03 693.16
+D H 14:00 18:36 682.98
+E A 19:00 22:48 472.79
+K L 06:00 10:07 826.31
+D J 03:59 10:02 682.15
+E I 09:00 15:06 670.33
+H M 09:00 12:21 467.41
+N A 03:00 09:30 1364.00
+J G 10:00 15:13 1045.02
+L E 12:59 17:44 935.34
+J B 17:00 18:34 702.11
+J H 07:00 08:48 205.28
+E C 06:00 08:14 269.61
+L D 21:00 22:07 399.80
+J E 01:59 03:29 219.00
+H D 01:00 05:36 778.98
+L A 03:59 10:19 910.46
+L D 12:00 13:07 560.80
+B F 09:00 12:02 983.14
+C B 17:00 19:41 532.26
+A N 10:00 16:30 771.00
+H K 00:00 05:08 879.78
+L G 14:00 17:36 1089.56
+K Z 03:00 04:00 337.00
+L I 19:00 21:03 823.16
+F E 03:00 06:00 636.00
+E J 01:59 03:29 185.00
+C L 11:00 13:32 922.95
+D M 05:00 08:36 805.56
+G B 15:59 20:01 755.11
+H Z 07:00 12:42 1283.09
+A M 05:00 09:01 887.11
+H Z 03:00 08:42 1040.09
+E K 05:00 11:02 1239.15
+D M 09:00 12:36 1083.56
+H I 00:00 05:18 1199.51
+E F 07:00 10:00 706.00
+K E 01:59 08:02 974.15
+K E 15:00 21:02 758.15
+C A 17:00 21:18 1077.12
+I L 15:59 18:03 287.16
+H K 01:59 07:08 1153.78
+H Z 15:59 21:42 1008.09
+I H 10:00 15:18 531.51
+M I 01:00 03:32 279.95
+E K 07:59 14:02 1139.15
+N A 09:00 15:29 1206.00
+M F 07:00 13:30 778.00
+B H 20:00 21:30 304.00
+L M 07:00 09:30 889.00
+N F 07:00 08:30 538.00
+F Z 01:59 07:31 1146.27
+C J 10:00 13:12 663.16
+E L 17:00 21:44 572.34
+A N 12:00 18:29 1051.00
+K L 18:00 22:07 1128.31
+F N 20:00 21:30 379.00
+M K 14:00 20:15 1274.50
+C J 07:59 11:12 849.16
+J C 14:00 17:12 555.16
+L C 14:00 16:32 693.95
+A B 00:00 03:38 748.01
+H E 05:00 06:00 526.00
+K Z 05:00 06:00 145.00
+H E 09:00 09:59 779.00
+G M 01:59 03:07 321.80
+K F 17:00 22:31 1142.27
+H J 05:00 06:48 926.28
+D B 03:59 10:04 1148.28
+A B 11:00 14:38 624.01
+J F 09:00 10:30 427.00
+C Z 07:00 12:42 673.09
+C B 19:00 21:41 484.26
+D G 01:00 05:43 502.70
+Z D 12:00 16:30 1115.00
+K D 09:00 12:30 900.00
+M K 01:59 08:15 731.50
+K M 07:00 13:15 1131.50
+B J 15:00 16:34 899.11
+N B 11:00 14:09 532.23
+C G 15:59 18:32 671.95
+L H 12:59 16:48 798.79
+B K 09:00 15:29 732.00
+E C 07:59 10:14 526.61
+H D 10:00 14:36 852.98
+C D 06:00 09:30 1020.00
+H Z 05:00 10:42 1024.09
+A J 05:00 10:13 887.02
+D M 15:59 19:36 550.56
+L A 14:00 20:19 733.46
+K H 07:59 13:08 560.78
+Z J 06:00 11:51 1301.23
+B E 12:00 12:30 316.00
+C L 01:00 03:32 276.95
+N C 06:00 11:18 1163.51
+E G 07:00 10:48 483.79
+E K 12:59 19:02 627.15
+F L 05:00 11:21 1216.40
+D I 12:59 15:32 631.95
+C L 10:00 12:32 506.95
+C Z 15:00 20:42 1073.09
+J D 15:59 22:02 877.15
+A L 03:00 09:19 1093.46
+B N 06:00 09:09 747.23
+D J 15:00 21:02 1148.15
+B G 11:00 15:01 871.11
+M D 05:00 08:36 500.56
+F Z 09:00 14:31 1070.27
+A M 06:00 10:01 818.11
+J F 10:00 11:30 290.00
+N H 07:59 11:54 830.51
+J D 15:59 22:02 903.15
+G A 03:00 06:00 1004.00
+B L 10:00 15:13 1247.02
+L I 15:59 18:03 685.16
+A G 18:00 21:00 596.00
+E I 09:00 15:06 819.33
+B A 14:00 17:38 632.01
+H L 00:00 03:48 754.79
+F H 12:00 15:09 895.23
+F J 07:00 08:30 274.00
+L M 01:00 03:30 341.00
+J D 10:00 16:02 1044.15
+L K 00:00 04:07 597.31
+G H 00:00 03:32 1027.55
+E H 22:00 22:59 169.00
+A C 09:00 13:18 853.12
+L D 12:59 14:07 772.80
+I E 01:59 08:06 1268.33
+J C 11:00 14:12 812.16
+E C 20:00 22:14 297.61
+M G 05:00 06:07 448.80
+H L 03:00 06:48 562.79
+M H 15:59 19:21 754.41
+E N 07:59 11:21 692.41
+F K 09:00 14:31 691.27
+I H 09:00 14:18 689.51
+H L 18:00 21:48 855.79
+D M 15:00 18:36 527.56
+I L 12:00 14:03 738.16
+A J 12:59 18:13 1180.02
+N B 18:00 21:09 819.23
+G L 06:00 09:36 783.56
+D K 00:00 03:30 676.00
+F K 12:59 18:31 1255.27
+H D 06:00 10:36 732.98
+K B 01:59 08:29 686.00
+A G 06:00 09:00 354.00
+A E 11:00 14:48 1078.79
+F K 01:00 06:31 985.27
+F N 15:59 17:29 884.00
+A L 12:00 18:19 696.46
+F B 18:00 21:02 403.14
+H D 17:00 21:36 571.98
+C I 14:00 17:54 1111.51
+A M 01:59 06:01 976.11
+D I 12:00 14:32 375.95
+K I 03:00 09:01 1216.08
+J N 12:59 15:07 509.13
+D I 07:00 09:32 708.95
+J A 06:00 11:13 810.02
+I C 15:59 19:54 843.51
+I K 01:00 07:01 783.08
+D K 09:00 12:30 706.00
+I E 15:59 22:06 1042.33
+M B 15:59 20:14 891.26
+J Z 12:59 18:51 797.23
+D H 14:00 18:36 858.98
+H M 11:00 14:21 615.41
+C B 06:00 08:41 915.26
+H D 01:00 05:36 676.98
+B G 01:59 06:01 839.11
+H D 07:00 11:36 949.98
+N E 00:00 03:21 1032.41
+A N 03:00 09:30 1203.00
+A E 11:00 14:48 857.79
+B N 01:59 05:09 1020.23
+A H 01:00 05:18 541.12
+L D 20:00 21:07 250.80
+C L 11:00 13:32 891.95
+K Z 07:00 08:00 106.00
+F B 11:00 14:02 605.14
+E I 03:00 09:06 942.33
+F M 07:59 14:29 820.00
+M G 07:00 08:07 492.80
+G D 15:59 20:43 866.70
+I A 09:00 15:29 943.00
+M A 00:00 04:01 695.11
+G H 12:00 15:32 748.55
+N F 12:59 14:29 542.00
+N J 12:00 14:07 822.13
+H L 01:00 04:48 1128.79
+M L 09:00 11:30 685.00
+L D 03:59 05:07 828.80
+J A 01:59 07:13 1013.02
+E D 14:00 19:35 1051.02
+C A 11:00 15:18 731.12
+C Z 00:00 05:42 750.09
+J H 14:00 15:48 535.28
+L I 20:00 22:03 772.16
+E F 12:59 15:59 761.00
+K F 09:00 14:31 666.27
+D G 07:00 11:43 531.70
+B J 11:00 12:34 469.11
+C A 07:59 12:18 1049.12
+C Z 05:00 10:42 1133.09
+D M 07:00 10:36 390.56
+J Z 12:59 18:51 1112.23
View
93 lib/all_paths.rb
@@ -0,0 +1,93 @@
+require 'ostruct'
+
+class AllPaths
+ def initialize(flights, departure_city, arrival_city)
+ @flights = flights
+ @departure_city = departure_city
+ @arrival_city = arrival_city
+ @final_paths = []
+ end
+
+ def create_paths
+ @departure_city_groups = group_flights_by_departure_city
+ @departure_city_groups[@departure_city.to_sym].each do |flight_from_dep_city|
+ if flight_from_dep_city.to == @arrival_city
+ @final_paths << flight_from_dep_city
+ else
+ @current_path = create_new_path(flight_from_dep_city)
+ @path_attributes = reset_path_attributes(flight_from_dep_city)
+ update_current_path
+ end
+ end
+ @final_paths
+ end
+
+ def group_flights_by_departure_city
+ departure_city_groups = {}
+ @flights.each do |flight|
+ new_from = flight.from.to_sym
+ departure_city_groups[new_from] ||= []
+ departure_city_groups[new_from] << flight
+ end
+ departure_city_groups
+ end
+
+ def create_new_path(flight)
+ OpenStruct.new(:from => @departure_city, :to => nil, :dep => flight.dep, :arr => nil, :price => nil)
+ end
+
+ def reset_path_attributes(flight)
+ path_attributes = {}
+ path_attributes[:prices], path_attributes[:arrival_times], path_attributes[:arrival_cities] = [], [], []
+ path_attributes[:prices] << flight.price
+ path_attributes[:arrival_times] << flight.arr
+ path_attributes[:arrival_cities] << flight.to
+ path_attributes
+ end
+
+ def update_current_path(new_path_level = false)
+ new_dep_city = @path_attributes[:arrival_cities].last.to_sym
+ @departure_city_groups[new_dep_city].each do |possible_flight|
+ if new_path_level == false
+ remove_attributes_from_last_flight if @path_attributes[:arrival_cities].size > 1
+ end
+ if valid_next_flight?(possible_flight)
+ update_path_attributes(possible_flight)
+ if possible_flight.to == @arrival_city
+ add_attributes_to_path
+ add_current_path_to_final_paths
+ remove_attributes_from_last_flight
+ next
+ end
+ update_current_path(true)
+ end
+ end
+ end
+
+ def valid_next_flight?(possible_flight)
+ @path_attributes[:arrival_times].last < possible_flight.dep && possible_flight.to > possible_flight.from
+ end
+
+ def update_path_attributes(possible_flight)
+ @path_attributes[:prices] << possible_flight.price
+ @path_attributes[:arrival_times] << possible_flight.arr
+ @path_attributes[:arrival_cities] << possible_flight.to
+ end
+
+ def add_attributes_to_path
+ @current_path.price = @path_attributes[:prices].reduce(:+)
+ @current_path.arr = @path_attributes[:arrival_times].last
+ @current_path.to = @path_attributes[:arrival_cities].last
+ end
+
+ def remove_attributes_from_last_flight
+ @path_attributes[:prices].pop
+ @path_attributes[:arrival_times].pop
+ @path_attributes[:arrival_cities].pop
+ end
+
+ def add_current_path_to_final_paths
+ end_path = @current_path.dup
+ @final_paths << end_path
+ end
+end
View
30 lib/best_flights.rb
@@ -0,0 +1,30 @@
+class BestFlights
+ class << self
+ def get_cheapest_flight(all_flight_paths)
+ lowest_price = nil
+ all_flight_paths.each do |flight|
+ if lowest_price.nil? || flight.price < lowest_price
+ @best_flight = flight
+ lowest_price = flight.price
+ elsif lowest_price == flight.price
+ @best_flight = get_fastest_flight([@best_flight, flight])
+ end
+ end
+ @best_flight
+ end
+
+ def get_fastest_flight(all_flight_paths)
+ shortest_length = nil
+ all_flight_paths.each do |flight|
+ length = flight.arr - flight.dep
+ if shortest_length.nil? || length < shortest_length
+ @best_flight = flight
+ shortest_length = length
+ elsif shortest_length == length
+ @best_flight = get_cheapest_flight([@best_flight, flight])
+ end
+ end
+ @best_flight
+ end
+ end
+end
View
22 lib/flight_formatter.rb
@@ -0,0 +1,22 @@
+class FlightFormatter
+ class << self
+ def get_print_format(flights)
+ @line_to_print = ""
+ flights.each do |flight|
+ departure_time = format_time(flight.dep)
+ arrival_time = format_time(flight.arr)
+ price = format_price(flight.price)
+ @line_to_print << "#{departure_time} #{arrival_time} #{price}\n"
+ end
+ @line_to_print << "\n"
+ end
+
+ def format_time(time)
+ "#{'%02d' % (time / 60)}:#{'%02d' % (time % 60)}"
+ end
+
+ def format_price(price)
+ "#{"%.2f" % price}"
+ end
+ end
+end
View
50 lib/flight_parser.rb
@@ -0,0 +1,50 @@
+require 'ostruct'
+
+class FlightParser
+ class << self
+ def parse(txt_file)
+ all_cases = []
+ flights_created = 0
+
+ file = File.open(txt_file).read
+ file.each_line.with_index do |line, index|
+ if empty_line?(line, index)
+ next
+ elsif number_of_cases?(line)
+ @number_of_flights = line.strip.to_i
+ @new_case = []
+ flights_created = 0
+ else
+ from, to, dep, arr, price = line.strip.split(" ")
+ @new_case << create_flight(from, to, dep, arr, price)
+ flights_created += 1
+ all_cases << @new_case if flights_created == @number_of_flights
+ end
+ end
+ all_cases
+ end
+
+ def create_flight(from, to, dep, arr, price)
+ OpenStruct.new(
+ :from => from,
+ :to => to,
+ :dep => to_number(dep),
+ :arr => to_number(arr),
+ :price => price.to_f
+ )
+ end
+
+ def to_number(time)
+ hour, minutes = time.split(":")
+ hour.to_i * 60 + minutes.to_i
+ end
+
+ def empty_line?(line, index)
+ index < 2 || line == "\n"
+ end
+
+ def number_of_cases?(line)
+ line.strip.to_i > 0
+ end
+ end
+end
View
28 lib/flight_route_calculator.rb
@@ -0,0 +1,28 @@
+require_relative("all_paths.rb")
+require_relative("best_flights.rb")
+require_relative("flight_formatter.rb")
+require_relative("flight_parser.rb")
+
+class FlightRouteCalculator
+ class << self
+ def get_cases(file)
+ FlightParser.parse(file)
+ end
+
+ def find_all_flight_paths(flight_case, departure_city, arrival_city)
+ AllPaths.new(flight_case, departure_city, arrival_city).create_paths
+ end
+
+ def get_cheapest_flight(all_flight_paths)
+ BestFlights.get_cheapest_flight(all_flight_paths)
+ end
+
+ def get_fastest_flight(all_flight_paths)
+ BestFlights.get_fastest_flight(all_flight_paths)
+ end
+
+ def get_print_format(flights)
+ FlightFormatter.get_print_format(flights)
+ end
+ end
+end
View
22 lib/runner.rb
@@ -0,0 +1,22 @@
+require_relative 'flight_route_calculator.rb'
+
+class Runner
+ class << self
+ def run_flight_route_calculator(flights_to_parse, departure_city, arrival_city)
+ output_file = File.open("output.txt","w")
+ cases = FlightRouteCalculator.get_cases(flights_to_parse)
+ cases.each do |flight_case|
+ flight_paths = FlightRouteCalculator.find_all_flight_paths(flight_case, departure_city, arrival_city)
+ cheapest_flight = FlightRouteCalculator.get_cheapest_flight(flight_paths)
+ fastest_flight = FlightRouteCalculator.get_fastest_flight(flight_paths)
+ line_to_print = FlightRouteCalculator.get_print_format([cheapest_flight, fastest_flight])
+ write_to_file(line_to_print,output_file)
+ end
+ output_file
+ end
+
+ def write_to_file(line_to_print, file)
+ file.write(line_to_print)
+ end
+ end
+end
View
78 spec/all_paths_spec.rb
@@ -0,0 +1,78 @@
+require 'all_paths'
+require 'ostruct'
+
+describe "All paths" do
+ let(:flight_one) {OpenStruct.new( :from => "A", :to => "B", :dep => 480, :arr => 540, :price => 50.0)}
+ let(:flight_two){OpenStruct.new( :from => "A", :to => "B", :dep => 720, :arr => 780, :price => 300.0)}
+ let(:flight_three){OpenStruct.new(:from => "A", :to => "C", :dep => 840, :arr => 930, :price => 175.0)}
+ let(:flight_four){OpenStruct.new( :from => "B", :to => "C", :dep => 600, :arr => 660, :price => 75.0)}
+ let(:flight_five){OpenStruct.new( :from => "B", :to => "Z", :dep => 900, :arr => 990, :price => 250.0)}
+ let(:flight_six){OpenStruct.new( :from => "C", :to => "B", :dep => 945, :arr => 1005,:price => 50.0)}
+ let(:flight_seven){OpenStruct.new(:from => "C", :to => "Z", :dep => 960, :arr => 1140,:price => 100.0)}
+ let(:flight_eight){OpenStruct.new(:from => "C", :to => "Z", :dep => 960, :arr => 1000,:price => 200.0)}
+
+ let(:path_one){OpenStruct.new( :from => "A", :to => "Z", :dep => 480, :arr => 1140, :price => 225.0)}
+ let(:path_two){OpenStruct.new( :from => "A", :to => "Z", :dep => 480, :arr => 1000, :price => 325.0)}
+ let(:path_three){OpenStruct.new(:from => "A", :to => "Z", :dep => 480, :arr => 990, :price => 300.0)}
+ let(:path_four){OpenStruct.new( :from => "A", :to => "Z", :dep => 720, :arr => 990, :price => 550.0)}
+ let(:path_five){OpenStruct.new( :from => "A", :to => "Z", :dep => 840, :arr => 1140, :price => 275.0)}
+ let(:path_six){OpenStruct.new( :from => "A", :to => "Z", :dep => 840, :arr => 1000, :price => 375.0)}
+
+ let(:departure_city){"A"}
+ let(:arrival_city){"Z"}
+ let(:all_paths){AllPaths.new([flight_one, flight_five], departure_city, arrival_city)}
+
+ it "gets the correct paths for just two flights" do
+ all_flights = [flight_one, flight_five]
+ all_paths_class = AllPaths.new(all_flights, departure_city, arrival_city)
+ all_paths = [path_three]
+
+ final_paths = all_paths_class.create_paths
+
+ expect(final_paths).to eq(all_paths)
+ end
+
+ it "gets the correct paths for flights with three levels" do
+ all_flights = [flight_one, flight_two, flight_three, flight_four, flight_five, flight_six, flight_seven]
+ all_paths_class = AllPaths.new(all_flights, departure_city, arrival_city)
+ all_paths = [path_one, path_three, path_four, path_five]
+
+ final_paths = all_paths_class.create_paths
+
+ expect(final_paths).to eq(all_paths)
+ end
+
+ it "gets the correct paths for flights with multiple end options" do
+ all_flights = [flight_one, flight_two, flight_three, flight_four, flight_five, flight_six, flight_seven, flight_eight]
+ all_paths_class = AllPaths.new(all_flights, departure_city, arrival_city)
+ all_paths = [path_one, path_two, path_three, path_four, path_five, path_six]
+
+ final_paths = all_paths_class.create_paths
+
+ expect(final_paths).to eq(all_paths)
+ end
+
+ it "creates a hash with all from cities" do
+ all_flights = [flight_one, flight_two, flight_three, flight_four, flight_five, flight_six, flight_seven, flight_eight]
+ all_paths_class = AllPaths.new(all_flights, departure_city, arrival_city)
+
+ all_froms = all_paths_class.group_flights_by_departure_city
+
+ expect(all_froms[:A]).to eq([flight_one, flight_two, flight_three])
+ end
+
+ it "creates a new path" do
+ path = all_paths.create_new_path(flight_one)
+
+ expect(path.from).to eq(flight_one.from)
+ expect(path.to).to eq(nil)
+ expect(path.dep).to eq(flight_one.dep)
+ expect(path.arr).to eq(nil)
+ expect(path.price).to eq(nil)
+ end
+
+ it "resets path attributes" do
+ path_attributes = all_paths.reset_path_attributes(flight_one)
+ expect(path_attributes).to eq({:prices =>[50],:arrival_times => [540],:arrival_cities => ["B"]})
+ end
+end
View
18 spec/best_flights_spec.rb
@@ -0,0 +1,18 @@
+require 'best_flights'
+
+describe "Best flights" do
+ let(:flight_one){OpenStruct.new(:from => "A", :to => "B", :dep => 540, :arr => 600, :price => 100.00)}
+ let(:flight_two){OpenStruct.new(:from => "B", :to => "Z", :dep => 690, :arr => 810, :price => 100.00)}
+ let(:flight_three){OpenStruct.new(:from => "A", :to => "Z", :dep => 600, :arr => 720, :price => 300.00)}
+ let(:case_one){[flight_one, flight_two, flight_three]}
+
+ it "gets the cheapest flight if they're equally cheap" do
+ best_flight = BestFlights.get_cheapest_flight(case_one)
+ expect(best_flight).to eq(flight_one)
+ end
+
+ it "gets the fastest flight" do
+ best_flight = BestFlights.get_fastest_flight(case_one)
+ expect(best_flight).to eq(flight_one)
+ end
+end
View
20 spec/flight_formatter_spec.rb
@@ -0,0 +1,20 @@
+require 'flight_formatter'
+
+describe "Flight formatter" do
+ it "creates the correct flight format" do
+ flight_one = OpenStruct.new(:from => "A", :to => "B", :dep => 540, :arr => 600, :price => 100.00)
+ flight_two = OpenStruct.new(:from => "B", :to => "Z", :dep => 690, :arr => 810, :price => 100.00)
+
+ line = FlightFormatter.get_print_format([flight_one, flight_two])
+
+ expect(line).to include("9:00 10:00 100.00\n")
+ end
+
+ it "formats the correct time" do
+ expect(FlightFormatter.format_time(550)).to eq("09:10")
+ end
+
+ it "formats the correct price" do
+ expect(FlightFormatter.format_price(375.2)).to eq("375.20")
+ end
+end
View
34 spec/flight_parser_spec.rb
@@ -0,0 +1,34 @@
+require 'flight_parser'
+
+describe "Flight parser" do
+ it "parses flights" do
+ cases = FlightParser.parse("sample-input.txt")
+
+ expect(cases.size).to eq(2)
+ expect(cases[0].size).to eq(3)
+ end
+
+ it "creates a flight object" do
+ new_flight = FlightParser.create_flight("A", "Z", "9:00", "10:00", "100.00")
+
+ expect(new_flight.from).to eq("A")
+ expect(new_flight.to).to eq("Z")
+ expect(new_flight.dep).to eq(540)
+ expect(new_flight.price).to eq(100.00)
+ end
+
+ it "turns time into numbers" do
+ number = FlightParser.to_number("9:15")
+ expect(number).to eq(555)
+ end
+
+ it "checks for empty lines" do
+ result = FlightParser.empty_line?("\n", 3)
+ expect(result).to eq(true)
+ end
+
+ it "checks for the case number" do
+ result = FlightParser.number_of_cases?("5")
+ expect(result).to eq(true)
+ end
+end
View
37 spec/flight_route_calculator_spec.rb
@@ -0,0 +1,37 @@
+require 'flight_route_calculator'
+
+describe "Flight route calc" do
+ let(:flight_one){OpenStruct.new(:from => "A", :to => "B", :dep => 540, :arr => 600, :price => 100.00)}
+ let(:flight_two){OpenStruct.new(:from => "B", :to => "Z", :dep => 690, :arr => 810, :price => 100.00)}
+ let(:flight_three){OpenStruct.new(:from => "A", :to => "Z", :dep => 600, :arr => 720, :price => 300.00)}
+ let(:case_one){[flight_one, flight_two, flight_three]}
+
+ it "parses text file into cases" do
+ cases = FlightRouteCalculator.get_cases("input.txt")
+ expect(cases.size).to eq(3)
+ end
+
+ it "finds the best flights for each case" do
+ departure_city = "A"
+ arrival_city = "Z"
+
+ all_paths = FlightRouteCalculator.find_all_flight_paths(case_one, departure_city, arrival_city)
+
+ expect(all_paths.size).to eq(2)
+ end
+
+ it "finds the cheapest flight" do
+ best_flight = FlightRouteCalculator.get_cheapest_flight(case_one)
+ expect(best_flight).to eq(flight_one)
+ end
+
+ it "finds the fastest flight" do
+ best_flight = FlightRouteCalculator.get_fastest_flight(case_one)
+ expect(best_flight).to eq(flight_one)
+ end
+
+ it "formats the flights for print" do
+ line_to_print = FlightRouteCalculator.get_print_format([flight_two, flight_three])
+ expect(line_to_print).to include("10:00 12:00 300.00\n\n")
+ end
+end
View
33 spec/runner_spec.rb
@@ -0,0 +1,33 @@
+require 'runner'
+
+describe "Runner" do
+ let(:file) {"sample-input.txt"}
+ let(:departure_city) {"A"}
+ let(:arrival_city) {"Z"}
+
+ it "gets the cases from the flight parser" do
+ file = "sample-input.txt"
+ departure_city = "A"
+ arrival_city = "Z"
+
+ expect(File).to receive(:open)
+ expect(FlightRouteCalculator).to receive(:get_cases) {[1, 1]}
+ expect(FlightRouteCalculator).to receive(:find_all_flight_paths).twice
+ expect(FlightRouteCalculator).to receive(:get_cheapest_flight).twice
+ expect(FlightRouteCalculator).to receive(:get_fastest_flight).twice
+ expect(FlightRouteCalculator).to receive(:get_print_format).twice
+ expect(Runner).to receive(:write_to_file).twice
+
+ Runner.run_flight_route_calculator(file, departure_city, arrival_city)
+ end
+
+ it "writes to a file" do
+ temp_output = StringIO.new
+
+ Runner.write_to_file("testing", temp_output)
+
+ temp_output.each_line do |line|
+ expect(temp_output.read).to eq("tesing")
+ end
+ end
+end
View
2  start.rb
@@ -1,6 +1,6 @@
require_relative 'lib/runner.rb'
-flights_to_parse = "sample-input.txt"
+flights_to_parse = "input.txt"
departure_city = "A"
arrival_city = "Z"

Showing you all comments on commits in this comparison.

@gitlaura
Owner
  • refactored all_paths.rb to eliminate duplicate among the methods that update the current path's attributes
  • refactored tests for multiple classes
  • removed "let" in rspec unless reused the variable multiple times
Something went wrong with that request. Please try again.