Twice as long, half the price

7

Related: I double the source, you double the output!


When Steve Jobs launched the iPhone 3G, the slogan said

Twice as fast, half the price

I changed the word fast to long to better represent this challenge.

Objective

Write a program that outputs a non-zero, even integer. If the source code is doubled, the value of the output must be cut off by half.

To double the source in a Unix shell (with Core Utilites):

cat source.txt source.txt > doubled-source.txt

Rules & Requirements

Now the requirement has changed and the task has been even more challenging!

  • You must build a full program.
  • The initial source is no longer required to be at least 1 byte long. An empty program qualifies as long as the output is correct.
  • Both the integers must be in base 10 (outputting them in any other base or with scientific notation is forbidden), and must not be zero. The integer is no longer required to be positive, so if you like a negative number, it's OK.
  • Your program must not take input (or have an unused, empty input) and must not throw any error (compiler warnings are not considered errors). Exit code must be zero.
  • Outputting the integers with trailing / leading spaces / newlines is allowed, as long as only one integer is output.
  • You can't assume a newline between copies of your source. If you need one, clarify it. See the Unix shell command above.
  • Standard loopholes are forbidden.

Because this is a , the shortest (original, undoubled) code in each language wins!

Extras

Please give description / explanation to your code if possible.

iBug

Posted 2017-08-29T03:59:10.090

Reputation: 2 477

Question was closed 2017-08-29T06:45:18.937

Must the old and new program be in the same language? – tsh – 2017-08-29T04:03:12.323

@tsh Definitely – iBug – 2017-08-29T04:10:55.520

2By the way, zero byte answers to this question can't exist. – Joshua – 2017-08-29T04:20:42.450

1@WheatWizard This is exactly the opposite. – Arthur – 2017-08-29T06:42:49.017

@Arthur That is somewhat true but also misleading. The only difference between this question and that is the operation involved. I consider this a rather minor difference that doesn't add anything not already present in the original. – Post Rock Garf Hunter – 2017-08-29T06:45:48.810

Answers

5

Brain-Flak, 12 bytes

(()()[][{}])

Outputs 2. Try it online!

The doubled program outputs 1. Try it online!

Explanation

This code pushes 2 + stack height - top of stack. Initially, this is 2 + 0 - (implicit) 0 = 2. When this code is run again, it pops the existing 2 and pushes 2 + 1 - 2 = 1.

Nitrodon

Posted 2017-08-29T03:59:10.090

Reputation: 9 181

This is in fact a provably optimal solution. I don't have the space for the complete proof in this comment but it's not very complex, feel free to ping me in chat for a complete proof. – Post Rock Garf Hunter – 2017-08-29T06:56:29.740

5

Python 2, 35 34 bytes

Uses a concept similar to that of Wheat Wizard in the double challenge with an improvement I found to his original code. Here I use the fact that the length of the file is 34 bytes long. Meaning it is equal to 4 modulo 6. (34%6 = 4). The double source code would have a length of 68. And 68%6 = 2, which is exactly what we want.

print open(__file__,"a").tell()%6#

Try it online!

Double source code

Explanation

This opens the source code file in append mode

open(__file__,"a")

We then find the current position in the file, this will be at the end of the file due to opening in append mode

open(__file__,"a").tell()

We print this length modulo 6

print open(__file__,"a").tell()%6

And add a comment, so that doubling the source code does not execute more code

print open(__file__,"a").tell()%6#

Halvard Hummel

Posted 2017-08-29T03:59:10.090

Reputation: 3 131

4

Jelly, 2 bytes

¬‘

Outputs 2, Try it online!

While

¬‘¬‘

Outputs 1, Try it online!

How?

The default input to a monadic chain is zero...

¬‘   - Main link: no arguments
¬    - logical not -- not the implicit input of 0 to yield 1
 ‘   - increment   -- increment 1 to yield 2
     - implicit print if this is the end of the program, otherwise...
     ...
  ¬‘ - continues the monadic chain:
  ¬  - logical not -- not the 2 to yield 0
   ‘ - increment   -- increment 0 to yield 1
     - implicit print if this is the end of the program

The two byters:

Ị‘ - insignificant (abs(z)<=1); increment
Ṇ‘ - non-vectorising logical not; increment

would work too.

Jonathan Allan

Posted 2017-08-29T03:59:10.090

Reputation: 67 804

2

Ly, 7 bytes

2y1-+-N

Try it online!

Simple port of the Brain-Flak answer.

LyricLy

Posted 2017-08-29T03:59:10.090

Reputation: 3 313

1

05AB1E, 3 bytes

Outputs 4

6rÍ

Try it online!

Doubled Source

Outputs 2

6rÍ6rÍ

Try it online!

Explanation

6      # push 6
 r     # reverse the stack (does nothing)
  Í    # subtract 2

With the doubled source we continue with

6      # push 6
 r     # reverse the stack (puts 4 on top)
  Í    # subtract 2

Emigna

Posted 2017-08-29T03:59:10.090

Reputation: 50 798

Does the r in the singlet form of your answer put 4 on top the top of the stack? – Taylor Scott – 2017-09-10T19:25:41.573

@TaylorScott: In the single form we only have a 6 on the stack so r does nothing. – Emigna – 2017-09-11T05:12:44.677

0

sh, 50 bytes

#!/bin/sh
expr 200 / `wc -c $0 | cut -c 1-3`
exit

Joshua

Posted 2017-08-29T03:59:10.090

Reputation: 3 043

0

Jelly, 7 bytes

®‘!İ©Ḥµ

Try it online!

Outputs 2.

Duplicated Source:

®‘!İ©Ḥµ®‘!İ©Ḥµ

Try it online!

Outputs 1.

fireflame241

Posted 2017-08-29T03:59:10.090

Reputation: 7 021