Successful code golf submissions are, by nature, filled with crazy symbols all over the place. To make their submission easier to understand, many code-golfers choose to include an explanation of their code. In their explanation, the line of code is turned into a vertically exploded diagram.
For example, if this were my code:
1_'[3:~2@+]`
One of the many possible diagrams I could create would look like this:
1
_'
[ ]
[3: ]
[ ~ ]
[ 2@ ]
[ +]
`
The Goal
In this challenge, you will write an explanation auto-formatting tool which takes a line of code and creates a diagram to which explanatory text can be easily added.
In order to make this a more useful challenge, the user will be able to specify the contents of each line, by providing a formatting string. The formatting string will be a second line, containing only letters A-Za-z
, that is the same length as the program. The letters show the order in which the characters of the program should be printed in the explanation.
Here is an example of I/O without any bracket-like formatting:
123423
AabcBC
1
2
3
2
3
4
Brackets
If more than one character in the program has the same priority level, then that set of characters acts as a single block of code (if they form a group) or a set of brackets (if they contain other characters in-between). The general rules are simple:
Characters do not appear in a line of the diagram until all other characters of greater priority have already appeared on the lines above it in the diagram.
Characters of equal priority are always printed on the same lines. If a certain character appears on a line, all other characters of equal priority appear on the line.
A set of characters of equal priority continue to appear on each line until all other characters enclosed by it have appeared at least once. This allows for "bracket-like" constructions. If
bceab
are the priorities, then theb
characters will appear on the second line (they are second-highest priority) and will continue to appear until all of thecea
characters have appeared. If the priority string isabcadeafga
, then all ofbcdefg
are considered contained within it, an all 4a
s will continue to appear until theg
has appeared.
More formatting requirements
All lines of output should be the same length (the length of the input lines), padded with spaces as necessary. The input program line may contain spaces, although those spaces will also be given a priority letter. Trailing newlines on output/input are optional.
Scoring
This is code golf, fewest bytes wins.
Examples
Here is a commented example of a piece of code with more complex formatting.
1_'[3:~2@+]`
abbcddeffgch
1 #highest priority is denoted by the lowercase letter a
_' #priority b
[ ] #all characters with priority c
[3: ] #priority d, but priority c still printed because it encloses more
[ ~ ] #priority e
[ 2@ ] #priority f
[ +] #priority g, last line of c because all enclosed characters have appeared
` #priority h
An example in Perl:
$_=<>;s/[^aeiou\W]/$&o$&/gi;print
aaaaaabbccccccccccbdddddbbbbeeeee
$_=<>;
s/ / /gi;
s/[^aeiou\W]/ /gi;
s/ /$&o$&/gi;
print
Here are a few examples in CJam, courtesy of Martin Büttner:
l~2*{_2%{3*)}{2/}?_p_(}g;
aabbcdddefffeeggeehhiiccj
l~
2*
{ }g
{_2% }g
{ { }{ }? }g
{ {3*)}{ }? }g
{ { }{2/}? }g
{ _p }g
{ _(}g
;
q{_eu'[,66>"EIOU"-#)g{'o1$}*}/
abcccddddddeeeeeeefgghiijjhhbb
q
{ }/
{_eu }/
{ '[,66> }/
{ "EIOU"- }/
{ # }/
{ )g }/
{ { }*}/
{ {'o }*}/
{ { 1$}*}/
Here is a crazy example just to mess with you:
1_'[3:~2@+]`
azTABACBDCAT
[ : ]
[3: 2 ]
[3:~2 +]
[ :~ @+]
' `
1
_
Here is a more explicit example of what happens when brackets overlap like abab
. (Normally, this is not the way you would choose to format your explanation.)
aabbccddaaeebb
aabbccddaaeebb
aa aa
aabb aa bb
aabbcc aa bb
aabb ddaa bb
bb eebb #"aa" no longer appears because all of "bbccdd" have already appeared.