# I reverse the source code, you negate the input!

Blatant rip-off of a rip-off. Go upvote those!

Your task, if you wish to accept it, is to write a program/function that outputs/returns its integer input/argument. The tricky part is that if I reverse your source code, the output must be the original integer negated.

## Examples

Let's say your source code is ABC and its input is 4. If I write CBA instead and run it, the output must be -4.

Let's say your source code is ABC and its input is -2. If I write CBA instead and run it, the output must be 2.

An input of 0 may give 0 or -0, however, if you do support signed zero, -0 should give 0.

• Why do we need a copy of the same question? – Christian Sep 18 '19 at 7:30
• @Christian That one outputs a constant number (and its negation) whereas this one has to take input and return/negate it. A very different job in a lot of languages. – Adám Sep 18 '19 at 7:32
• A yes, now I see the difference. One needs to read VERY carefully – Christian Sep 18 '19 at 7:34
• If using a structured language like C#, are you just reversing lines? – Emma - PerpetualJ Sep 19 '19 at 2:31
• @PerpetualJ No, look at the source like list of characters, some of which are line breaks. – Adám Sep 19 '19 at 5:42

# Brachylog, 2 bytes

&ṅ

Brachylog implicitly inputs from the left and outputs from the right.
& ignores anything to the left and passes the input to the function rightwards.
constrains each side of it to be negated versions of each other.

Try it online

# Triangular, 8 7 bytes

$%%.|.$

Try it online!

Ungolfed:

$|$: Read an Integer
% %          | %: Output it
. | .

Try it online!

Reverse:

Try it online!

# PHP, 20 bytes

<?=$argn;#;ngra$-=?<

Try it online!

# MathGolf, 4 bytes

*b╘k

Explanation:

Regular:

*     # Multiply the (implicit) input with the (implicit) input
#  STACK: [input**2]
b    # Push -1
#  STACK [input**2, -1]
╘   # Discard everything on the stack
#  STACK: []
k  # Push the input as integer
#  STACK: [input]
# (output the entire stack joined together as result)

Reversed:

k     # Push the input as integer
#  STACK: [input]
╘    # Discard everything on the stack
#  STACK: []
b   # Push -1
#  STACK: [-1]
*  # Multiple the (implicit) input with this -1
#  STACK: [-input]
# (output the entire stack joined together as result)

And to answer your question: no, MathGolf does not have a 1-byte negate for integers. There is ~ for -n-1, but unfortunately nothing for -n (so *b could alternatively be )~ for the same byte-count).

n U

Try it

Try it reversed

nÎN

Try it

Try it reversed

# C++ (gcc), 29 bytes

#define f(x)x//x-)x(g enifed#

Try it online!

Fixed issue brought up by @Nick Kennedy

• This is shown the wrong way round; it should return the input when called without reversing the code. – Nick Kennedy Sep 18 '19 at 10:51
• Is #define f//- f enifed# (call as f x) cheating? ;) – Quentin Sep 19 '19 at 15:54

# Runic Enchantments, 4 bytes

i@Zi

Its the answer I originally wrote on the other challenge when I misread the description.

# Gol><>, 4 bytes

Ih-I

One byte golfed off courtesy of JoKing

Try it online!

• @JoKing Thank you very much, is that a negate operation? I honestly forgot that there was one – KrystosTheOverlord Sep 18 '19 at 23:43
• @JoKing Ohhhhh, I didn't know that, thank you very much for enlightening me on that operation – KrystosTheOverlord Sep 19 '19 at 0:16

# Scala, 6 bytes

+_//_-

Try it online!

Unfortunately a function can't just contain a _ to return the first argument, but using unary_+ will return the value unchanged.

Once flipped, the code is -_ which does a unary_- on the first argument, which flips the sign.

• Shouldn't this be _//_-? However, _-0 seems like a valid solution. – Adám Sep 18 '19 at 18:45
• @Adám It should be +_//_-. Unfortunately can't have _ as the body of the function, but doing +_ returns the value unchanged. Thanks for point it out! – Soapy Sep 19 '19 at 8:43

## Perl 5 - 12 Bytes

say$_#_$-yas

### Reversed

say-$_#_$yas

Abides by the -0 rule.

Does require the -nE flags on execution.

# Retina 0.8.2, 13 12 bytes

-*$1#$
)^(|-

Try it online! It's impossible for the first line to match anything, so nothing happens. In reverse:

-|(^)
$#1$*-

Try it online! Explanation: Either a leading (implicitly) - or a leading empty string is matched. This is replaced with - repeated according to the number of empty strings that were matched.

Edit: Saved 1 byte thanks to @MartinEnder (although, cunningly, the reversed original worked with floating-point numbers in scientific notation). As he points out, Retina 1 would be a further byte shorter as its syntax for * is slightly different and does not require the adjacent $(and it would therefore also modify illegal inputs such as -1# which this version ignores). • I don't think you need the ^ at the end. Also, Retina 1 would save a byte on$*. – Martin Ender Sep 21 '19 at 10:15

# BitCycle-U, 15 bytes

First, a word about the -U flag. Internally, BitCycle only deals in 1's and 0's, so to allow working with decimal integers, it has flags to convert decimal input to unary. In particular, -U allows for signed integers by adding a 0 to the front of any nonpositive integer's unary representation: 4 is 1111, -4 is 01111, and 0 is 0. The same transformation is applied in reverse to the output, with the convenient addition that empty output is treated as 0.

### Forward

^>

?!+?
<0/

Try it online!

The leftmost ? gets the input, which goes straight into the ! to be output unchanged.

### Reverse

/0<
?+!?

>^

Try it online!

The leftmost ? gets the input. The + sends the leading 0 bit, if any, left (north), and the 1 bits right (south).

If there is a leading 0 bit, it hits the splitter / and turns east, deactivating the splitter. The < sends it west again, through the deactivated splitter and off the playfield. The 0 bit that started on the playfield also hits the < and goes through the deactivated splitter and off the playfield. Meanwhile, the 1 bits are directed around by the > and ^ and reach the !, where they are output.

If there is no leading 0 bit, the 0 bit that starts on the playfield hits the < and goes west to the splitter /. Since the splitter has not been deactivated, it directs the bit south, and the + sends it west into the output !. Meanwhile, the 1 bits are directed to the output, with enough delay to make sure they get there after the 0 bit.

TL;DR: Nonpositive inputs have their leading 0 bit stripped and their magnitude is output. Positive inputs have a leading 0 bit added and are thus output as negative numbers of the same magnitude.

• @JoKing That's clever! It's really different from mine; do you want to post it yourself? – DLosc Sep 24 '19 at 17:00
• Posted – Jo King Sep 24 '19 at 21:54

# BitCycle-U, 14 bytes

<  ^
v/?!
+~ +

Try it online!

And reversed

+ ~+
!?/v
^  <

Try it online!

The -U flag translates input/output to signed Unary, i.e. unary 1s with a zero in front if the number is negative. The first program just pipes input (?) to the output (!) directly right of it.

The reverse instead takes only the first bit of the number with the / command and dupnegs (~). If it is a 1 then it sends 0 left and 1 right, where the +s redirect them to join the flow to the output. If the first bit is 0 instead, the 0 goes right and the 1 goes left, and the + redirect them away from the output. All in all, this has the behaviour of stripping a leading zero if the input has one, otherwise prepending a zero if it doesn't, which means inverting the sign of the input.

# Keg, 2 bytes

Real simple. When run in normal direction, it simply prints the input. When run in reverse direction (±#), it negates the input.

# TI-BASIC, 3 bytes

Ans-0

Very simple and works the same way as the top J answer.

Input is an integer in Ans.

Examples:

4:Ans-0
4
4:0-Ans
-4

@IO\n

Try it online!

Try it reversed!

# Lua, 23 bytes

print(...)--)...-(tnirp

Try it online!

Comment abuse all over again.

# -
&#

Try it online!

and reversed:

#&
- #

Try it online!

I think this is the optimal answer for this question. In order to negate the input, we have to use the - operator, which means the code must be at least three wide so the left and the right operands aren't the same. It must then be two tall to have not loop infinitely. That means at least one row must be three wide, and the other has to be two wide, since if it was one wide, it must be the redirect to the #, which would have to be directly under it. Feel free to prove me wrong though.

### Explanation:

For the initial program, we only execute # (integer output) on & (integer input). Basically, the only instructions that matter are:

#
&

For the reversed program, the # on the second line leads to - (left minus right). This wraps around the program, with the right being the &, and the left as an empty space, which is zero. This results in 0 - input, which is the negated input.