50
\$\begingroup\$

Lots of people on this site use esoteric languages, and since these languages are unusual and hard to understand, they will frequently write an explanation in a certain format. For example, if the code was

abcdefghijklmnop

And this languages uses # for comments, they would write an explanation like this:

a                #Explanation of what 'a' does
 bc              #Bc
   d             #d
    e            #Explanation of e
     fgh         #foobar
        ij       #hello world
          k      #etc.
           l     #so on
            mn   #and
              op #so forth

I frequently do this too, but every time I do this, I feel like creating the layout of text is really obnoxious and time-consuming. So I want you to create a "Esolang-Comment-Template-Generator" for me. For example, if we ignore the comments, the previous code has this template:

a                #
 bc              #
   d             #
    e            #
     fgh         #
        ij       #
          k      #
           l     #
            mn   #
              op #

The Challenge:

You must write a program or function that takes two strings as input, and outputs this "Esolang-Comment-Template". The first input will be the code, but with bars (|) inserted where the newlines go. The second input is what we will use for comments. So our last example would have this for input:

"a|bc|d|e|fgh|ij|k|l|mn|op", "#"

Unfortunately this excludes bars from being part of the code input, but that's OK. You can assume that the comment input will be a single character. For simplicity's sake, the comment char will not be a bar. The code input will only contain printable ASCII, and it will not contain any newlines.

Hopefully you can infer what to do from the testcases, but I'll try to clarify some things.

You must split the code input up into "code-sections" on every bar. Then, each section of code is output on its own line and left-padded with the length of all the previous code (not including the bars). Then, each line is right-padded with enough spaces so that the last two characters on every line are "One additional space" + "The comment character".

One trailing newline is allowed.

Here is another example. For the input

"Hello|World", "/"

The first section of code is "Hello" and the second is "World". So it should give the output:

Hello      /
     World /

Here are some more samples:

Input:
"a|b|c|d|e|f|g", ","

Output:
a       ,
 b      ,
  c     ,
   d    ,
    e   ,
     f  ,
      g ,

Input:
"abcdefg", ":"

Output:
abcdefg :

Input:
"4|8|15|16|23|42", "%"

Output:
4          %
 8         %
  15       %
    16     %
      23   %
        42 %

Input:
"E|ac|h s|ecti|on is| one c|haracte|r longer| than the| last!", "!"

Output:
E                                                   !
 ac                                                 !
   h s                                              !
      ecti                                          !
          on is                                     !
                one c                               !
                     haracte                        !
                            r longer                !
                                     than the       !
                                              last! !

Input:
"This|Code|has||empty||sections", "@"

Output:
This                     @
    Code                 @
        has              @
                         @
           empty         @
                         @
                sections @

Rules:

You may take these inputs and outputs in any reasonable format. For example, reading/writing a file, STDIN/STOUT, function arguments/return value, etc. As usual, this is , so try to make your code as short as possible and you win if you can get the shortest solution in your language! I will also select the shortest solution as the overall winner. Standard loopholes are banned.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Related \$\endgroup\$ Commented Sep 9, 2016 at 4:32
  • \$\begingroup\$ Are trailing blanks allowed? \$\endgroup\$
    – Titus
    Commented Sep 9, 2016 at 9:02
  • 30
    \$\begingroup\$ Next step : a 3D representation for 2D languages \$\endgroup\$
    – Aaron
    Commented Sep 9, 2016 at 9:26
  • 4
    \$\begingroup\$ A bonus if you manage to do it without using the | character would be nice, so you can explain yourself \$\endgroup\$ Commented Sep 9, 2016 at 12:11
  • \$\begingroup\$ Can the comment character be a bar (|) ? \$\endgroup\$
    – Ton Hospel
    Commented Sep 12, 2016 at 8:46

46 Answers 46

10
\$\begingroup\$

Java 11, 189 159 136 bytes

s->c->{int p=0;for(var a:s.split("\\|"))System.out.println(" ".repeat(p)+a+" ".repeat(s.replace("|","").length()-(p+=a.length())+1)+c);}

-30 bytes converting Java 7 to Java 10 and optimizing the loops.
-23 bytes converting Java 10 to Java 11, so String#repeat builtin can be used.

Try it online.

Explanation:

s->c->{                     // Method with String & char parameters and no return-type
  int p=0;                  //  Position-integer, starting at 0
  for(var a:s.split("\\|")) //  Loop over the parts split by "|":
    System.out.println(     //   Print with trailing newline:
      " ".repeat(p)         //    `p` amount of spaces
      +a                    //    plus the current part
      " ".repeat(s.replace("|","").length()-(p+=a.length())+1)
                            //    plus the correct trailing amount of spaces
                            //    (and add the length of the current part to `p` at the
                            //    same time)
      +c);}                 //    plus the current comment character
\$\endgroup\$
9
\$\begingroup\$

Retina, 35 34 bytes

Byte count assumes ISO 8859-1 encoding.

\|
·$'¶$`±
T0-2`·±|p`___ `.+±.|·.+

The two input strings are separated by a space (which is unambiguous since we know that the comment delimiter is always a single character).

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Why do you need a space to delimit the strings? Since it is a single character, it could just be the last one. \$\endgroup\$
    – Adám
    Commented Sep 9, 2016 at 12:56
  • 1
    \$\begingroup\$ @Adám I'm reusing it as the space separator in the final output. \$\endgroup\$ Commented Sep 9, 2016 at 13:01
7
\$\begingroup\$

Pyth - 28 27 24 23 bytes

Might be able to golf a little off. A lot off, apparently!

jt+R+;zC.t.u+*lNdYcQ\|k

Try it online here.

\$\endgroup\$
6
\$\begingroup\$

Pyke, 31 28 24 bytes

\|cDslF2l-hd*Q+Zi:il?+ZK

Try it here!

\$\endgroup\$
5
\$\begingroup\$

JavaScript (ES6), 92 bytes

f=
(s,c)=>s.split`|`.map((_,i,a)=>a.map((e,j)=>i-j?e.replace(/./g,` `):e).join``+` `+c).join`
`
;
<div oninput=o.textContent=f(s.value,c.value)><input id=s placeholder=Code><input id=c size=1 maxlength=1 value=#><pre id=o>

\$\endgroup\$
4
\$\begingroup\$

GNU sed (85 + 1 for -r) 86

:s;h;:;s,\|( *)[^ \|](.),|\1 \2,;t;s,\|,,g
p;g;:l;s,^( *)[^ \|],\1 ,;tl;s,\|,,;/\S/bs

The inputs are strings separated by a space.

Tests:
input.txt:

a|b|c|d|e|f|g ,
abcdefg :
4|8|15|16|23|42 %
E|ac|h s|ecti|on is| one c|haracte|r longer| than the| last! !
This|Code|has||empty||sections @

Output:

$ cat input.txt | sed -rf template
a       ,
 b      ,
  c     ,
   d    ,
    e   ,
     f  ,
      g ,

abcdefg :

4          %
 8         %
  15       %
    16     %
      23   %
        42 %

E                                                   !
 ac                                                 !
   h s                                              !
      ecti                                          !
          on is                                     !
                one c                               !
                     haracte                        !
                            r longer                !
                                     than the       !
                                              last! !

This                     @
    Code                 @
        has              @
                         @
           empty         @
                         @
                sections @
\$\endgroup\$
2
  • \$\begingroup\$ The nameless label : is a GNU sed feature/bug and \S is I think an extension, so maybe the title should be GNU sed. Other than that, great code. \$\endgroup\$
    – seshoumara
    Commented Sep 10, 2016 at 5:57
  • \$\begingroup\$ @seshoumara Thanks! \$\endgroup\$
    – Riley
    Commented Sep 10, 2016 at 6:10
4
\$\begingroup\$

Python 2, 107 105 102 99 bytes

Tested with all test cases above

EDIT Golfed off 2 bytes by changing d=a.split("|");i=0 to d,i=a.split("|"),0 Not sure how I missed that one. Thanks @Oliver Ni

Another 3 bytes gone. Thanks again.

Suggestion from @Jonathan actually saves 3 bytes and takes it down to the magic 99. Thanks.

def c(a,b):
 d,i=a.split("|"),0
 for e in d:j=i+len(e);print" "*i+e+" "*(len("".join(d))-j+1)+b;i=j
\$\endgroup\$
11
  • 1
    \$\begingroup\$ Golfed it down another byte \$\endgroup\$
    – Oliver Ni
    Commented Sep 10, 2016 at 4:04
  • 3
    \$\begingroup\$ Hey @OliverNi , providing tips for golfing stuff down is appreciated, but editing code isn't really appropriate on this site (source), so I've rolled back your edit. Feel free to post those tips as a comment though! I'm sure the OP would appreciate it, but it should be up to them to test it and choose if they want to use it. \$\endgroup\$
    – DJMcMayhem
    Commented Sep 10, 2016 at 14:04
  • 1
    \$\begingroup\$ Thanks to both of you. Firstly to @Oliver for taking the interest and the time to improve my humble effort and secondly to DJMcMayhem for clarifying what I believed to be the case but had not had a chance to comment on. Oliver - thanks again and please feel free to post changes as comments so that I can learn from your golfing experience. \$\endgroup\$
    – ElPedro
    Commented Sep 10, 2016 at 18:25
  • 1
    \$\begingroup\$ You can remove the parenthesis around " "*i to ave 2 bytes \$\endgroup\$
    – Oliver Ni
    Commented Sep 10, 2016 at 20:55
  • 1
    \$\begingroup\$ You can also set a variable to len(e) like for e in d:z=len(e).... to save a byte because it is used twice \$\endgroup\$
    – Oliver Ni
    Commented Sep 10, 2016 at 20:59
3
\$\begingroup\$

Haskell, 139 135 bytes

s#p=j$foldl g("",0)s where g(a,n)c|c=='|'=(j(a,n)++"\n"++q n,n)|1>0=(a++[c],n+1);q m=' '<$[1..m];j(a,n)=a++q(sum[1|c<-s,c/='|']-n+1)++p

Saved 4 bytes by inlining a definition.

Ungolfed:

template :: String -> String -> String
template code comment = format $ foldl g ("", 0) code
    where g (acc, n) c
            | c == '|' = (format (acc, n) ++ "\n" ++ spaces n, n)
            | otherwise = (acc ++ [c], n+1)
          l = length $ filter (/= '|') code
          spaces n = replicate n ' '
          format (acc, n) = acc ++ spaces (l-n+1) ++ comment
\$\endgroup\$
3
\$\begingroup\$

Groovy, 120 113 111 Bytes

def m(s,c){s.split(/\|/).inject(0,{e,t->println((' '*e+t).padRight(s.replace('|','').size()+1)+c);e+t.size()})}

ungolfed*

def m(s,c){
  s.split(/\|/).inject(0, { e, t ->
    println((' '*e+t).padRight(s.replace('|','').size())+' '+c)
    e+t.size()
  })
}

(First Draft with 120 Bytes)

def m(s,c){def l=0;s.split(/\|/).collect{l+=it.size();it.padLeft(l).padRight(s.replace('|','').size())+' '+c}.join('\n')}

ungolfed*

def m(s,c){
  def l=0 // minimized version needs a semicolon here
  s.split(/\|/).collect{
    l+=it.size() // minimized version needs a semicolon here
    it.padLeft(l).padRight(s.replace('|','').size())+' '+c
  }.join('\n')
}

Tests

%> m('a|bc|d|e|fgh|ij|k|l|mn|op', '#')
a                #
 bc              #
   d             #
    e            #
     fgh         #
        ij       #
          k      #
           l     #
            mn   #
              op #

%> m('Hello|World', '/')
Hello      /
     World /

%> m('a|b|c|d|e|f|g', ',')
a       ,
 b      ,
  c     ,
   d    ,
    e   ,
     f  ,
      g ,

%> m('abcdefg', ':')
abcdefg :

%> m('4|8|15|16|23|42', '%')
4          %
 8         %
  15       %
    16     %
      23   %
        42 %

%> m('E|ac|h s|ecti|on is| one c|haracte|r longer| than the| last!', '!')
E                                                   !
 ac                                                 !
   h s                                              !
      ecti                                          !
          on is                                     !
                one c                               !
                     haracte                        !
                            r longer                !
                                     than the       !
                                              last! !

%> m('This|Code|has||empty||sections', '@')
This                     @
    Code                 @
        has              @
                         @
           empty         @
                         @
                sections @
\$\endgroup\$
2
  • \$\begingroup\$ How about .padRight(s.replace('|','').size()+1)+c) \$\endgroup\$ Commented Sep 9, 2016 at 11:57
  • \$\begingroup\$ good idea! thank you! saved another 2 chars! \$\endgroup\$
    – norganos
    Commented Sep 9, 2016 at 12:55
3
\$\begingroup\$

Python 2, 125 124 132 bytes

-1 byte thanks to @TuukkaX (missed golfing the space from i, v)

def g(s,c):x=s.split('|');print((' '+c+'\n').join(' '*len(''.join(x[:i]))+v+' '*len(''.join(x[i+1:]))for i,v in enumerate(x))+' '+c)

All test cases on ideone

\$\endgroup\$
2
  • 1
    \$\begingroup\$ Your supposed to use c as the comment char, not #. \$\endgroup\$
    – Oliver Ni
    Commented Sep 11, 2016 at 1:01
  • \$\begingroup\$ @OliverNi - uh that was a hit for the code in its current form. \$\endgroup\$ Commented Sep 11, 2016 at 1:10
3
\$\begingroup\$

05AB1E, 29 38 31 29 bytes

'|„ǝʒ:'ǝ¡'ʒмεD®>úsg®+©s}.Bεð²J,

Can definitely be golfed, but at least its working now..
+9 bytes because ¡ (split) removes empty items automatically, so I had to add '|„ǝʒ:'ǝ¡'ʒм..
-2 bytes thanks to @MagicOctopusUrn by changing '|„ǝʒ:'ǝ¡'ʒм to '|¶:.BεðÜ} (current solution doesn't work on items with trailing spaces, but I've assumed that's allowed according to the test cases).

Try it online.

Explanation:

'|¶:           # Take the first input, and replace every "|" with "¶"
               #  i.e. "abc|d|e||fg" → "abc¶d¶e¶¶fg" (¶ are new-lines in 05AB1E)
    .B         # Box all the items (appending trailing whitespace to make it a rectangle)
               #  i.e. "abc¶d¶e¶¶fg" → ['abc','d  ','e  ','   ','fg ']
      εðÜ}     # Remove all trailing spaces from each item
               #  i.e. ['abc','d  ','e  ','   ','fg '] → ['abc','d,'e','','fg']
               #  NOTE: `'|¡` would have resulted in ['abc','d','e','fd'], hence the use of
               #        Box which implicitly splits on new-lines to keep empty items
ε              # For-each:
 D             #  Duplicate the current item
  ®>ú          #  Prepend global_variable + 1 amount of spaces
               #  (+1 because the global_variable is -1 by default)
               #   i.e. "e" and 3+1 → "    e"
 sg            #  Swap so the duplicated item is at the top, and take its length
   ®+          #  Sum it with the global_variable
               #   i.e. "e" (→ 1) and 4 → 5
     ©         #  And store it as new global_variable
      s        #  Then swap so the space appended item is at the end again
       }       # And end the for-each loop
.B             # Box all the items (appending the appropriate amount of spaces)
               #  i.e. ['abc','   d','    e','     ','     fg']
               #   → ['abc    ','   d   ','    e  ','       ','     fg']
ε              # For-each again:
 ð             #  A space character
  I            #  The second input-character
   J           #  Join both together with the current item
    ,          #  And print the current row with trailing new-line
\$\endgroup\$
6
  • \$\begingroup\$ This is invalid if the code were to contain ǝʒ. '|¶:.B could work tho. \$\endgroup\$ Commented Jun 5, 2018 at 20:32
  • \$\begingroup\$ @MagicOctopusUrn The challenge description states "The code input will only contain printable ASCII, and it will not contain any newlines." Also, what part of the code should be replaced with '|¶:.B? \$\endgroup\$ Commented Jun 5, 2018 at 20:42
  • \$\begingroup\$ I was thinking that'd be a shorter split, but it wouldn't work with your current code just slamming it in, you'd have to trim the excess. OR just ignore the excess and .B a second time after adding in the preceeding spaces. \$\endgroup\$ Commented Jun 5, 2018 at 21:08
  • \$\begingroup\$ @MagicOctopusUrn It indeed might save some bytes, since my current work-around is pretty long, but it'll be harder to calculate the amount of preceeding spaces with the spaces after the .B already present. \$\endgroup\$ Commented Jun 5, 2018 at 21:12
  • 1
    \$\begingroup\$ '|¶:.BεðÜ}εD®>úsg®+©s}.BεðIJ,? 29 bytes. Back to iteration 1 :). .B splits on newlines, which is a feature not many people know. It's the only way I know of to keep empty elements. I'd request this as a feature. should mean split, but keep empty elements.. \$\endgroup\$ Commented Jun 5, 2018 at 21:15
3
\$\begingroup\$

Japt -R, 17 bytes

There's a byte to be saved here, at least, I just can't quite see it.

q| ËùT±DÊÃú Ë+ViS

Try it

q| ËùT±DÊÃú Ë+ViS     :Implicit input of strings U=programme & V=character
q|                    :Split U on |
   Ë                  :Map each D
    ù                 :  Left pad with spaces to length
     T±               :    Increment T (initially 0) by
       DÊ             :    Length of D
         Ã            :End map
          ú           :Right pad each with spaces to length of longest
            Ë         :Map
             +        :  Append
              Vi      :    V prepended with
                S     :      Space
                      :Implicit output joined with newlines
\$\endgroup\$
2
2
\$\begingroup\$

PowerShell v2+, 103 99 bytes

param($a,$b)$a-split'\|'|%{" "*$l+$_+" "*(($a-replace'\|').length+1-$_.length-$l)+$b;$l+=$_.Length}

Takes input as two strings, -splits the first on literal pipe (since split uses regex syntax), and feeds the elements into a loop |%{...}.

Each iteration, we construct a string as being a number of spaces defined by $l concatenated with the current element. For the first loop, $l initializes to $null, which gets evaluate here as 0.

That string is further concatenated with another number of spaces (defined by how long $a would be if we -replaced every pipe with nothing, plus 1 for the additional padding between code and comments, minus the .length of the current element, minus $l which is how many spaces we padded left on this iteration), concatenated with our comment character $b. That's left on the pipeline.

We then update $l for the next iteration.

The resultant strings are all left on the pipeline, and output via implicit Write-Output happens at program execution, with a newline between them by default.

Examples

PS C:\Tools\Scripts\golfing> .\esolang-comment-template-generator.ps1 "This|Code|has||empty||sections" "@"
This                     @
    Code                 @
        has              @
                         @
           empty         @
                         @
                sections @

PS C:\Tools\Scripts\golfing> .\esolang-comment-template-generator.ps1 "a|bc|def|ghi|h" "|"
a          |
 bc        |
   def     |
      ghi  |
         h |
\$\endgroup\$
2
\$\begingroup\$

Vim, 39 38 keystrokes

-1 byte thanks to DJMcMayhem

Expects as input a buffer (e.g. a file) whose first character is the comment delimiter, followed by the code, e.g. #foo|bar|baz.

"cxqaf|m`Yp<Ctrl+o>v$r jv0r x@aq@a$p<Ctrl+v>gg$C <Ctrl+r>c<Esc>

Explanation

("_" denotes a literal space.)

"cx          " Delete the first character (the comment delimiter) and store in register 'c'
qa           " Start recording macro 'a'
f|m`         " Advance to the first '|' on the line and set mark
Yp<Ctrl+o>   " Duplicate this line and return to mark
v$r_         " Replace everything after the cursor on this line (inclusive) with spaces
jv0r_x       " Go down a line and replace everything before the cursor on this line (inclusive) with
             "   spaces, then delete one space
@a           " Call macro recursively
q@a          " Stop recording and immediately call the macro
$p           " Paste the deleted space at the end of the last line
<Ctrl+v>gg$       " Highlight the column where the comment delimiters will go and all trailing spaces
C_<Ctrl+r>c<Esc>  " Replace the highlighted text on each line with a space and the contents of
                  "   register 'c' (the comment delimiter)
\$\endgroup\$
2
  • 1
    \$\begingroup\$ :D I always upvote vim! I think you could take one byte off if you change mm to m` and then change `m to <C-o> \$\endgroup\$
    – DJMcMayhem
    Commented Sep 9, 2016 at 22:33
  • \$\begingroup\$ @DJMcMayhem Thanks! I love golfing in Vim because I always learn something about a tool I use every day. \$\endgroup\$
    – Jordan
    Commented Sep 10, 2016 at 15:14
2
\$\begingroup\$

Floroid - 94 bytes

Ah(a,b):c=a.fn("|");z(" "+b+"\n".y(' '*Z("".y(c[:j]))+l+" "*Z("".y(c[j+1:]))Kj,lIai(c))+' '+b)

Uses an approach similar to @JonathanAllans' Python solution.

Testcases

Call: h("a|bc|d|e|fgh|ij|k|l|mn|op", "#")
Output: 
a                #
 bc              #
   d             #
    e            #
     fgh         #
        ij       #
          k      #
           l     #
            mn   #
              op #
\$\endgroup\$
2
\$\begingroup\$

C# 176 167 154 bytes

string f(string s,char x){var c=s.Split('|');var d="";int i=0;foreach(var b in c)d+=b.PadLeft(i+=b.Length).PadRight(s.Length+2-c.Length)+x+"\n";return d;}

UnGolfed

string f(string s, char x)
{
    var c = s.Split('|');
    var d = "";
    int i = 0;
    foreach (var b in c)
        d += b.PadLeft(i += b.Length).PadRight(s.Length + 2 - c.Length) + x + "\n";
    return d;
}

A LINQ solution would have been 146 but needed using System.Linq; bringing it back up to 164:

string f(string s,char x){var c=s.Split('|');int i=0;return c.Aggregate("",(g,b)=>g+b.PadLeft(i+=b.Length).PadRight(s.Length+2-c.Length)+x+"\n");}

Old solutions:

167 bytes:

string f(string s,char x){var c=s.Split('|');var d="";int i=0;foreach(var b in c){d+=b.PadLeft(i+b.Length).PadRight(s.Length+2-c.Length)+x+"\n";i+=b.Length;}return d;}

176 bytes using string interpolation

string f(string s,char x){var c=s.Split('|');var d="";int i=0;foreach(var b in c){d+=string.Format($"{{1,{i}}}{{0,-{s.Length+2-c.Length-i}}}{x}\n",b,"");i+=b.Length;}return d;}
\$\endgroup\$
2
\$\begingroup\$

Jelly, 10 bytes

ṣ”|⁶ṁ;ɗ\pG

Try it online!

\$\endgroup\$
1
2
\$\begingroup\$

AWK, 107 104 103 bytes

w=2-(n=split($1,p,"|"))+gsub(t=".",FS,$1){for(;s++<n;gsub(t,FS,b))y=y substr((b=b p[s])$1,1,w)$2RS}$0=y

Try it online!

Thanks to Pedro Maimere for helping shave off more 3 chars

One interesting thing about this challenge... I found a quirk about the TIO AWK setup. I can't figure out way to specify input where a positional argument can contain a space. Meaning setup a TIO test that behaves like this commandline,

echo "this is the first parameter" second | gawk '{ print $1 }'

In TIO, the first parameter will be "this not the whole string. That means while it works in a terminal window, I can't show that it handle the second test for this challenge in TIO.

Here's how the code works... The test associated with the codeblock is always truthy, so the code always runs. It's only a test (rather then code) to avoid needing a ; character. It does a couple of things by using AWK's willingness to combine multiple expressions into one "line" of code.

w=2-(n=split($1,p,"|"))+gsub(t=".",FS,$1)

First is splits the first parameter into in array of strings using the pipe character as the delimeter with n=split($1,p,"|"). It also saves the number of substrings generated in n. Then it changes all the characters in the first parameter to spaces with gsub(t=".",FS,$1) setting convenience constants t and n in the process.

Finally the value of w is set to the number of non-pipe characters in the first parameter by subtracting the number of sub-strings from the total number of characters in the original.

The associated code block is one statement, a for loop, that processes each substring.

for(;s++<n;gsub(t,FS,b))y=y substr((b=b p[s])$1,1,w)$2RS

The iteration check is simple s++<n. The end of loop statement converts all the characters in the previous output line to blanks gsub(t,FS,b), which turns it into the prefix for the next line. The body of the loop constructs and appends the next line of output to an accumulator y. That string is a concatenation of the previous string, plus the current string (code fragment) with a bunch of blanks appended (meaning $1) (b=b p[s])$1. That mess is truncated to the length we want w and the comment delimiter and LF are appended with y=y substr(...,w)$2RS.

Once all the code substrings have been processes, assigning $0 to the accumulator takes care if printing out the results.

$0=y
\$\endgroup\$
7
  • 1
    \$\begingroup\$ 104 bytes. It is having trouble when the code has spaces. I haven't figured out a way to fix that, though. \$\endgroup\$ Commented Jun 8, 2021 at 0:44
  • 1
    \$\begingroup\$ I keep forgetting to use FS and RS. :) Thanks! The spaces work when I run it from the command line. Meaning when I run it in a terminal window. I think it's something about passing tokenized input to AWK in TIO. I haven't found a way to pass something like "this is one parameter" as $1 in TIO. \$\endgroup\$
    – cnamejj
    Commented Jun 8, 2021 at 8:33
  • \$\begingroup\$ I tried in gawk 5.0.1 and mawk 1.3.3, without success. What version of awk are you using? I also tried another algorithm, but only reached 105 bytes. (sigh) \$\endgroup\$ Commented Jun 8, 2021 at 20:26
  • 1
    \$\begingroup\$ This works on my system echo "E|ac|h s|ecti|on is| one c|haracte|r longer| than the| last!" '!' | gawk 'w=2-(n=split($1,p,"|"))+gsub(t=".",c=FS,$1){for(;s++<n;gsub(t,c,b))y=y substr((b=b p[s])$1,1,w)$2RS}$0=y' and it's on Ubuntu 18.04 running GNU Awk 4.1.4, API: 1.1 (GNU MPFR 4.0.1, GNU MP 6.1.2) \$\endgroup\$
    – cnamejj
    Commented Jun 8, 2021 at 20:42
  • 1
    \$\begingroup\$ I'd post that one as a separate answer since it's a completely different approach. \$\endgroup\$
    – cnamejj
    Commented Jun 8, 2021 at 23:41
2
\$\begingroup\$

Vyxal j, 74 bitsv2, 9.25 bytes

\|/:¦↳:t↲Ḋ

Try it Online!

Bitstring:

01101010100010101110000101000000110001011111111110001111111100110011110001

Explained

\|/:¦↳:t↲Ḋ­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌⁢⁢​‎‏​⁢⁠⁢‌­
\|/         # ‎⁡Split on "|"s
   :¦       # ‎⁢Push the cumulative sums without popping. This acts as the space length before each section
     ↳      # ‎⁣Pad each item right with spaces to length in ^
      :t↲   # ‎⁤And pad each item left with spaces to length of tail of ^
         Ḋ  # ‎⁢⁡Append " {comment char}" to each
‎⁢⁢Implicitly join on newlines
💎

Created with the help of Luminespire and not this challenge.

\$\endgroup\$
1
  • \$\begingroup\$ You should probably make the Try it Online link have a test case that actually shows the task \$\endgroup\$ Commented Oct 8, 2023 at 22:10
2
\$\begingroup\$

JavaScript (Node.js), 79 bytes

c=>s=>s.split`|`.map((_,i,a)=>a.map(y=>i--?y.replace(/./g,' '):y).join``+' '+c)

Attempt This Online!

+4 to add an extra space before #

\$\endgroup\$
2
\$\begingroup\$

Haskell, 96 bytes

f"".lines.map r
r '|'='\n'
r c=c
w=(' '<$)
f l(a:s)c=(w l++a++(w=<<c:s)++c):f(l++a)s c
f l s c=s

Attempt This Online!

Eplanation

The function r is defined to map pipe characters to newlines (r '|'='\n') and every other char to itself (r c=c). lines.map r thus first replaces all pipes in the input string with new lines (map r), and then splits the string at the newlines with the build-in lines function. The main function f iterates over this list of strings and constructs the output line by line with the expression w l++a++(w=<<c:s)++c where

  • a is the part of the code that appears on the current line
  • c is the comment character (as a string) that gets put at the end of the line
  • w is a function that replaces each character of a string by spaces (definde by w=(' '<$))
  • l is the string of code fragments to the left of a, which is put at the beginnig of the line as spaces with w l
  • s is the list of remaining code strings which are also converted to spaces, concatenated, and put after a (c is added to s to get the extra space required before the comment character)
\$\endgroup\$
2
\$\begingroup\$

Perl 5 -pl, 50 bytes

$f=<>;s%\||$%$"x$'=~y/|//c." $f
".$"x$`=~y/|//c%ge

Try it online!

\$\endgroup\$
1
\$\begingroup\$

PHP, 120 117 116 110 109 bytes

foreach($a=split('\|',$argv[1])as$i=>$t){$c=preg_replace('#.#',' ',$a);$c[$i]=$t;echo join($c)," $argv[2]
";}

or

foreach($a=split('\|',$argv[1])as$t){$c=preg_replace('#.#',' ',$a);$c[$i++|0]=$t;echo join($c)," $argv[2]
";}
\$\endgroup\$
1
\$\begingroup\$

MATL, 33 31 bytes

'\|'0'|'hYXo8M&YbY:&YdtaZ)0ihYc

Try it online!

Explanation

The builtin function Yd (blkdiag), which builds a block-diagonal matrix from its inputs, does most of the work. The fill values in the matrix are 0, and char 0 is treated as a space for displaying purposes. The code would simply split on |, build a matrix from the resulting blocks, convert to char, and append two columns with space and comment symbol.

However, the possibility of empty sections in the input string complicates makes the problem more interesting: the resulting block would be empty and thus wouldn't show in the resulting matrix.

To solve this, we introduce a char 0 before each |, so no block will be empty; and then in the resulting char matrix we remove columns that are formed by char 0 only. A non-empty code section will have some printable ASCII char, and thus the columns it spans will survive. An empty section will contribute a row, but won't introduce an extra column.

'\|'    % Push this string: source for regexp matching. It's just | escaped
0'|'h   % Push a string formed by char 0 followed by | (no escaping needed)
YX      % Input string implicitly. Replace first of the above string by the second
o       % Convert from chars to code points. Gives a numeric vector
8M      % Push '|' again
&Yb     % Split numeric vector at occurences of | (the latter is automatically
        % converted  to its code point). This gives a cell array of numeric vectors
Y:      % Unbox cell array: pushes the numeric vectors it contains
&Yd     % Form a block-diagonal matrix from those vectors
ta      % Duplicate. Compute vector that equals true for columns that have some
        % nonzero value
Z)      % Used that as a logical index (mask) for the columns of the matrix.
        % This removes columns that contain only zeros
0ih     % Input comment symbol and prepend char 0 (which will be displayed as space)
Yc      % Append that to each row of the matrix. The matrix is automatically 
        % converted from code points to chars
        % Display implicitly
\$\endgroup\$
4
  • 1
    \$\begingroup\$ I'm vaguely disappointed that you didn't put your explanation in the format the OP mentioned \$\endgroup\$
    – Random832
    Commented Sep 9, 2016 at 18:30
  • 1
    \$\begingroup\$ @Random832 I don't use that format often. It uses up a lot of space, leaving little room for the explanations \$\endgroup\$
    – Luis Mendo
    Commented Sep 9, 2016 at 18:37
  • \$\begingroup\$ Why is the escape needed in the first string? \$\endgroup\$ Commented Sep 11, 2016 at 4:04
  • \$\begingroup\$ @ConorO'Brien Good question. I never know which/when special symbols need escaping and which/when not. In this case, | (match subexpression before or after the |) does need it, at least in the Matlab/Octave regexp engine \$\endgroup\$
    – Luis Mendo
    Commented Sep 11, 2016 at 11:01
1
\$\begingroup\$

Ruby, 96 80 bytes

->s,c{s.gsub(/(^|\|)([^|]*)/){" "*$`.count(t="^|")+$2+" "*(1+$'.count(t))+c+$/}}

See it on eval.in: https://eval.in/639012

I really ought to just learn Retina.

\$\endgroup\$
1
\$\begingroup\$

Pyth, 30 bytes

VJcE\|s[*ZdN*h--lsJZlNdQ)=+ZlN

or

jm+dQ.t.t+MC,.u*l+NYdJc+Ed\|kJ

Both are full programs that take input on STDIN of the comment string, and then the program string, newline-separated.

Try the first version online

Try the second version online

How they work

VJcE\|s[*ZdN*h--lsJZlNdQ)=+ZlN  Program. Inputs: E, Q
  cE\|                          Split E on "|"
 J                              Assign to J
                                Implicit Z=0
V                               For N in that:
       [                )        Create a list with elements:
        *Zd                       Z spaces
           N                      N
               -lsJZ              len(concatenate(J))-Z
              -     lN             -len(N)
             h                     +1
            *         d            spaces
                       Q          Q
      s                          Concatenate the list
                                 Implicitly print
                        =+ZlN    Z=Z+len(N)

jm+dQ.t.t+MC,.u*l+NYdJc+Ed\|kJ  Program. Inputs: E, Q
                       +Ed      Add a trailing space to E
                      c   \|    Split that on "|"
                     J          Assign to J
             .u                 Cumulatively reduce J with:
                            k    starting value empty string and
                                 function N, Y ->
                l+NY              len(N+Y)
               *    d             spaces
            ,                J  Two-element list of that and J
           C                    Transpose
         +M                     Map concatenation over that
       .t                       Transpose, padding with spaces
     .t                         Transpose again
 m+dQ                           Map concatenation with Q over that
j                               Join on newlines
                                Implicitly print
\$\endgroup\$
1
\$\begingroup\$

Perl, 63 bytes

Includes +5 for -Xpi

Run with input on STDIN and comment character after -i:

perl -Xpi% esolang.pl <<< "Ab|Cd||ef"

esolang.pl:

s/
/|/;s%(.*?)\|%$"x$`=~y/|//c.$1.$"x$'=~y/|//c." $^I
"%eg

Totally boring straightforward solution

\$\endgroup\$
1
\$\begingroup\$

Turtlèd, 35 bytes (noncompeting)

Takes one input, the last character is the comment character. Does not work with comment character as space, but I assume that isn't necessary.

!' [*.+(|' dl)r_]' r[*+.(|u)_][ .d]

Explanation:

!                                  take input into string variable
 '                                 write space over current cell
   [*           ]                  while cell is not *
     .+                            write pointed char of string, stringpointer+1 (starts 0)
       (|    )                     if current cell is |
         ' dl                      write space (over |), move down, left
              r_                   move right, write * if pointed char is
                                   last char, else space

                 ' r               write space, move right
                    [*       ]     while cell is not *
                      +.           increment pointer and write pointed char
                        (|u)       if cell is |, move up
                            _      write * if the pointed char is the last char

                              [   ] while cell is not space
                                .d  write the pointed char from string, move down 
\$\endgroup\$
1
\$\begingroup\$

GolfScript, 85 bytes

{(;);}:r;", "%(r\(r n+:c;;.,\'|'%.,@\-)):l;0:m;{.,0>}{" "m*\(.,m+:m l\-" "\*+c@}while

Try it online

2017 Update - GolfScript - 71 bytes

', '/~~:c;~1/.,\.{'|'=},,@\-):l;['|']/0:i;{.,i+:i l i-' '*c+\' '*"
"\}/

Explanation

', '/~~:c;~1/        # Parses input
.,\.{'|'=},,@\-):l;  # Computes string length without '|'
['|']/               # Splits the array
0:i;                 # Counter
{.,                  # Length of the substring
i+:i                 # Counter update
l i-' '*c+\          # Adds spaces after the substring 
' '*"\n"\            # Adds spaces before the next substring
}/                   # ...For each substring
\$\endgroup\$
1
\$\begingroup\$

Funky, 89 bytes

s=>c=>{y=0forn inl=s::split"|"print((v=" "::rep)(y)+l[n]+v((#l::reduce@++1)-y+=#l[n])+c)}

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.