Created
March 4, 2016 20:31
-
-
Save amosrivera/cfb691f2aa9f9b79da0e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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