I reverse the source code, you negate the input!

40

2

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.

Adám

Posted 2019-09-17T19:32:25.203

Reputation: 37 779

5Why do we need a copy of the same question? – Christian – 2019-09-18T07:30:59.957

5@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 – 2019-09-18T07:32:12.453

5A yes, now I see the difference. One needs to read VERY carefully – Christian – 2019-09-18T07:34:31.927

If using a structured language like C#, are you just reversing lines? – Emma - PerpetualJ – 2019-09-19T02:31:03.343

@PerpetualJ No, look at the source like list of characters, some of which are line breaks. – Adám – 2019-09-19T05:42:56.903

Answers

21

J, 3 bytes

-&0

Try it online!

-&0 is "argument minus 0"

0&- is "0 minus argument"

Jonah

Posted 2019-09-17T19:32:25.203

Reputation: 8 729

3this is really quite nice :) – Conor O'Brien – 2019-09-18T02:59:19.720

3Clever, I didn't think of that one, but rather ][- etc. – Adám – 2019-09-18T05:35:24.587

19

PowerShell, 18 14 bytes

$args#"sgra$"-

Try it online! !enilno ti yrT

First of the trivial comment-abuse answers

Veskah

Posted 2019-09-17T19:32:25.203

Reputation: 3 580

5Oh, God. Not this again. – S.S. Anne – 2019-09-17T19:57:31.800

3"First of the trivial comment-abuse answers" Redeemed by the TIO links! – Jonah – 2019-09-18T22:06:42.287

14

JavaScript, 11 bytes

n=>n//n->=n

Try it Online! | Reversed

Shaggy

Posted 2019-09-17T19:32:25.203

Reputation: 24 623

12

x86 machine code, 3 bytes

C3 D8 F7

The above bytes of code define a function that is a no-op: it simply returns control to the caller. That function is followed by two garbage bytes that will not be executed, since they come after a return—they are in "no man's land". In assembler mnemonics:

ret                     ; C3    
fdiv  st(0), st(7)      ; D8 F7

Okay, so now some troll comes by and reverses the order of the bytes:

F7 D8 C3

These bytes now define a function that takes an integer argument in the EAX register, negates it, and returns control to the caller. In assembler mnemonics:

neg  eax     ; F7 D8
ret          ; C3

So…that was simple. :-)

Note that we can make the "negation" instruction be anything we want, since it is never executed in the "forward" orientation and only executed in the "reversed" orientation. Therefore, we can follow the same pattern to do arbitrarily more complicated stuff. For example, here we take an integer argument in a different register (say, EDI, to follow the System V calling convention commonly used on *nix systems), negate it, and return it in the conventional EAX register:

C3      ret
D8 F7   fdiv  st(0), st(7)      ;  \ garbage bytes that
F8      clc                     ;  | never get executed,
89      .byte 0x89              ;  / so nobody cares

  ↓ ↓

89 F8   mov  eax, edi
F7 D8   neg  eax
C3      ret

Cody Gray

Posted 2019-09-17T19:32:25.203

Reputation: 2 639

11

Jelly, 2 bytes

oN

Try it online! and its reverse.

oN - (input) OR ((input) negated)

No - ((input) negated) OR (input)

Jonathan Allan

Posted 2019-09-17T19:32:25.203

Reputation: 67 804

Heh, you can also do ḷN, no need for the OR logic. :D – Erik the Outgolfer – 2019-09-17T21:54:46.917

10Na, words give aN aesthetic effect :) – Jonathan Allan – 2019-09-17T22:05:16.510

8

Haskell, 8 bytes

Anonymous identity function, turning into subtraction from 0 when reversed.

id--)-0(

Try it online!

Reversed:

(0-)--di

Try it online!

Ørjan Johansen

Posted 2019-09-17T19:32:25.203

Reputation: 6 914

3You’ve managed to break the code highlighting with your reversed version! – Tim – 2019-09-19T09:57:50.120

@Tim Curious. Testing suggests it fails when a comment starts right after a right parenthesis. – Ørjan Johansen – 2019-09-20T02:07:15.547

8

Whitespace, 48 bytes

S S S N
S N
S T N
T   T   T   T   T   T   N
S T N
N
N
T   S N
T   N
S S T   N
T   T   S S T   T   T   T   T   N
T   S N
S N
S S S 

Letters S (space), T (tab), and N (new-line) added as highlighting only.

Minor modification of my Whitespace answer for the I reverse the source code, you negate the output! challenge.

Try it online or try it online reversed (with raw spaces, tabs and new-lines only).

Explanation:

Utilizing the Exit Program builtin being a short palindrome NNN.
The regular program will:

SSSN   # Push 0 to the stack
SNS    # Duplicate it
TNTT   # Read STDIN as integer, and store it at heap address 0
TTT    # Retrieve the input from heap address 0, and push it to the stack
TNST   # Pop and print the top of the stack as number
NNN    # Exit the program, making everything after it no-ops

The reverse program will:

SSSN   # Push 0 to the stack
SNS    # Duplicate it
TNTT   # Read STDIN as integer, and store it at heap address 0
TTT    # Retrieve the input from heap address 0, and push it to the stack
SSTTN  # Push -1 to the stack
TSSN   # Multiply the top two values on the stack together
TNST   # Pop and print the top of the stack as number
NNN    # Exit the program, making everything after it no-ops

Small additional explanation of pushing a number:

  • First S: Enable Stack Manipulation
  • Second S: Push a number to the stack
  • S or T: Positive/negative respectively
  • Some S/T followed by a trailing N: number in binary, where S=0 and T=1

I.e. SSTTSTSN pushes -10. For the 0 we don't need an explicit S=0, so simply SSSN or SSTN is enough.

Kevin Cruijssen

Posted 2019-09-17T19:32:25.203

Reputation: 67 575

6

C (clang), 23 bytes

f(*x){}//};x*-=x*{)x*(g

Try it online!

AZTECCO

Posted 2019-09-17T19:32:25.203

Reputation: 2 441

6

R, 23 bytes

I decided to give it a go without the comment trick.

Forward

`+`=scan;""+-0;nacs=`+`

Try it online!

Reverse

`+`=scan;0-+"";nacs=`+`

Try it online!

In the forward version + is acting a binary operator, and - is a unary operator.

In the reverse the + becomes unary and the - is binary. So scan function takes the arguments: file="" which means stdin and what=0, which are also defaults. So when the + is unary the first argument is on the right, when it is binary the first argument is on the left.

The

;nacs=`+`

part of the code does nothing really useful, so in a sense my code is not really very much more valid than using the comment trick.

Aaron Hayman

Posted 2019-09-17T19:32:25.203

Reputation: 481

2This is very smart (+1). We often redefine the R operators to golf bytes, but I think this is the first time I have seen + redefined to be used as both unary and binary. It took me a minute to understand how this was parsed… No other operator name would have done the job. – Robin Ryder – 2019-09-19T15:45:53.180

5

Labyrinth / Hexagony, 6 bytes

Labyrinth:

?!@!`?

Try it online! and its reverse.

Hexagony:

?!@!~?

Try it online! and its reverse.

How?

?       - take a signed integer
(` / ~) - negate
!       - output top-of-stack / current-memory-edge
@       - exit

Jonathan Allan

Posted 2019-09-17T19:32:25.203

Reputation: 67 804

5

Brain-Flak, 7 bytes

#)]}{[(

Try it online!

Reversed:

([{}])#

Try it online!

Note: Only works in interpreters that support comments (e.g. works in Rain-Flak, but not in BrainHack)


If we also swap opening/closing brackets instead of just reversing the bytes we can do this in 8 bytes without using comments:

({}[{}])

Try it online!
Try it reversed!

Riley

Posted 2019-09-17T19:32:25.203

Reputation: 11 345

Is this undefined behaviour abuse? I don't think Brain-Flak specification allows such parenthesis. – HighlyRadioactive – 2019-09-18T14:06:07.023

@TwilightSparkle The # starts a comment, so the parenthesis in the original version are ignored. – Riley – 2019-09-18T14:08:20.803

Oh yeah, I forgot! But it only works in Rain-Flak then(It's the official intepreter though). You will probably need to mention it? – HighlyRadioactive – 2019-09-18T14:12:49.380

@TwilightSparkle added a note for clarification. Thanks. – Riley – 2019-09-18T14:21:40.487

Fun little challenge: Can you do this without comments if you also swap opening/closing brackets instead of just reversing? – James – 2019-09-18T21:42:57.307

@DJMcMayhem My first idea came out to 10 bytes: ({}<>[{}])

– Riley – 2019-09-18T21:46:48.087

I came up with nearly the same thing: ({}[{}]) – James – 2019-09-18T21:51:37.050

Yep. Just realized the <> doesn't do anything – Riley – 2019-09-18T21:52:02.163

5

Perl 6 / Raku, 3 bytes

*-0

Try it online!

Creates a Whatever code block. Read in normally its standard block equivalent is -> \x {x - 0}, but in reverse it becomes -> \x {0 - x}.

user0721090601

Posted 2019-09-17T19:32:25.203

Reputation: 928

5

Python 3, 22 bytes

lambda x:x#x-:x adbmal

Try it online!

A lambda which implements the identity function (or negation)

Nick Kennedy

Posted 2019-09-17T19:32:25.203

Reputation: 11 829

5

MarioLANG, 22 bytes

;:
=#
:)!
--
<(
"
[>
;

Try it online!

He just inputs and outputs the number before he falls to EOF

reversed:

;
>[
"
(<
--
!):
#=
:;

Try it online!

He loops until the input value is 0 and the output value is -input, the he says the number.

Dorian

Posted 2019-09-17T19:32:25.203

Reputation: 1 521

4

Gaia, 2 bytes

_@

Try it online!

_	| implicit push input and negate
 @	| push next input OR push last input (when all inputs have been pushed)
	| implicit print TOS

Reversed:

@	| push input
 _	| negate
	| implicit print TOS

Giuseppe

Posted 2019-09-17T19:32:25.203

Reputation: 21 077

4

Perl 5 (-p), 7 6 bytes

-1 thanks to @primo

$_*=$#

TIO

A comment doesn't change input

#1-=*_$

Negate the input

$_*=-1#

TIO

Nahuel Fouilleul

Posted 2019-09-17T19:32:25.203

Reputation: 5 582

-1: $_*=$# TIO. Note that the # must be the very last byte of the program, otherwise it will be interpreted as the variable $#, rather than the last index of the array with name <empty>.

– primo – 2019-09-18T16:32:37.713

1however i don't understand how it works because trying to print $# gives either an error (if # is not the last character) or nothing – Nahuel Fouilleul – 2019-09-18T18:08:44.497

Seems to only work with -p or -n. I suspect the boilerplate has something to do with it... – primo – 2019-09-19T11:26:40.727

2

@primo It works because -p/-n adds a ; after the code. Which means that $# is actually $#;: the size of the array @;. If the size of @; changes, the result isn't correct anymore (TIO). Anyway, this is super clever, well done! :)

– Dada – 2019-09-20T15:31:20.203

that's the explanation could be seen with perl -MO=Deparse -p <(echo -n '$_*=$#'), because it seems perl -MO=Deparse -pe '$_*=$#' adds a newline – Nahuel Fouilleul – 2019-09-21T06:18:10.653

4

R, 14 bytes

scan()#)(nacs-

Try it online!

A full program that reads a number, or reads and negates a number. The reverse functionality is protected by an inline comment

Nick Kennedy

Posted 2019-09-17T19:32:25.203

Reputation: 11 829

Your brackets are the wrong way around in the commented part. – Aaron Hayman – 2019-09-18T22:17:18.110

@AaronHayman thanks! – Nick Kennedy – 2019-09-18T22:29:12.777

4

Backhand, 6 5 bytes

I@-Ov

Try it online! Try it doubled!

Made a little complex due to the nature of the pointer in Backhand. I don't think it's possible to get any shorter haha, turns out I was wrong. This duplicates no instruction and reuses both the input, output and terminate commands between the two programs. Now I think it is optimal, since you need all of the IO-@ commands to work, and in a 4 byte program you can only execute two of those commands.

Explanation:

The pointer in Backhand moves at three cells a tick and bounces off the boundaries of the cell, which means the general logic is overlapping. However you can manipulate this speed with the v and ^ commands.

The original program executes the instructions IO-@, which is input as number, output as number, subtract, terminate. Obviously the subtract is superfluous. In the code these are:

I@-Ov
^  ^    Reflect
  ^     Reflect again
 ^

The reversed program executes v-I-vO-@. The v reduces the pointer steps between ticks, and the - subtracts from the bottom of the stack, which is implicitly zero. The extra - commands do nothing. The program executes like

vO-@I
v       Reduce pointer speed to 2
  -     Subtract zero from zero
    I   Get input as number and reflect off boundary
  -     Subtract input from zero
v       Reduce pointer speed to 1
 O      Output as number
  -     Subtract zero from zero
   @    Terminate

Jo King

Posted 2019-09-17T19:32:25.203

Reputation: 38 234

4

Haskell, 12 bytes

f=id;x-0=x f

Try it online! Reverse:

f x=0-x;di=f

Try it online!

Not as short as Ørjan Johansen's answer, but without comments.

nimi

Posted 2019-09-17T19:32:25.203

Reputation: 34 639

3

APL (Dyalog Unicode), 13 3 bytes

-∘0

Try it online!

Trivial answer. Returns arg or ¯arg.

Saved 10 bytes by not being dumb (thanks Adám).

Altered the resulting 3-byter to a more fitting function.

J. Sallé

Posted 2019-09-17T19:32:25.203

Reputation: 3 233

Whoa, this can be done trivially in three bytes! – Adám – 2019-09-17T20:31:54.963

Interestingly, you already have a 3-byte answer embedded as a substring of this. – Adám – 2019-09-18T09:01:58.447

@Adám yeah, I knew there was a simple answer in there somewhere. Thanks. – J. Sallé – 2019-09-18T12:54:06.463

3

Wolfram Language (Mathematica), 9 bytes

1&0+#-0&1

Try it online!

Forward: read ((1)&*0+#-0)&*1=#&

Backward: read ((1)&*0-#+0)&*1=-#&

attinat

Posted 2019-09-17T19:32:25.203

Reputation: 3 495

3

Excel VBA, 12

?[A1]']1A[-?

Reversed:

?-[A1]']1A[?

Input is cell A1 of the ActiveSheet. Comments still work in the Immediate Window :)

Chronocidal

Posted 2019-09-17T19:32:25.203

Reputation: 571

1I get what you are going for here, but I am pretty sure that you meant to answer ?[A1]']1A[-? – Taylor Scott – 2020-01-03T12:53:16.937

@TaylorScott A good catch, oops – Chronocidal – 2020-01-03T15:52:53.123

3

Python 3, 22 14 bytes

int#__bus__. 0

Try it online!

Uses the int class's constructor and a built-in pseudo-private method.

mypetlion

Posted 2019-09-17T19:32:25.203

Reputation: 702

Huh. Why is the space before the attribute mandatory? – Jo King – 2019-09-19T01:25:38.037

2@JoKing 0. would be interpreted as a number, which is followed by a symbol – attinat – 2019-09-19T08:22:33.157

3

Batch, 34 bytes

@ECHO.%1 2>MER@
@REM>2 1%=-aa/TES@

Echoes (ECHO.) the input (%1). The rest of the first line technically redirects STDERR to a file called MER@, but this isn't impactful.
Second line is commented out (REM...).

Reversed

@SET/aa-=%1 2>MER@
@REM>2 1%.OHCE@

Uses the arithmetic mode of the set command (SET /a) to subtract (-=) the input (%1) from an undefined variable (a) which is equivalent to 0 - input. Again, the rest of the first line technically redirects STDERR to a file called MER@, but this isn't impactful.
Second line is commented out (REM...).

Οurous

Posted 2019-09-17T19:32:25.203

Reputation: 7 916

This looks interesting. Care to explain? – Adám – 2019-09-25T05:34:38.120

@Adám Added an explanation, and also realised that I had the programs around backwards. – Οurous – 2019-09-25T06:34:28.707

2

Triangular, 8 7 bytes

$%%.|.$

Try it online!

Ungolfed:

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

Reversed:

$.|.%%$

Try it online!

Previous Version (8 bytes):

$.%..|.$

Reinstate Monica

Posted 2019-09-17T19:32:25.203

Reputation: 1 382

2

05AB1E, 2 bytes

(I

Try it online!

Reversed

(    negates nothing
  I  pushes input

I    pushes input
  (  negates input

jasanborn

Posted 2019-09-17T19:32:25.203

Reputation: 21

1It is supposed to only negate the input in one direction, leaving it as-is in the other. Clearly, 1-character solution cannot be valid. – Adám – 2019-09-17T19:49:28.053

My bad, I misunderstood – jasanborn – 2019-09-17T19:52:12.963

2

ovs

Posted 2019-09-17T19:32:25.203

Reputation: 21 408

2

Klein, 2 bytes

Works in all 12 topologies!

@-

Try it online!

Reverse

-@

Try it online!

- negates the input and @ ends the program

Post Rock Garf Hunter

Posted 2019-09-17T19:32:25.203

Reputation: 55 382

3Oh come on. This question seems to be made for some clever flipping on the Möbius strip or so, and you submit an answer that doesn't depend on the topology? Meh. – ceased to turn counterclockwis – 2019-09-18T09:39:00.340

Ah, I hadn't seen that: https://codegolf.stackexchange.com/a/192983/2183

– ceased to turn counterclockwis – 2019-09-18T11:11:04.730

2

Turing Machine Language, 39 bytes

The Positive

1 r - _ 0
0 l * * 0
0 - _ l 0
0 _ _ r 0

The Negative

0 r _ _ 0
0 l _ - 0
0 * * l 0
0 _ - r 1

This one was a bit trickier than I thought, mostly because I had to get past my prejudices of having code that runs with 'compile' errors.

ouflak

Posted 2019-09-17T19:32:25.203

Reputation: 925

2

><>, 5 4 bytes

n-r0

uses stack initialisation with the -v option, put your input variable there.

Try it online!

Or try the reversal

Explanation

n       Prints whatever is on the stack as a number
 -      Subtract the top 2 elements on the stack.
        There aren't 2 elements, so it crashes.
  r0    Never gets executed

or reversed:

0       Push a 0 onto the stack
 r      reverse the stack (now 0, -v)
  -     Subtract top 2 elements and push result (0-v, ie negated)
   n    Print as number
        The code wraps around and executes again. 
        It crashes on the - as there is only one
        item on the stack: 0.

steenbergh

Posted 2019-09-17T19:32:25.203

Reputation: 7 772

2

Stack Cats -mn, 2 bytes

-X

Try it online!

Try the reverse!

Explanation

Turns out this is actually a lot easier than the previous challenge in Stack Cats. The full program (after applying -m) here is -X-. X is used to swap the stacks left and right of the tape head, i.e. it doesn't affect the initial stack at all, so we can ignore it. But then the program is effectively just -- (negate the top of the stack twice), which does nothing.

For the inverse program, applying -m gives X-X. Again, X does nothing, so the program is effectively just -, which negates the top of the stack.

The only other 2-byte solution is -=, but it's virtually the same. The only difference is that = swaps only the tops of the adjacent stacks, not the entire stacks.

But again, using -m feels a bit like cheating, so below is a solution that uses a fully mirrored program.


Stack Cats -n, 7 bytes

:I<->I:

Try it online!

Try the reverse!

Explanation

The considerations from the previous answer still apply: any valid solution needs to use the paired characters and I. The six possible solutions (included in the TIO link) are all virtually the same. - and _ are equivalent in this program, and : can be replaced by | or T (which do the same for non-zero inputs and coincidentally also work for zero inputs). I've just picked this one to explain because it's easiest.

So remember that the initial stack holds the input on top of a -1 (on top of infinitely many zeros) whereas all the other stacks along the tape only hold zeros. Stack Cats also has the property that any even-length program does nothing (provided it terminates, but we can't use loops for this challenge anyway). The same is then obviously true for any odd-length program whose centre character does nothing... let's see:

:    Swap the input with the -1 below.
I    Move the -1 one stack to the left and turn it into +1.
<    Move another stack left (without taking the value).
-    Negate the zero on top of that stack (i.e. do nothing).

Therefore, the second half of the program exactly undoes the first half and we end up with the input on top of a -1 again.

The inverse program is :I>-<I:. Let's see how that changes things:

:    Swap the input with the -1 below.
I    Move the -1 one stack to the left and turn it into +1.
>    Move one stack right, i.e. back onto the initial stack which still holds the input.
-    Negate the input.
<    Move back to the left where we've parked the 1.
I    Move that 1 back onto the initial stack and turn it back into a -1.
:    Swap the -1 below the negated input to act as an EOF marker.

Martin Ender

Posted 2019-09-17T19:32:25.203

Reputation: 184 808

2

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

IFcoltransG

Posted 2019-09-17T19:32:25.203

Reputation: 191

2

W, 3 2 bytes

Pretty much a port of the Keg answer. Might get around to implement an implicit return.

%_

Explanation

   % Since there is nothing on-stack, return the input.
%_ % Start a comment

noitanalpxE

_  % Negate input & return
 % % Start a comment

user85052

Posted 2019-09-17T19:32:25.203

Reputation:

1

Japt, 3 bytes

TnU

Try it | Reversed

Same method as my earlier solution, with T being 0 and U being the input.

Shaggy

Posted 2019-09-17T19:32:25.203

Reputation: 24 623

1

V (vim), 5 bytes

É-ó--

Try it online!

Hexdump:

00000000: c92d f32d 2d                             .-.--

James

Posted 2019-09-17T19:32:25.203

Reputation: 54 537

1

C++ (gcc), 29 bytes

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

Try it online!

Fixed issue brought up by @Nick Kennedy

Nishioka

Posted 2019-09-17T19:32:25.203

Reputation: 181

1This is shown the wrong way round; it should return the input when called without reversing the code. – Nick Kennedy – 2019-09-18T10:51:25.840

Is #define f//- f enifed# (call as f x) cheating? ;) – Quentin – 2019-09-19T15:54:50.583

1

Stax, 2 bytes

pN

Run and debug it

Oliver

Posted 2019-09-17T19:32:25.203

Reputation: 7 160

1

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).

Neil

Posted 2019-09-17T19:32:25.203

Reputation: 95 035

I don't think you need the ^ at the end. Also, Retina 1 would save a byte on $*. – Martin Ender – 2019-09-21T10:15:24.203

1

Ruby -p , 7+1 = 8 bytes

#-?<<>$

Try it online!

Reverse:

Try it online!

G B

Posted 2019-09-17T19:32:25.203

Reputation: 11 099

1

PHP, 20 bytes

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

Try it online!

Night2

Posted 2019-09-17T19:32:25.203

Reputation: 5 484

1

MathGolf, 4 bytes

*b╘k

Try it online or try it online reversed.

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).

Kevin Cruijssen

Posted 2019-09-17T19:32:25.203

Reputation: 67 575

1

Oliver

Posted 2019-09-17T19:32:25.203

Reputation: 7 160

1

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.

Soapy

Posted 2019-09-17T19:32:25.203

Reputation: 251

1Shouldn't this be _//_-? However, _-0 seems like a valid solution. – Adám – 2019-09-18T18:45:57.583

@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 – 2019-09-19T08:43:45.097

1

Gol><>, 4 bytes

Ih-I

One byte golfed off courtesy of JoKing

Try it online!

KrystosTheOverlord

Posted 2019-09-17T19:32:25.203

Reputation: 681

@JoKing Thank you very much, is that a negate operation? I honestly forgot that there was one – KrystosTheOverlord – 2019-09-18T23:43:51.017

@JoKing Ohhhhh, I didn't know that, thank you very much for enlightening me on that operation – KrystosTheOverlord – 2019-09-19T00:16:25.583

1

Runic Enchantments, 4 bytes

i@Zi

Try it online! Try it reversed!

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

Draco18s no longer trusts SE

Posted 2019-09-17T19:32:25.203

Reputation: 3 053

1

Perl 5 - 12 Bytes

say$_#_$-yas

Reversed

say-$_#_$yas

Abides by the -0 rule.

Does require the -nE flags on execution.

booshlinux

Posted 2019-09-17T19:32:25.203

Reputation: 81

1

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.

DLosc

Posted 2019-09-17T19:32:25.203

Reputation: 21 213

@JoKing That's clever! It's really different from mine; do you want to post it yourself? – DLosc – 2019-09-24T17:00:13.493

Posted – Jo King – 2019-09-24T21:54:49.220

1

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.

Jo King

Posted 2019-09-17T19:32:25.203

Reputation: 38 234

1

Keg, 2 bytes

Try it online! !enilno ti yrT

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

Lyxal

Posted 2019-09-17T19:32:25.203

Reputation: 5 253

1

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

Tau

Posted 2019-09-17T19:32:25.203

Reputation: 1 935

0

Nitrodon

Posted 2019-09-17T19:32:25.203

Reputation: 9 181

0

Lua, 23 bytes

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

Try it online!

Comment abuse all over again.

val says Reinstate Monica

Posted 2019-09-17T19:32:25.203

Reputation: 409

0

Cascade, 6 bytes

# -
&#

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.

Jo King

Posted 2019-09-17T19:32:25.203

Reputation: 38 234