Skip to content

Instantly share code, notes, and snippets.

@darraghjones
Created January 24, 2012 21:43
Show Gist options
  • Save darraghjones/1672889 to your computer and use it in GitHub Desktop.
Save darraghjones/1672889 to your computer and use it in GitHub Desktop.
Facebook Hacker Cup 2012
def calculate_font_size(width, height, words)
font_size = 0
while true do
return font_size if !will_text_fit(width, height, words, font_size + 1)
font_size += 1
end
end
def will_text_fit(width, height, words, font_size)
rows = 1
current = 0
words.each do |word|
return false if word.length * font_size > width
current += font_size if current != 0
if (current + word.length * font_size > width)
rows += 1
current = word.length * font_size
else
current += word.length * font_size
end
end
return (rows * font_size <= height)
end
# this is your public static void main(blah blah blah)...
input = File.open("billboards_real_input.txt", "r").read
input = input.split("\n")
num_cases = input.shift
input.each_with_index do |test_case, i|
next if test_case[0] == "#"
test_case = test_case.split(" ")
width = test_case.shift.to_i
height = test_case.shift.to_i
words = test_case
font_size = calculate_font_size(width, height, words)
puts "Case ##{i+1}: #{font_size}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment