Ruby, 8080 74 bytes
->s>i,c{z=0;sa=0;(i=i.split( ?|).map{["" "*z+_1,z+=_1.size][0].ljust"*a+_1+" "*(1+s1+i.trjoin.size-(?|,"")a+=_1.size))+c}}
Attempt This Online!Attempt This Online!
I managed to golf a non-regex based solution to tie withbeat the other Ruby. There might still beThe solution previously tied but I found a coupleslightly different method that is 6 bytes to be golfed off. I’m still learning Ruby so it took a while to get it this shortshorter.
Explanation:
->s>i,c{…}
Proc (lambda function) taking arguments si
(code) and c
(comment)
z=0;a=0;
Assign variableza
to zeros(i=i.split( ?|)
SplitAssignsi
to it split on|
.map{…}
Map each_1
in the list to:["" "*z+_1,z+=_1.size][0]
Return_1"*a
withza
spaces prepended, before adding the length of_1
toz+_1
. Append the list item.ljust+" "*(…)
Append this many spaces until the length is equal to…:1+s1+i.tr(?|,"")join.size
One more than the lengthThe size ofsi
with|
removed, plus one for the extra space-(a+=_1.size)
Add the size of the list item toa
and subtracta
from this.
+c
Append the comment character to the end of the line.