Skip to content

Instantly share code, notes, and snippets.

@amosrivera
Created March 4, 2016 20:31
Show Gist options
  • Save amosrivera/cfb691f2aa9f9b79da0e to your computer and use it in GitHub Desktop.
Save amosrivera/cfb691f2aa9f9b79da0e to your computer and use it in GitHub Desktop.
def get_serve_pattern(queue, barbers)
min = barbers.min
freq = barbers.map { |b| (b / min).ceil }
count = 0
0.upto(queue).each do |n|
puts "count is #{count}"
count += 1
barbers.each_with_index do |b, i|
if n % freq[i] == 0
count += 1
return (i+1) if count == queue
end
end
end
count
end
def solve_for(file)
solutions = []
file.gets.to_i.times do |n|
served_by = get_serve_pattern(
file.gets.split(" ").map(&:to_i)[1],
file.gets.split(" ").map(&:to_i)
)
solutions.push(served_by)
end
solutions
end
def save(lines)
if File.exist?("output.txt")
File.delete("output.txt")
end
lines.each_with_index do |content, n|
File.open('output.txt', 'a') do |f|
f.puts "Case ##{n+1}: #{content}"
end
end
end
solutions = solve_for(File.new(ARGV[0], "r"))
save(solutions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment