Last active
March 6, 2019 15:36
-
-
Save benmanns/c348bbd6c42288acdac055daf11f7535 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
message = <<-END | |
aaa h | |
a a a h | |
a a a hhh eee r rrr eee | |
a a a h h ee ee rr ee ee | |
aa a h h e r e | |
aaaa h h eee r eee | |
END | |
lines = message.split("\n").map(&:rstrip) | |
prefix_length = 0 | |
lines.each do |l| | |
break unless l.strip == '' | |
prefix_length += 1 | |
end | |
lines = lines[prefix_length..-1] | |
suffix_length = 0 | |
lines.reverse.each do |l| | |
break unless l.strip == '' | |
suffix_length += 1 | |
end | |
lines = lines[0...-suffix_length] if suffix_length > 0 | |
max_len = lines.max_by(&:length).length | |
min_left = max_len | |
lines.each do |l| | |
left = l.length - l.lstrip.length | |
min_left = left if left < min_left | |
end | |
if min_left > 0 | |
lines = lines.map { |l| l[min_left..-1] } | |
end | |
max_len -= min_left | |
output = Array.new(lines.length) { Array.new(max_len) } | |
lines.each.with_index do |line, y| | |
line.chars.each.with_index do |c, x| | |
output[y][x] = 0 unless c == " " | |
end | |
end | |
border = 5 | |
output.each.with_index do |line, y| | |
output[y] = Array.new(border) + line + Array.new(border) | |
end | |
output = Array.new(border) { Array.new(border + max_len + border) } + output + Array.new(border) { Array.new(border + max_len + border) } | |
def display(input) | |
puts input.map { |l| l.map { |c| c ? c : " " }.join } | |
end | |
def iterate(input, level) | |
input.each.with_index do |line, y| | |
line.each.with_index do |val, x| | |
next if val | |
if input.dig(y - 1, x) || input.dig(y, x - 1) || input.dig(y, x + 1) || input.dig(y + 1, x) | |
line[x] = false | |
end | |
end | |
end | |
input.each do |line| | |
line.each.with_index do |val, x| | |
line[x] = level if val == false | |
end | |
end | |
end | |
level = 1 | |
loop do | |
display(output) | |
puts | |
break if output.all?(&:all?) | |
level += 1 | |
level = 1 if level == 10 | |
output = iterate(output, level) | |
end | |
output.each do |line| | |
puts line.map { |v| v == 0 ? ":parrot:" : ":w#{v}:" }.join | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment