Skip to main content
deleted 184 characters in body
Source Link
nwellnhof
  • 10.6k
  • 1
  • 19
  • 38

Perl 5 -p, 50 4949 48 bytes

s/.{5} (.=)?/:/g;s/if.(.*?)\)(.=)?/\1?/g;$_=$2.$_
s/if.(.*?)\)(.=)/\2\1?/g;s/.if./?/g;s/;.{6}=/:/g

Try it online!Try it online!

The 48 byte version is inspired by Neil's Retina answer.

Explanation

# Replace any 5 chars followed by space and possibly an assignment
# like "x=" with a colon. Since whitespace is only allowed after
# "else" and "else" is always preceded by ";", this is the same as
# matching ";else (.=)?", but one byte shorter.
s/.{5} (.=)?/:/g;
# Replace "if(expr)" possibly followed by an assignment like "x="
# with "expr?".
s/if.(.*?)\)(.=)?/\1?/g;
# Prepend an assignment like "x=" from the final match.
$_=$2.$_
# Replace "if(e1)if(e2)x=" with "x=e1)if(e2?"
s/if.(.*?)\)(.=)/\2\1?/g;
# Replace ")if(" with "?"
s/.if./?/g;
# Replace ";else x=" with ":"
s/;.{6}=/:/g

Old 49 byte solution

s/.{5} (.=)?/:/g;s/if.(.*?)\)(.=)?/\1?/g;$_=$2.$_

Perl 5 -p, 50 49 bytes

s/.{5} (.=)?/:/g;s/if.(.*?)\)(.=)?/\1?/g;$_=$2.$_

Try it online!

Explanation

# Replace any 5 chars followed by space and possibly an assignment
# like "x=" with a colon. Since whitespace is only allowed after
# "else" and "else" is always preceded by ";", this is the same as
# matching ";else (.=)?", but one byte shorter.
s/.{5} (.=)?/:/g;
# Replace "if(expr)" possibly followed by an assignment like "x="
# with "expr?".
s/if.(.*?)\)(.=)?/\1?/g;
# Prepend an assignment like "x=" from the final match.
$_=$2.$_

Perl 5 -p, 50 49 48 bytes

s/if.(.*?)\)(.=)/\2\1?/g;s/.if./?/g;s/;.{6}=/:/g

Try it online!

The 48 byte version is inspired by Neil's Retina answer.

Explanation

# Replace "if(e1)if(e2)x=" with "x=e1)if(e2?"
s/if.(.*?)\)(.=)/\2\1?/g;
# Replace ")if(" with "?"
s/.if./?/g;
# Replace ";else x=" with ":"
s/;.{6}=/:/g

Old 49 byte solution

s/.{5} (.=)?/:/g;s/if.(.*?)\)(.=)?/\1?/g;$_=$2.$_
added 15 characters in body
Source Link
nwellnhof
  • 10.6k
  • 1
  • 19
  • 38

Perl 5 -p, 50 49 bytes

s/if.(.*?)\)(.=)?/\1?/g;s/.{5} (.=)?/:/g;$_=$1.$_

Try it online!

Explanation

# Replace "if(expr)" possibly followed by an assignment like "x="
# with "expr?".
sg;s/if.(.*?)\)(.=)?/\1?/g;g;$_=$2.$_

Try it online!

Explanation

# Replace any 5 chars followed by space and possibly an assignment
# like "x=" with a colon. Since whitespace is only allowed after
# "else" and "else" is always preceded by ";", this is the same as
# matching ";else (.=)?", but one byte shorter.
s/.{5} (.=)?/:/g;
# Replace "if(expr)" possibly followed by an assignment like "x="
# with "expr?".
s/if.(.*?)\)(.=)?/\1?/g;
# Prepend an assignment like "x=" from the final match.
$_=$1$_=$2.$_

Perl 5 -p, 50 49 bytes

s/if.(.*?)\)(.=)?/\1?/g;s/.{5} (.=)?/:/g;$_=$1.$_

Try it online!

Explanation

# Replace "if(expr)" possibly followed by an assignment like "x="
# with "expr?".
s/if.(.*?)\)(.=)?/\1?/g;
# Replace any 5 chars followed by space and possibly an assignment
# like "x=" with a colon. Since whitespace is only allowed after
# "else" and "else" is always preceded by ";", this is the same as
# matching ";else (.=)?", but one byte shorter.
s/.{5} (.=)?/:/g;
# Prepend an assignment like "x=" from the final match.
$_=$1.$_

Perl 5 -p, 50 49 bytes

s/.{5} (.=)?/:/g;s/if.(.*?)\)(.=)?/\1?/g;$_=$2.$_

Try it online!

Explanation

# Replace any 5 chars followed by space and possibly an assignment
# like "x=" with a colon. Since whitespace is only allowed after
# "else" and "else" is always preceded by ";", this is the same as
# matching ";else (.=)?", but one byte shorter.
s/.{5} (.=)?/:/g;
# Replace "if(expr)" possibly followed by an assignment like "x="
# with "expr?".
s/if.(.*?)\)(.=)?/\1?/g;
# Prepend an assignment like "x=" from the final match.
$_=$2.$_
added 508 characters in body
Source Link
nwellnhof
  • 10.6k
  • 1
  • 19
  • 38

Perl 5 -p, 50 49 bytes

s/;if.(.*?)\)(.=)?/\1?/g;s/.{5} (.=)?/:/g;sg;$_=$1.$_

Try it online!

Explanation

# Replace "if(expr)" possibly followed by an assignment like "x="
# with "expr?".
s/if.(.*?)\)(.=)?/\1?/g;$_=$2g;
# Replace any 5 chars followed by space and possibly an assignment
# like "x=" with a colon. Since whitespace is only allowed after
# "else" and "else" is always preceded by ";", this is the same as
# matching ";else (.=)?", but one byte shorter.
s/.{5} (.=)?/:/g;
# Prepend an assignment like "x=" from the final match.
$_=$1.$_

Try it online!

Perl 5 -p, 50 49 bytes

s/;.{5}(.=)?/:/g;s/if.(.*?)\)(.=)?/\1?/g;$_=$2.$_

Try it online!

Perl 5 -p, 50 49 bytes

s/if.(.*?)\)(.=)?/\1?/g;s/.{5} (.=)?/:/g;$_=$1.$_

Try it online!

Explanation

# Replace "if(expr)" possibly followed by an assignment like "x="
# with "expr?".
s/if.(.*?)\)(.=)?/\1?/g;
# Replace any 5 chars followed by space and possibly an assignment
# like "x=" with a colon. Since whitespace is only allowed after
# "else" and "else" is always preceded by ";", this is the same as
# matching ";else (.=)?", but one byte shorter.
s/.{5} (.=)?/:/g;
# Prepend an assignment like "x=" from the final match.
$_=$1.$_
added 13 characters in body
Source Link
nwellnhof
  • 10.6k
  • 1
  • 19
  • 38
Loading
Source Link
nwellnhof
  • 10.6k
  • 1
  • 19
  • 38
Loading