Skip to content

Instantly share code, notes, and snippets.

@mark
Created April 8, 2015 03:00
Show Gist options
  • Save mark/b79e1ed01cbaba12cc71 to your computer and use it in GitHub Desktop.
Save mark/b79e1ed01cbaba12cc71 to your computer and use it in GitHub Desktop.
cities = ('a'..'p').to_a
def generate_lists(cities)
pos = cities.combination(3).to_a.shuffle; nil
map = {}
cities.each do |c|
taken = []
while taken.length < 3
c3 = pos.shift
if c3.include? c
pos.push c3
else
taken.push c3
end
end
# puts "#{ c }\t#{ taken.inspect }"
map[c] = taken
end
origins = map.values.flatten
counts = Hash.new(0)
origins.each { |c| counts[c] += 1 }
# puts counts.inspect
# puts counts.values.min
{ map: map, counts: counts, min: counts.values.min }
end
def find_city_list(cities, max_runs, min)
max_runs.times do
res = generate_lists(cities)
if res[:min] >= min
cities.each do |c|
o = res[:map][c]
puts "#{ c }\t#{ o[0].join(' ') }\t#{ o[1].join(' ') }\t#{ o[2].join(' ') }"
end
puts res[:counts].inspect
break
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment