reorg and add code for second edition

This commit is contained in:
Aditya Bhargava
2023-08-09 08:20:19 -05:00
parent 9306432a1b
commit 933acafaf3
89 changed files with 18 additions and 117 deletions

View File

@@ -0,0 +1,31 @@
require "set"
# You pass an array in, and it gets converted to a Set.new.
states_needed = Set.new(%w(mt wa or id nv ut ca az))
stations = {}
stations["kone"] = Set.new(%w(id nv ut))
stations["ktwo"] = Set.new(%w(wa id mt))
stations["kthree"] = Set.new(%w(or nv ca))
stations["kfour"] = Set.new(%w(nv ut))
stations["kfive"] = Set.new(%w(ca az))
final_stations = Set.new
until states_needed.empty?
best_station = nil
states_covered = Set.new
stations.each do |station, states|
covered = states_needed & states
if covered.length > states_covered.length
best_station = station
states_covered = covered
end
end
states_needed -= states_covered
final_stations << best_station
end
p final_stations