8
\$\begingroup\$

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.

\$\endgroup\$
5
  • \$\begingroup\$ Must the old and new program be in the same language? \$\endgroup\$
    – tsh
    Aug 29, 2017 at 4:03
  • \$\begingroup\$ @tsh Definitely \$\endgroup\$
    – iBug
    Aug 29, 2017 at 4:10
  • 2
    \$\begingroup\$ By the way, zero byte answers to this question can't exist. \$\endgroup\$
    – Joshua
    Aug 29, 2017 at 4:20
  • 1
    \$\begingroup\$ @WheatWizard This is exactly the opposite. \$\endgroup\$
    – Arthur
    Aug 29, 2017 at 6:42
  • \$\begingroup\$ @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. \$\endgroup\$
    – Wheat Wizard
    Aug 29, 2017 at 6:45

7 Answers 7

5
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$
    – Wheat Wizard
    Aug 29, 2017 at 6:56
5
\$\begingroup\$

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#
\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

Ly, 7 bytes

2y1-+-N

Try it online!

Simple port of the Brain-Flak answer.

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ Does the r in the singlet form of your answer put 4 on top the top of the stack? \$\endgroup\$ Sep 10, 2017 at 19:25
  • \$\begingroup\$ @TaylorScott: In the single form we only have a 6 on the stack so r does nothing. \$\endgroup\$
    – Emigna
    Sep 11, 2017 at 5:12
0
\$\begingroup\$

Jelly, 7 bytes

®‘!İ©Ḥµ

Try it online!

Outputs 2.

Duplicated Source:

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

Try it online!

Outputs 1.

\$\endgroup\$
0
\$\begingroup\$

sh, 50 bytes

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.