# Parse the comments out of my esoteric code

Earlier this week, we learned about how to format esoteric languages for commenting. Today, we're going to do the inverse of that. I need you to write a program or function that parses some well-commented esoteric code and parses the comments out, returning just the code. Using some examples from the previous challenge, here is what well-commented code looks like:

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


Here is what you need to do to extract the code out. First, remove the comment character (#), the space before it, and everything after the comment character.

a
bc
d
e
fgh
ij
k
l
mn
op


Then, collapse each line upwards into a single line. For example, since b is in the second column on line two, once we collapse it up, it will be in the second column on line one. Similarly, c will be put in the third column of line one, and d will be put on the fourth. Repeat this for every character, and you get this:

abcdefghijklmnop


Important note: It seems like the trivial solution is to just remove the comments, remove every space, and join every line. This is not a valid approach! Because the original code might have spaces in it, these will get stripped out with this approach. For example, this is a perfectly valid input:

hello         #Line one
#Line two
world! #Line three


And the corresponding output should be:

hello  world!


# The Challenge:

Write a program or function that takes commented code as input, and outputs or returns the code with all the comments parsed out of it. You should output the code without any trailing spaces, although one trailing newline is permissible. The comment character will always be #, and there will always be one extra space before the comments start. # will not appear in the comment section of the input. In order to keep the challenge simpler, here are some inputs you do not have to handle:

• You can assume that the code will not have two characters in the same column. For example, this is an input that violates this rule:

a  #A character in column one
bc #Characters in columns one and two

• You can also assume that all comment characters appear in the same column. For example, this input:

short       #this is a short line
long        #This is a long line


violates this rule. This also means that # will not be in the code section.

• And lastly, you do not have to handle code sections with leading or trailing spaces. For example,

  Hello,          #
World!   #


You may also assume that the input only contains printable ASCII characters.

# Examples:

Input:
hello         #Line one
#Line two
world! #Line three

Output:
hello  world!

Input:
E                                                   #This comment intentionally left blank
ac                                                 #
h s                                              #
ecti                                          #
on is                                     #
one c                               #
haracte                        #
r longer                #
than the       #
last! #

Output:
Each section is one character longer than the last!

Input:
4          #This number is 7
8         #
15       #That last comment is wrong.
16     #
23   #
42 #

Output:
4815162342

Input:
Hello                     #Comment 1
world               #Comment 2
,              #Comment 3
how          #Comment 4
are      #Comment 5
you? #Comment 6

Output:
Hello world, how are you?

Input:
Prepare                               #
for...                        #
extra spaces! #

Output:
Prepare for...          extra spaces!


You may take input in whatever reasonable format you like, for example, a list of strings, a single string with newlines, a 2d list of characters, etc. The shortest answer in bytes wins!

• Will we need to accept code with characters lower than the next? – wizzwizz4 Sep 13 '16 at 6:41
• Could you add the test case with the empty line with just two spaces (like the hello world! you've showed)? Also, you state: "# will not appear in the comment section of the input.", but can it occur in the code-snippet itself? – Kevin Cruijssen Sep 13 '16 at 7:01
• @KevinCruijssen See my edits – James Sep 13 '16 at 7:09
• @wizzwizz4 I'm not sure if I understand your question – James Sep 13 '16 at 7:10
• @DJMcMayhem Example: do {stuff} while (condition); with the explanation in order do while (condition); #Explainything then {stuff} #Explainything. – wizzwizz4 Sep 13 '16 at 15:45

# Jelly, 8 7 bytes

»/ṣ”#ḢṖ


Try it online!

### How it works

»/ṣ”#ḢṖ  Main link. Argument: A (array of strings)

»/       Reduce the columns of A by maximum.
Since the space is the lowest printable ASCII characters, this returns the
non-space character (if any) of each column.
ṣ”#    Split the result at occurrences of '#'.
Ḣ   Head; extract the first chunk, i.e., everything before the (first) '#'.
Ṗ  Pop; remove the trailing space.

• That is just ...wow. – Jonathan Allan Sep 13 '16 at 2:26
• I am so jelly right now. – MonkeyZeus Sep 13 '16 at 13:49
• How do you even hack that into your phone? – simbabque Sep 13 '16 at 16:14
• @simbabque Patience and a lot of copy-pasting. – Dennis Sep 13 '16 at 16:49
• I'm always putting using a 9-iron, maybe it's time I learned how to use a putter when on the green... – Magic Octopus Urn Sep 13 '16 at 20:19

# Python 2, 48 43 bytes

lambda x:map(max,*x)[2::5].split(' #')[0]


Thanks to @xnor for golfing off 5 bytes!

Test it on Ideone.

• I think you can just do map(max,*x) because max takes any number of arguments and None is small. – xnor Sep 13 '16 at 3:15
• Right, I always forget that map can be used like that... Thanks! – Dennis Sep 13 '16 at 3:28
• How does the ...[2::5] trick work? – smls Sep 14 '16 at 6:06
• @smls ... is equivalent to repr(...), so for the list of singleton strings ['a', 'b', 'c'], you get the string "['a', 'b', 'c']". Finally, [2::5] chops off the first two characters ("['") and takes every fifth character of the remaining string. – Dennis Sep 14 '16 at 6:22

# JavaScript (ES6), 9775 60 bytes

Thanks to @Neil for helping golf off 22 bytes

a=>a.reduce((p,c)=>p.replace(/ /g,(m,o)=>c[o])).split #[0]


Input is an array of lines.

• a is array input
• p is previous item
• c is current item
• m is match string
• o is offset
• I count 96 bytes? Also, the m regexp flag is unnecessary (did you have a $ at one point?) as is the space in (p, c). Finally, I think replace will work out shorter than [...p].map().join. – Neil Sep 13 '16 at 8:00 • 97 for me, both from manual length and userscript, maybe you didn't count the newline, but only because I accidentally included the semicolon – ASCII-only Sep 13 '16 at 8:39 • I see now - I hadn't copied the ; which isn't required (JavaScript has ASI). – Neil Sep 13 '16 at 8:41 • Yeah, sorry, I had it to make sure Chromium console puts the function call outside the function body (had it once on a badly written lambda) – ASCII-only Sep 13 '16 at 8:43 • Oh wow, I didn't realise replace would help so much, that's really neat! – Neil Sep 13 '16 at 8:47 # Perl, 3534 32 bytes Includes +1 for -p Give input on STDIN eso.pl #!/usr/bin/perl -p y/ /\0/;/.#/;$\|=$}{$\=~y;\0;


Notice that there is a space after the final ;. The code works as shown, but replace \0 by the literal character to get the claimed score.

• Very nice code. That $a|=... is rather well done, it took me a while to figure out what you were doing! One question though : *_=a seems to be roughly equivalent to $_=$a, why is that? – Dada Sep 13 '16 at 12:45 • *_=a is a very obscure glob assignment which aliases the _ globals and the a globals. So it's not so much a copy from $a to $_ but from that point on (global) $a and $_ are actually the same variable. All to save 1 byte... – Ton Hospel Sep 13 '16 at 13:27 • Ok, thanks for the explanation! (and nice improvement thanks to $\) – Dada Sep 13 '16 at 14:46

## Python 2, 187 bytes

def f(x,o=""):
l=[i[:i.index("#")-1]for i in x]
for n in range(len(l[0])):
c=[x[n]for x in l]
if sum([1for x in c if x!=" "])<1:o+=" "
else:o+=[x for x in c if x!=" "][0]
print o


I'm gonna golf this more tomorrow I have school ;)

• 1 for can be reduced to 1for. Also, if the sum of the list (at line 5) can't be negative, you can just check for <1 instead of ==0. Happy school day! :D +1. – Yytsi Sep 13 '16 at 21:35

# Ruby, 63 bytes

Basically a port of Dennis' Jelly answer. Takes input as an array of strings.

->a{l,=a
l.gsub(/./){a.map{|m|m[$.size]||$/}.max}[/(.+) #/,1]}


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

## CJam, 12 bytes

Thanks to Sp3000 for saving 2 bytes.

{:.e>_'##(<}


An unnamed block that takes a list of strings (one for each line) and replaces it with a single string.

Try it online!

### Explanation

:.e>  e# Reduce the list of strings by elementwise maximum. This keeps non-spaces in
e# favour of spaces. It'll also wreak havoc with the comments, but we'll discard
e# those anyway.
_'##  e# Duplicate and find the index of '#'.
(<    e# Decrement that index and truncate the string to this length.


# J, 30 bytes

(#~[:<./\'#'~:])@(>./&.(3&u:))


Takes a list of strings as input. Basically uses the same approach as Dennis in his Jelly answer.

## Commented and explained

ord =: 3 & u:
under =: &.
max =: >./
over =: @
maxes =: max under ord
neq =: ~:
arg =: ]
runningMin =: <./\
magic =: #~ [: runningMin ('#' neq arg)

f =: magic over maxes


Intermediate steps:

   p
Hello                     #Comment 1
world               #Comment 2
,              #Comment 3
how          #Comment 4
are      #Comment 5
you? #Comment 6
maxes p
Hello world, how are you? #Comment 6
magic
#~ ([: runningMin '#' neq arg)
3 neq 4
1
'#' neq '~'
1
'#' neq '#'
0
'#' neq maxes p
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1
runningMin 5 4 2 5 9 0 _3 4 _10
5 4 2 2 2 0 _3 _3 _10
runningMin '#' neq maxes p
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0
0 1 0 1 1 0 # 'abcdef'
bde
'abcdef' #~ 0 1 0 1 1 0
bde
(maxes p) #~ runningMin '#' neq maxes p
Hello world, how are you?
(#~ [: runningMin '#' neq arg) maxes p
Hello world, how are you?
((#~ [: runningMin '#' neq arg) over maxes) p
Hello world, how are you?
(magic over maxes) p
Hello world, how are you?


## Test case

   f =: (#~[:<./\'#'~:])@(>./&.(3&u:))
a
Hello                     #Comment 1
world               #Comment 2
,              #Comment 3
how          #Comment 4
are      #Comment 5
you? #Comment 6
t;/#[^ ]/!{H;s,#.,#,g};t;g;s,\n#(.)[^\n]*,\1,g;s,...$,,  Requires a newline at the end of the input. I'm sure I can golf this a little more, but I'm just happy it works for now. # Perl 6, 39 bytes {[Zmax](@_».comb).join.split(' #')[0]}  Translation of the Python solution by Dennis. Takes input as a list of strings, and returns a string. # Jelly, 27 bytes żḟ€” ;€” Ḣ€ i€”#’©ḣ@"ç/ḣ®ṪṖ  Test it at TryItOnline Uses the strictest spec - the extra space before the comment character is removed at the cost of a byte. Input is a list of strings. • @Erik the Golfer - maybe so, but did you see the crushing he gave me here? – Jonathan Allan Sep 13 '16 at 12:45 ## Ruby, 77 bytes puts File.readlines("stack.txt").join('').gsub(/\s{1}#.*\n/,'').gsub(/\s/,'')  • Hardcoding an input filename is not an acceptable method of input. – Mego Sep 14 '16 at 3:36 • @Mego, where can I find the rules of what's "acceptable"? – Forwarding Sep 14 '16 at 3:53 • meta.codegolf.stackexchange.com/q/2447/45941 – Mego Sep 14 '16 at 3:57 # TSQL, 216 175 bytes Golfed: DECLARE @ varchar(max)= 'hello #Line one #Line two world! #Line three' DECLARE @i INT=1,@j INT=0WHILE @i<LEN(@)SELECT @=stuff(@,@j+1,len(x),x),@j=iif(x=char(10),0,@j+1),@i+=1FROM(SELECT ltrim(substring(@,@i,1))x)x PRINT LEFT(@,patindex('%_#%',@))  Ungolfed: DECLARE @ varchar(max)= 'hello #Line one #Line two world! #Line three' DECLARE @i INT=1,@j INT=0 WHILE @i<LEN(@) SELECT @=stuff(@,@j+1,len(x),x),@j=iif(x=char(10),0,@j+1),@i+=1 FROM(SELECT ltrim(substring(@,@i,1))x)x PRINT LEFT(@,patindex('%_#%',@))  Fiddle # Javascript, 56 34 bytes, non-competing q=>q.split(/\n/).map(x=>/ (.?) #./.exec(x)[1]).join() q=>q.replace(/^ *| *#.*$\n?/gm,'')


As @n̴̖̋h̷͉̃a̷̭̿h̸̡̅ẗ̵̨́d̷̰̀ĥ̷̳ pointed out, I am not prepared for extra spaces

# Dyalog APL, 22 bytes

(⎕UCS¯2↓⍳∘35↑⊢)⌈⌿∘⎕UCS


(

⎕UCS character representation of

¯2↓ all but the last two of

⍳∘35↑ up until the position of the first 35 ("#"), in that which is outside the parenthesis, taken from

⊢ that which is outside the parenthesis

) namely...

⌈⌿ the columnar maximums

∘ of

⎕UCS` the Unicode values

TryAPL online!

• How many bytes? – acrolith Sep 14 '16 at 1:34