Last active
August 29, 2015 14:07
-
-
Save tom-galvin/aecbdb1be271108957c5 to your computer and use it in GitHub Desktop.
DailyProgrammer Challenge #182e Solution (The Column Conundrum)
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
#!/usr/bin/env ruby | |
# /r/DailyProgrammer Challenge 182e: The Column Conundrum | |
words = [] | |
columns, column_width, space_width = gets.chomp.split(' ').map {|s| s.to_i} | |
while (line = gets; line != nil && line.chomp.length > 0) | |
words += line.chomp.split(' ') | |
end | |
lines = [] | |
while words.count > 0 | |
line = "" | |
loop do | |
new_line = "#{line} #{words[0]}".strip | |
break if new_line.length >= column_width || words.count == 0 | |
words.shift | |
line = new_line | |
end | |
lines.push line.ljust(column_width, ' ') | |
end | |
total_line_count = (lines.length / columns).ceil + 1 | |
(0..total_line_count).each do |index| | |
puts (index...lines.length) | |
.step(total_line_count + 1) | |
.map {|line_index| lines[line_index]} | |
.join (' ' * space_width) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment