gets.scan(/(\d)*(.)/){f=->a{$&>?-?a:a.transpose} $*<<Z=f[$1?[[$2]*$&[0].hex]*$1.hex: (l,r=$*.pop(2).map &f l+=r).map{|x|(x+[?.]*41)[0,l.map(&:size).max]}]} puts Z.map &:join
Note that non-ascii characters in the above source code will be escaped (such as \x9f).