Third time the charm

49

2

Your task, if you wish to accept it, is to write a program that outputs a positive integer (higher than 0). If the source code is duplicated the output must remain the same. The tricky part is that if the source code is typed three times (triplicated?) the output will be multiplied by 3.

Rules

  • You must build a full program. That is, your output has to be printed to STDOUT.

  • The initial source must be at least 1 byte long.

  • Both the integers must be in base 10 (outputting them in any other base or with scientific notation is forbidden).

  • Your program must not take input (or have an unused, empty input).

  • Outputting the integers with trailing / leading spaces is allowed.

  • Leading Zeroes are allowed only if the numbers of digits is consistent eg: 001 - 001 - 003 or 004 - 004 - 012

  • You may not assume a newline between copies of your source.

  • This is , so the shortest (original) code in each language wins!

  • Default Loopholes apply.

Example

Let's say your source code is Abc and its corresponding output is 4. If I write AbcAbc instead and run it, the output must still be 4. However if I write AbcAbcAbc and run it, the output must be 12.


Shamelessly stolen Derived from Mr. Xcoder's challenge

workoverflow

Posted 2018-03-12T13:24:54.130

Reputation: 1 025

Is our code allowed to read its own source code? – AdmBorkBork – 2018-03-12T13:45:27.523

@AdmBorkBork I'd assume so, since this isn't tagged as [tag:quine]. – Erik the Outgolfer – 2018-03-12T13:47:58.513

@AdmBorkBork Yes. – workoverflow – 2018-03-12T14:06:45.457

9I don't think the 1 byte restriction is needed since it wouldn't be possible to tell the difference between nothing and nothing repeated 3 times. – 12Me21 – 2018-03-13T01:42:58.823

May we print blank lines before the output in the multiplied cases? – xnor – 2018-03-13T07:02:47.707

Are we allowed leading zeroes? – Jo King – 2018-03-13T21:01:35.697

@xnor No blank lines before the output. – workoverflow – 2018-03-14T08:38:51.750

@JoKing Only if the numbers of digits is consistent eg: 001 - 001 - 003 or 004 - 004 - 012 – workoverflow – 2018-03-14T08:40:22.040

What does "the source code is duplicated" mean? – r12 – 2018-03-15T06:20:05.173

@r12 "Let's say your source code is Abc and its corresponding output is 4. If I write AbcAbc instead and run it, the output must still be 4. However if I write AbcAbcAbc and run it, the output must be 12." – workoverflow – 2018-03-15T08:46:57.933

Thank you for trying to explain it to me. I read that text several times and wasn't able to understand it. At the time, I didn't know what "Abc" was when I read the post and kept rereading it. I scrolled up and down through this post and thought "Abc" was one of these weird programming languages with unusual symbols doing unusual things. Very little on this page makes any sense to me. – r12 – 2018-03-15T19:30:27.177

1@r12 "Abc" is an example for any programming language program, say if your code is (int i=1;print i;) then the duplicated code of (int i=1;print i;int i=1;print i;) must output the same number as the original code, and when the code is triplicated to (int i=1;print i;int i=1;print i;int i=1;print i;) it must show the number multiplied by 3 – workoverflow – 2018-03-16T16:54:07.017

@r12 You should not expect to be able to understand something without learning it first. Especially on PPCG. – user202729 – 2018-03-19T09:55:06.200

Can the program print leading zero? – user202729 – 2018-03-19T10:10:06.940

@user202729 Only if the numbers of digits is consistent eg: 001 - 001 - 003 or 004 - 004 - 012 – workoverflow – 2018-03-19T21:36:11.317

I suggest adding the rule to the question instead of in the comment. / What about 4-4-12 or 04-04-12? – user202729 – 2018-03-20T01:17:40.263

Answers

21

Wumpus, 6 bytes

{~)
@O

Try it online!
Try it doubled!
Try it tripled!

Prints 1 and 3.

Explanation

I found a ton of 6-byte solutions by brute force search, but none for 5 bytes. That doesn't necessarily mean there aren't any at 5 bytes but they'd probably use weird characters or something.

I ended up picking this solution because it doesn't print any leading zeros (most of them do) and it has some interesting control flow. Let's start with the single program:

enter image description here

So the executed code is:

{~)O@

{   Turn the IP left by 60°.
~   Swap two implicit zeros on the stack, does nothing.
)   Increment the top zero to 1.
O   Print it.
@   Terminate the program.

Easy enough. Now the doubled program. Since the first line gets appended onto the second line, the grid extends to width 5 (and height 3) which changes the control flow significantly:

enter image description here

The IP goes around that loop exactly once, so the executed code is:

{~){~)){~O@

{~) As before, we end up with a 1 on top of the stack.
{   Turn left by 60° again.
~   Swap the 1 with the 0 underneath.
))  Increment the zero to 2.
{   Turn left by 60° again.
~   Swap the 2 with the 1 underneath.
O   Print the 1.
@   Terminate the program.

Finally, the tripled program is quite similar to the doubled one, but we get a couple more important commands onto that third line:

enter image description here

So the executed code is:

{~){~)){~~)O@

{~){~)){~
    As before. We end up with a 1 on top of the stack and a 2 underneath.
~   Swap the 1 with the 2 underneath.
)   Increment the 2 to a 3.
O   Print the 3.
@   Terminate the program.

Martin Ender

Posted 2018-03-12T13:24:54.130

Reputation: 184 808

1i think i'm in love with this language. – conquistador – 2018-03-14T13:54:01.433

11

Husk, 5 bytes

KΣK+1

Try it online!

Repeated twice!

Repeated thrice!

Explanation

It's quite difficult to construct a repeatable program in Husk. Because the type system forbids a function that can be applied to itself, I have to somehow allow the first part to evaluate to a function, and the remainder to evaluate to a value, and the types of existing built-ins are designed to prevent this kind of ambiguity. The tokens of the program are

  • K, which constructs a constant function. K a b is equivalent to a.
  • Σ, which takes an integer n and returns the nth triangular number.
  • +, which adds two numbers.
  • 1, which is the literal 1.

The original program is interpreted like this:

   K Σ (K+) 1
== Σ 1
== 1

The (K+) is a nonsensical function that gets eaten by the first K.

The twice repeated program is interpreted like this:

   K Σ (K+1KΣK+) 1
== Σ 1
== 1

The function in parentheses is again eaten by the first K.

The thrice repeated program is interpreted like this:

   K (Σ (K (+1) (KΣK+) 1)) (KΣK+1)
== Σ (K (+1) (KΣK+) 1)
== Σ ((+1) 1)
== Σ (+1 1)
== Σ 2
== 3

Zgarb

Posted 2018-03-12T13:24:54.130

Reputation: 39 083

This takes really long time to run ... – Weijun Zhou – 2018-03-12T17:03:25.690

@WeijunZhou If you have many occurrences of K in a program, type inference becomes very slow, because each of them could potentially "eat" any number of tokens and the interpreter will try all combinations... – Zgarb – 2018-03-12T17:09:26.520

I see, thank you for your explanation. – Weijun Zhou – 2018-03-12T17:14:55.293

2@Zgarb +1 for kek +1 – workoverflow – 2018-03-14T08:54:37.467

10

Jelly, 7 5 bytes

»‘µ*Ḃ

Try it online!

Try it doubled!

Try it tripled!

How it works

»‘µ*Ḃ            Main link. No arguments. Implicit argument: x = 0

 ‘               Increment; yield x + 1 = 1.
»                Take the maximum of x and 1. Yields 1.
  µ              Begin a new, monadic chain. Argument: y = 1
    Ḃ            Bit; yield 1 if y is odd, 0 if it is even. Yields 1.
   *             Power; yield y**1 = 1.
»‘µ*Ḃ»‘µ*Ḃ       Main link.

»‘µ*Ḃ            As before.
      ‘          Increment; yield y + 1 = 2.
     »           Take the maximum of 1 and 2. Yields 2.
       µ         Begin a new, monadic chain. Argument: z = 2
         Ḃ       Bit; yield 1 if z is odd, 0 if it is even. Yields 0.
        *        Power; yield z**0 = 1.
»‘µ*Ḃ»‘µ*Ḃ»‘µ*Ḃ  Main link.

»‘µ*Ḃ»‘µ*Ḃ       As before.
           ‘     Increment; yield z + 1 = 3.
          »      Take the maximum of 1 and 3. Yields 3.
            µ    Begin a new, monadic chain. Argument: w = 3
              Ḃ  Bit; yield 1 if w is odd, 0 if it is even. Yields 1.
             *   Power; yield w**1 = 3.

Dennis

Posted 2018-03-12T13:24:54.130

Reputation: 196 637

10

Haskell, 24 bytes

main=print.div 3$4
 -1--

Prints 1: Try it online!

main=print.div 3$4
 -1--main=print.div 3$4
 -1--

Also prints 1: Try it online!

main=print.div 3$4
 -1--main=print.div 3$4
 -1--main=print.div 3$4
 -1--

Prints 3: Try it online!

Laikoni

Posted 2018-03-12T13:24:54.130

Reputation: 23 676

10

Cubix, 5 bytes

)<@OP

Try it online: once, twice, thrice.


Explanation

Cubix is a stack-based language whose instructions are wrapped around the outside of a cube. Important to note is that the stack is initially filled with infinite zeroes, which allows us to "pull values out of thin air" with operators rather than pushing them explicitly.

I must admit that this was found by a brute-forcer; I never would have found it on my own. In fact, @MartinEnder was the one who asked me to try brute-forcing, as he had been looking for this solution without luck. This is the only solution the brute-forcer found, and I do believe it is the one and only shortest solution in Cubix.

Single program

Watch it run!

The original program fits on a unit cube. Here's the unfolded net:

  )
< @ O P
  .

The IP (instruction pointer) starts on the leftmost face (the <) headed east. The < immediately points it west, and it wraps around to the P. P is exponentiation, and since there's nothing on the stack, the interpreter pulls out two 0s and calculates 00, which is 1 according to JavaScript. O then prints this value, and @ ends the program.

Double program

Watch it run!

)<@OP)<@OP

The 10-byte program is too long to fit onto a unit cube, and so it is expanded to a size-2 cube:

    ) <
    @ O
P ) < @ O P . .
. . . . . . . .
    . .
    . .

As before, the IP starts out at the top-left of the left-most face. This time, the very first instruction is P, which pushes a 1 as before. Next is ), which increments the top item, turning it into a 2. Then < turns the IP around, and it hits the ) again, transforming the 2 into a 3.

Here's where it gets interesting. P raises the second-from-top item to the power of the first item, which gives 03 = 0. Then the IP wraps around to the rightmost face and passes through two no-ops . before hitting another P. Here we see another quirk of Cubix: binary operators (such as P) don't remove their operands from the stack. So since the stack is now [3, 0], we calculate 30 = 1, which O outputs, and @ terminates the program.

Triple program

Watch it run!

)<@OP)<@OP)<@OP

As with the double program, the triple can fit on a size-2 cube:

    ) <
    @ O
P ) < @ O P ) <
@ O P . . . . .
    . .
    . .

This program starts out in the same way as the previous: P pushes 1, ) increments, < points the IP west, ) increments again, and P now pushes 0. The IP is then wrapped around to the < on the rightmost face, which does nothing since the IP is already pointed west.

Here is the one difference from the double program: the ) increments the 0 on top of the stack to a 1. When P performs its magic again, this time it calculates 31 = 3. O outputs and @ terminates, and we prove conclusively that the third time is indeed the charm.

ETHproductions

Posted 2018-03-12T13:24:54.130

Reputation: 47 880

1I really enjoyed your explanation. +1 – workoverflow – 2018-03-16T16:56:53.477

7

Brain-Flak, 10 bytes

<>([]{}())

Try it online!

Try it doubled!

Try it tripled!

Explanation:

#Toggle stacks
<>

#Push
(
    #Stack-height (initially 0) + 
    []

    #The TOS (initially 0) + 
    {}

    #1
    ()
)

When we run this once, it will put (0 + 0 + 1) == 1 onto the alternate stack. Ran a second time, it puts the same onto the main stack. Run a third time however, it evaluates to (1 + 1 + 1) == 3, and pushes that to the alternate stack and implicitly prints.

James

Posted 2018-03-12T13:24:54.130

Reputation: 54 537

7

SQL, 25 24 23 bytes

(-1 Byte Removed a mistyped character that was always commented out and doing nothing)
(-1 Byte Changed SELECT to PRINT as recommended by Razvan Socol)

PRINT 2/*
*2+1--*/-1
--

How it works:
In SQL, you can comment out the comment tags, like so:

/*
'Comment'--*/

vs

--/*
'Not Comment'--*/

Code on 1 line with comments excluded:
First iteration: SELECT 2-1 Output: 1
Second iteration: SELECT 2-1*2+1 Output: 1
Third iteration: SELECT 2-1*2+1*2+1 Output: 3

Chronocidal

Posted 2018-03-12T13:24:54.130

Reputation: 571

1PRINT instead of SELECT would save an extra byte. – Razvan Socol – 2018-03-24T09:07:57.287

6

SOGL V0.12, 7 5 4 bytes

ē»«I

Try it here!

Try it doubled!

Try it tripled!

Explanation:

ē»«I
ē     push counter, then increment it.
      First time running this will push 0, then 1, then 2.
             TOS on each: 0  1  2
 »    floor divide by 2   0  0  1
  «   multiply by 2       0  0  2
   I  and increment       1  1  3

dzaima

Posted 2018-03-12T13:24:54.130

Reputation: 19 048

I tried to do something along the lines of ē1|, but apparently there's no command for bitwise OR... – ETHproductions – 2018-03-13T02:43:23.093

@ETHproductions Yeah, I wanted to try something like that too, and the best I got was ē:2\+ :/ – dzaima – 2018-03-13T19:55:34.503

5

Python 2,  46 45  39 bytes

Inspired by Halvard's answer. I'm glad that my challenge inspired a new one, which I find even more interesting. Saved 6 bytes thanks to Kevin Cruijssen.

print open(__file__,"a").tell()/79*3|1#

Try it online!

Try it doubled!

Try it tripled!

How it works (outdated)

k=open(__file__,"a").tell() # Read the source code in "append" mode and get its length.
                            # Assign it to a variable k.
;print k>>(k==90)#          # Print k, with the bits shifted to the right by 1 if k
                            # is equal to 90, or without being shifted at all overwise.
                            # By shifting the bits of a number to the right by 1 (>>1),
                            # we basically halve it.

When it is doubled, the length becomes 90, but the new code is ignored thanks to the #, so k==90 evaluates to True. Booleans are subclasses of integers in Python, so k>>True is equivalent to k>>1, which is essentially k / 2 = 45. When it is tripled, the new code is again ignored, hence the new length is 135, which doesn't get shifted because k==90 evaluates to False, so k>>(k==90) ⟶ k>>(135==90) ⟶ k>>False ⟶ k>>0 ⟶ k, and k is printed as-is.


Python 2, 36 bytes

This was a suggestion by Aidan F. Pierce at 38 bytes, and I golfed it by 2 bytes. I’m not posting this as my main solution because I didn’t come up with it by myself.

0and""
True+=1
print True>3and 3or 1

Try it online! Try it doubled! Try it tripled!

Mr. Xcoder

Posted 2018-03-12T13:24:54.130

Reputation: 39 774

Thanks for the idea of using a comment - saved me 6 bytes. – AdmBorkBork – 2018-03-12T14:40:29.397

print open(__file__,"a").tell()/79*3|1# perhaps? – Kevin Cruijssen – 2018-03-12T14:48:06.890

@KevinCruijssen Great, thanks! – Mr. Xcoder – 2018-03-12T14:51:35.010

One fewer byte, no source code reading: https://tio.run/##K6gsycjPM/r/3yAxL0VJSYcrpKg0VdvWkKugKDOvRME4M00BJGJnnJpTnKpg@P8/AA

Prints with leading spaces for two and three repetitions, but that appears to be allowed.

– Aidan F. Pierce – 2018-03-13T03:28:36.113

@AidanF.Pierce Thank you so much! I golfed it a bit more and posted it as an alternate solution. – Mr. Xcoder – 2018-03-13T05:30:11.227

5

05AB1E, 6 5 bytes

.gDÈ+

Try it online! or Try it doubled! or Try it tripled!

Explanation

.g     # push length of stack
  D    # duplicate
   È   # check if even
    +  # add

Single: 0 + (0 % 2 == 0) -> 1
Double: 1 + (1 % 2 == 0) -> 1
Triple: 2 + (2 % 2 == 0) -> 3

Emigna

Posted 2018-03-12T13:24:54.130

Reputation: 50 798

Should that be % 2 in the triple explanation? – LarsW – 2018-03-12T18:01:32.290

@LarsW: Yes indeed :) – Emigna – 2018-03-12T19:42:50.503

@Emigna I think "check if even" is actually 2%_ right? – Magic Octopus Urn – 2018-03-15T11:36:33.160

2 + (2 % 2 == 0) -> 2 but 2 + !(2 % 2 == 0) -> 3 (for your explanation). – Magic Octopus Urn – 2018-03-15T11:36:50.770

@MagicOctopusUrn 2%_ is the same as È yes. Not sure what you mean about the explanation. 2+(2%2==0) = 2+(0==0) = 2+1 = 3. It probably would have been clearer if I had explanded the computations. – Emigna – 2018-03-15T11:49:48.517

2 + (2 % 2 == 0) -> 2 + 0 -> 2+0 != 3 is how I was reading it, but I guess == does mean check for equality in most languages other than 05AB1E. – Magic Octopus Urn – 2018-03-15T12:11:28.800

5

R, 37 31 28 bytes

Thanks to Giuseppe for golfing off the final 3 bytes.

length(readLines())%/%2*2+1

(with a trailing newline).

Try it once!

Try it twice!

Try it thrice!

This uses the readLines() trick from Giuseppe's answer to the 8-ball challenge, where stdin redirects to the source file. This code basically just counts up how many lines exist below the first line and outputs 1 if there are 1 or 3 lines (i.e. code is single or doubled), or 3 if there are 5 lines (i.e. code is tripled).

rturnbull

Posted 2018-03-12T13:24:54.130

Reputation: 3 689

oh, neat, +1! Wasn't sure how to do this in R. I think you need a newline after the s for this to work properly, BUT you should be able to golf it to 28 bytes by re-working some of the calculations.

– Giuseppe – 2018-03-12T21:41:30.637

@Giuseppe Thanks for pointing out the newline problem! I wasn't able to get your version to work when the code is tripled - am I missing something?

– rturnbull – 2018-03-12T21:47:25.357

oh weird I musta had a trailing newline, but if you do %/%2 it should work – Giuseppe – 2018-03-12T21:48:30.010

5

><>, 9 bytes

\5 n;
\\1

Try it online!

Try it doubled!

Try it tripled!

I found this sort of by luck, using the philosophy that "if you make the fish's path convoluted enough, eventually something will work". The original and doubled versions print a 5, and the tripled version prints 1 then 5 to make 15 = 3×5. Here are the multiplied versions, for your perusal:

\5 n;
\\1\5 n;
\\1
\5 n;
\\1\5 n;
\\1\5 n;
\\1

Not a tree

Posted 2018-03-12T13:24:54.130

Reputation: 3 106

5

Lost, 38 bytes

\\<<<<</<<<<>
2>((1+@>?!^%^
.........v

Try it online!

\\<<<<</<<<<>
2>((1+@>?!^%^
.........v\\<<<<</<<<<>
2>((1+@>?!^%^
.........v

Try it online!

\\<<<<</<<<<>
2>((1+@>?!^%^
.........v\\<<<<</<<<<>
2>((1+@>?!^%^
.........v\\<<<<</<<<<>
2>((1+@>?!^%^
.........v

Try it online!

Explanation

Lost is a very interesting language for this challenge. The usual Lost technique is to build a "trap". A trap is a section of the program designed to catch all the ips in one place so that their stacks can be cleared and they can be controlled to go in a specific direction. This makes writing programs in Lost a lot more manageable. However since the program is duplicated we need to avoid trap duplication as well. This requires us to design a new trap that works properly but when duplicated only one of the traps works. My basic idea here is the following

v<<<<>
>%?!^^

While the stack is non-empty the ? will remove a item and cause it to jump back to the begining if that item is non-zero. The key here is that when this stacks the ^^s line up

v<<<<>
>%?!^^v<<<<>
>%?!^^v<<<<>
>%?!^^v<<<<>
>%?!^^v<<<<>
>%?!^^

Meaning that no matter how you enter you will always exit in the same place.

From here we can attempt to implement the same idea from my Klein answer.

\\<<<<<v<<<<>
2>((1+@>?!^%^

The backbone of our program is the left had side which pushes a number of 2s. Each time we add a copy of the program another 2 gets added to the backbone of the program meaning an additional 2 is pushed to the stack. Once it goes off the bottom it bounces through \\> and executes the code

((1+@

This removes the first 2 stack items, adds one to the whatever is left, and exits. Once our backbone has 3 2s we will add 1 and get 3, if we have any less than 3 items we will just discard the entire stack and return 1.

Now the only problem left is that the ! in our program can cause an infinite loop. If the ip starts on ! going upwards it will jump and land right back where it was. This means we have to add another line underneath to prevent the loop.

\\<<<<</<<<<>
2>((1+@>?!^%^
.........^

This has the slight problem of putting some slashes in between our ^s in the trap. However, rather miraculously, everything works out. Our ips bounce around properly so that it doesn't make a difference.

Post Rock Garf Hunter

Posted 2018-03-12T13:24:54.130

Reputation: 55 382

127 bytes, doubled, tripled – Jo King – 2018-12-19T04:49:16.070

@joking unfortunately I'm stuck on mobile for the time being. Could you post it as an answer with an explanation? – Post Rock Garf Hunter – 2018-12-19T21:28:58.353

Posted – Jo King – 2018-12-19T22:26:52.030

4

Stax, 5 bytes

|dhH^

Run and debug online! · Doubled · Tripled

Explanation

|dhH^
|d       Push Current stack depth `d`, originally 0
         Doubled -> 1, Tripled -> 2
  hH^    Map d to 2*(floor(d/2))+1
         Implicit print

Weijun Zhou

Posted 2018-03-12T13:24:54.130

Reputation: 3 396

4

C (gcc), 95 91 85 bytes

#ifndef a
#define a-1
main(){puts(3
#include __FILE__
?"1":"3");}
#define a
#endif
a

Try it online!

l4m2

Posted 2018-03-12T13:24:54.130

Reputation: 5 985

Then I suggest that you edit the correct TIO link in. – Erik the Outgolfer – 2018-03-12T15:58:05.553

I sometimes can't connect to TIO, I don't know why – l4m2 – 2018-03-12T15:59:17.773

4

Perl 5, 18 15 13 12 11 bytes

-3 bytes thanks to nwellnhof

say 1|
+.7#

Once Try it online!

Twice Try it online!

Thrice Try it online!

Ton Hospel

Posted 2018-03-12T13:24:54.130

Reputation: 14 114

1n;$_=$a++|1;say (15 bytes) – nwellnhof – 2018-03-13T04:25:41.393

@nwellnhof Of course!. Thanks. – Ton Hospel – 2018-03-13T06:31:33.817

4

C (gcc), 107 bytes

My first submission in C (gcc). Way too long ...

i;
#ifdef c
#define c
#ifdef b
i=2;
#else
#define b
#endif
#else
#define c main(){putchar(i+49);}
#endif
c

TIO links: single, double, triple.

Weijun Zhou

Posted 2018-03-12T13:24:54.130

Reputation: 3 396

4

Japt, 8 6 5 bytes

-1 byte thanks to @ETHproductions

°U-v

Explanation:

°U-v
 U     # variable U=0                # U=0
°U     # ++U                         # U=1
  -    # minus:
   v   #   1 if U is divisible by 2  
       #     else
       #   0                         # U=1

This evaluates to 1-0 = 1

Doubled evaluates to 2-1 = 1

Tripled evaluates to 3-0 = 3

Oliver

Posted 2018-03-12T13:24:54.130

Reputation: 7 160

1Rearranging a little, I think you can do °U-v (plus newline) to save on the second U. – ETHproductions – 2018-03-13T02:27:27.293

@ETHproductions Thanks! v is perfect for this challenge :-) – Oliver – 2018-03-13T02:52:59.547

4

><>, 10 9 8 bytes

562gn|

Try it online!

Try it doubled!

Try it tripled!

I'm sure there's an 8 byte solution somewhere out there.

The unprintable at the end has ASCII value 1, and is only fetched by the get command on the third iteration. For the first two it prints 05, and then prints 15.

Jo King

Posted 2018-03-12T13:24:54.130

Reputation: 38 234

The 8 byte solution is proving elusive, but here are a couple more 9 byte ones in case they inspire you: TIO, TIO

– Not a tree – 2018-03-13T17:04:37.877

1@Notatree Got the 8 byte with an unprintable – Jo King – 2018-03-14T09:20:52.483

4

Pure Bash (no wc or other external utils), 27

trap echo\ $[a++&2|1] EXIT

Digital Trauma

Posted 2018-03-12T13:24:54.130

Reputation: 64 644

Nice! Replace EXIT with 0

– GammaFunction – 2019-09-06T19:12:47.787

3

JavaScript, 81 77 74 70 bytes

Saved 4 bytes thanks to Shaggy

var t,i=(i||[3,1,1]),a=i.pop()
clearTimeout(t)
t=setTimeout(alert,9,a)

Pretty lame JS solution. Consumes the values from the [3,1,1] array from the right (pop()). Registers a timeout to display the current value in the future. If a timeout was already registered, cancel it. Relies on the dirty nature of var, which hoists variable declarations.

Two times:

var t,i=(i||[3,1,1]),a=i.pop()
clearTimeout(t)
t=setTimeout(alert,9,a)

var t,i=(i||[3,1,1]),a=i.pop()
clearTimeout(t)
t=setTimeout(alert,9,a)

Three times:

var t,i=(i||[3,1,1]),a=i.pop()
clearTimeout(t)
t=setTimeout(alert,9,a)

var t,i=(i||[3,1,1]),a=i.pop()
clearTimeout(t)
t=setTimeout(alert,9,a)

var t,i=(i||[3,1,1]),a=i.pop()
clearTimeout(t)
t=setTimeout(alert,9,a)

Cristian Lupascu

Posted 2018-03-12T13:24:54.130

Reputation: 8 369

This needs a trailing semi-colon or newline in order to work but you can save 4 bytes by passing a as the 3rd argument of setTimeout: setTimeout(alert,9,a) – Shaggy – 2018-03-12T15:52:31.773

@Shaggy Thanks! It works fine even without an extra semi-colon. – Cristian Lupascu – 2018-03-12T21:30:25.560

3

Labyrinth, 12 11 9 bytes

:#%!@
 7

TIO (1x), TIO (2x), TIO (3x)

Herman L

Posted 2018-03-12T13:24:54.130

Reputation: 3 611

3

C (gcc), 53 52 bytes

Note the space after #endif.

n;main(){putchar(n+49);}
#if __LINE__>7
n=2;
#endif 

Try it online!

gastropner

Posted 2018-03-12T13:24:54.130

Reputation: 3 264

2

Charcoal, 12 bytes

⎚≔⁺ι¹ιI⁻ι⁼ι²

Try it online! Link is to verbose code.

Try it doubled!

Try it tripled!

Explanation

⎚             Clear
 ≔⁺ι¹ι        Assign plus(i, 1) to i
       I      Cast (and implicitly print)
         ⁻ ⁼ι² Subtract equals(i, 2) from
          ι    i

ASCII-only

Posted 2018-03-12T13:24:54.130

Reputation: 4 687

MapAssignRight(Plus, 1, i) saves you a byte, which gets you down to the same length as a port of my answer to @Mr. XCoder's challenge: PI∨›³L⊞Oυω³ – Neil – 2018-03-12T16:23:04.917

PI⊕⊗÷L⊞Oυω³ is another version of my answer, but still 11 bytes... – Neil – 2018-03-12T16:26:06.373

Found a 10-byter! – Neil – 2018-03-12T17:11:26.437

:/ I should really fix MapAssign(Incremented, i) – ASCII-only – 2018-03-12T22:29:08.110

2

PowerShell, 54 48 45 44 bytes

if(99-gt(gc $PSCOMMANDPATH|wc -c)){1;exit}3#

Try it online!

Try it doubled!

Try it tripled!

Gets its own invocation path with $PSCOMMANDPATH and performs a get-content on the file. If the character count of that file is less than 99 (checked via wc -c from coreutils), then we output 1 and exit (i.e., stop execution). That accounts for the original code and the doubled code. Otherwise we output 3 and exit. The actual code that's in the doubled or tripled sections is meaningless, since either we'll exit before we get to it, or it's behind a comment #.

Saved 6 bytes thanks to Mr. Xcoder
Saved 3 4 bytes thanks to Pavel

AdmBorkBork

Posted 2018-03-12T13:24:54.130

Reputation: 41 581

@Pavel Aha, yes. I need to swap around the -lt99 to a 99-gt to get the casting to work correctly, but that is indeed one byte shorter. Thanks! – AdmBorkBork – 2018-03-12T16:55:24.517

2

JavaScript, 43 40 Bytes

var t=t?--t:~!setTimeout`t=alert(1|~t)`;

2x:

var t=t?--t:~!setTimeout`t=alert(1|~t)`;var t=t?--t:~!setTimeout`t=alert(1|~t)`;

3x:

var t=t?--t:~!setTimeout`t=alert(1|~t)`;var t=t?--t:~!setTimeout`t=alert(1|~t)`;var t=t?--t:~!setTimeout`t=alert(1|~t)`;

l4m2

Posted 2018-03-12T13:24:54.130

Reputation: 5 985

p.s. this solution doesn't break the environment – l4m2 – 2018-03-12T16:12:28.377

2

C# (178 Bytes)

Console.WriteLine(1+2*4%int.Parse(System.Configuration.ConfigurationManager.AppSettings["z"]=(int.Parse(System.Configuration.ConfigurationManager.AppSettings["z"]??"0"))+1+""));

crazy C# solution, but I am happy it's possible in one line in C# at all. :)

For me the hardest part was having valid C# that would either intialize or increment the same variable, so I ended up abusing the ConfigurationManager because I needed a global static NameValueCollection and ConfigurationManager was the only one I could think of that I could update in memory. EnvironmentVariables was another option I Iooked at but it doesn't have an indexer so I am not sure how to do that in one line that can be copy pasted to produce the required output as per the spec.

lee

Posted 2018-03-12T13:24:54.130

Reputation: 200

2

Runic Enchantments, 35 bytes

^w3'\
    f
    1
/1@
/
 '54\w
/yyy

Try it online!

Working on this one allowed me to find an error in my parser dealing with the new delay modifier characters, although the final result ends up not being affected by it, as I ended up not needing them.

Functions due to the fact that the final line does not have a trailing newline (or for that matter, trailing spaces), allowing the duplicate IPs to spawn in a different place. The top-left one ends up making a large loop around the grid while the second IP performs a Reflection operation to replace the \ on the 6th line with a . This IP then will loop forever and do nothing.

The third IP also makes this same replacement at the same time, but because it's situated on the 13th line, its copy of that reflector sends it upwards and it executes the 1f'3w sequence present in the upper right corner, which replaces the 1 with a 3 on the 14th line, just before the original IP executes it, causing the tripled program to output 3 instead of 1 (values could also be 2 and 6, 3 and 9, 4 and 12, or 5 and 15 due to the availability of a-f numerical constants; 1 and 3 were chosen arbitrarily). It is then left in an endless loop performing more reflection commands that do nothing.

Try it in triplicate!

Draco18s no longer trusts SE

Posted 2018-03-12T13:24:54.130

Reputation: 3 053

29 bytes, though I think I could get it lower if I actually understood it :P – Jo King – 2018-12-19T05:47:56.237

@JoKing Not really surprised that a lot of that whitespace could come out. Did this one at work and due to running into an issue with the interpreter I was happy just to get something that worked (my original was 52 bytes and after getting the interpreter fixed, I was able to remove a good chunk). – Draco18s no longer trusts SE – 2018-12-19T14:16:54.387

2

Lost, 27 bytes

\\<<<<//<<<>
2>((1+@>((%^
v

Try it online!

Try it doubled!

Try it tripled!

This is mostly an improvement of Post Left Garf Hunter's answer that removes all the pesky no-ops from that last line,

To do this, we have to remove the !, otherwise the program always gets stuck in a loop. So we change the clearing section from >?!^%^ to >))%^' since there can only be a max of two elements on the stack anyway. Unfortunately, this means that the path from the extra copies to the original is broken, so we place an extra / in the first line to compensate. This forces the pointer to go through the clearing section for all copies of the program until it reaches the first one, where the ^ instead redirects it to the > instead.

Jo King

Posted 2018-03-12T13:24:54.130

Reputation: 38 234

1

Zsh, 25 bytes

<<<$[`wc -c<$0`>70?3:1] #

Zsh lets you use here-docs and here-strings in place instead of regular commands, so we can replace a call to echo with <<< which saves two bytes.

-2 thanks to Digital Trauma

Pavel

Posted 2018-03-12T13:24:54.130

Reputation: 8 585

1

Bash, 26 bytes

exit $[`wc -c&lt;$0`&gt;70?3:1];

Includes trailing newline.

Outputs via exit code.

Very similar to my zsh answer.

exit instead of echo means instead of <space># at the end, we only have a single ;.

-2 bytes thanks to Digital Trauma

Pavel

Posted 2018-03-12T13:24:54.130

Reputation: 8 585

$[...] instead of $((...)) saves 2 bytes. I don't know if this works for zsh too... – Digital Trauma – 2018-03-14T21:41:12.967

1

Perl 5, 28 25 bytes

-3 bytes thanks to @neil!

print"\e[D$a"if(++$a!=2);

Moves the cursor backwards (does nothing on start of line) and prints the value of $a, first and third time (i.e., the third time a 1 is printed, the cursor moves, and a 3 is printed on the position of the 1).

Marijn

Posted 2018-03-12T13:24:54.130

Reputation: 161

1print"\e[D$a"if(++$a!=2); perhaps? – Neil – 2018-03-12T17:15:53.663

1

Haskell (runhaskell), 46 bytes

-8 bytes thanks to BMO.

main=do x<-readFile"a";print$length x`gcd`3 --

Try it online!

Needs the file name to be a. Apparently GHC needs the file to end with .hs, but runhaskell doesn't care.

totallyhuman

Posted 2018-03-12T13:24:54.130

Reputation: 15 378

1

DOS .BAT, 60 Bytes

@cls&find /n " " %0|findstr /b .^3>nul&&set/p=^1<nul&echo 5

l4m2

Posted 2018-03-12T13:24:54.130

Reputation: 5 985

1

QBasic, 19 bytes

CLS
x=2^x
?x-1OR 1

The source should include a trailing newline.

Explanation

We want output of 1, 1, 3. Observe that these numbers are one less than powers of 2. So:

CLS      ' CLear Screen of any output from previous copies of the code

x        ' Numeric variables are preset to 0...
 =2^x    ' so as this statement is repeated, the value of x goes 1, 2, 4

 x-1     ' Therefore x-1 goes 0, 1, 3...
    OR 1 ' and we bitwise OR it with 1 to change the 0 to 1...
?        ' and print.

DLosc

Posted 2018-03-12T13:24:54.130

Reputation: 21 213

1

nwellnhof

Posted 2018-03-12T13:24:54.130

Reputation: 10 037

1

Python 2, 38 bytes

1
id=id<3and-~id
exec"print id|1"*0**0

Try it online! (doubled, tripled)

Outputs 1, 1, or 3.


Python 2, 33 bytes

1
True+=1
exec"print 7%True"*0**0

Try it online! (doubled, tripled)

Outputs 1,1,3. Uses the True-modifying idea by Aidan F. Pierce from Xcoder's solution.

xnor

Posted 2018-03-12T13:24:54.130

Reputation: 115 687

1

PHP, 29 bytes

<?=filesize(__FILE__)<86?:3;#

Try it online!

Try it doubled!

Try it tripled!

How it works

  • <?= -- Opens a PHP tag and echos whatever's inside. It's a shorthand for <?php echo.
  • filesize(__FILE__) -- Gets the size of the current file.
  • <86 -- Checks that the size is less than 86 since when the code is triplicated, it's 87 bytes long.
  • ?:3; -- If the condition is true, it outputs 1 since the middle of the ternary is omitted. If not, then output 3.
  • ;# -- PHP supports shell-style comments (#) and they're less bytes than the C++ style comment (//). This just makes the compiler ignore the other two copies of code.

Ethan

Posted 2018-03-12T13:24:54.130

Reputation: 435

1

Klein 000 or 010, 8 bytes

\((1+@
2

Try it online!

\((1+@
2\((1+@
2

Try it online!

\((1+@
2\((1+@
2\((1+@
2

Try it online!

Explanation

The backbone of our program is the left had side which pushes a number of 2s. Each time we add a copy of the program another 2 gets added to the backbone of the program. Once it goes off the bottom it executes the code

((1+@

Which removes the first 2 stack items and adds one to the remainder. Once our backbone has 3 2s we will add 1 and get 3, if we have any less we will just discard the entire stack and return 1.

Klein 001 or 011, 8 bytes

\(1+@2
.

Try it online!

\(1+@2
.\(1+@2
.

Try it online!

\(1+@2
.\(1+@2
.\(1+@2
.

Try it online!

Explanation

The 010 and 011 topologies work a little differently. When the ip goes of the bottom on the left it comes down on the right. Here we do something very similar except our ending bit is

\(1+@

which will output 1 until the stack is two high. The . causes the 2 to be cut off when we extend the program meaning we only pick up our second 2 when we reach the third iteration of the program.

Post Rock Garf Hunter

Posted 2018-03-12T13:24:54.130

Reputation: 55 382

1

Wolfram Language (Mathematica), 23 bytes

Once[c=0];c++//1-#+#^2&

Try it online!

Try it doubled!

Try it tripled!

Explanation:

In Wolfram Language Once evaluates the contained expression once in each session, always returning the result from the first evaluation. This lets me set an anchor point for c. Now it's just a matter of math.

chuy

Posted 2018-03-12T13:24:54.130

Reputation: 389

I think the reason it works locally is that you've already redefined Times on the first prompt, so on the last prompt it's already redefined. Does it still work if you only execute the tripled line? – Martin Ender – 2018-03-14T19:14:08.130

Ahh that's probably it – chuy – 2018-03-14T20:33:03.587

Back to the drawing board – chuy – 2018-03-14T20:33:58.800

According to Wolfram Sandbox, directly executing the tripled line outputs a 1. – Weijun Zhou – 2018-03-15T00:34:18.817

Yes I need to modify – chuy – 2018-03-15T00:48:12.880

1

Ruby, 31 bytes

$><<?\b;a||=0;a+=1;$><<a/3*2+1;

This uses \b which doesn't work everywhere (tio).

Asone Tuhid

Posted 2018-03-12T13:24:54.130

Reputation: 1 944

1

Perl 5, 10 bytes

say+0|++$i

Try it once!

Try it twice!

Try it thrice!

primo

Posted 2018-03-12T13:24:54.130

Reputation: 30 891

1

Attache, 34 bytes

Echo@Sum@ProperDivisors@`#<|1
'1??

Try it online! Implements A001065. There are probably shorter ways, but this was fun.

Prints A001065(# of repetitions + 1).

Doubled program

Tripled program

Conor O'Brien

Posted 2018-03-12T13:24:54.130

Reputation: 36 228

1

Powershell, 14 bytes

Sum array elements. An operator + can work as a unary and binary operator.

+(1,0,2)[$i++]

Test script:

+(1,0,2)[$i++]

$i=0 # or start new powershell session

+(1,0,2)[$i++]+(1,0,2)[$i++]

$i=0 # or start new powershell session

+(1,0,2)[$i++]+(1,0,2)[$i++]+(1,0,2)[$i++]

Note: $i=0 emulates a new session. You can type three times in a new powershell terminal window to start a pure test instead.

Output:

1
1
3

mazzy

Posted 2018-03-12T13:24:54.130

Reputation: 4 832

1

Befunge-98 (PyFunge), 12 9 6 bytes (by Jo King)

3g.@
`

Try it once!

Try it doubled!

Try it tripled!


Old solution:

2g' `4\kc.@

Try it online!

david

Posted 2018-03-12T13:24:54.130

Reputation: 180

Nice one. Edited the post. – david – 2019-09-07T03:45:46.747

Also just realized, that your answer makes it a valid Befunge-93 program, instead of being limited to Befunge-98. – david – 2019-09-07T04:05:28.053

1

Aha! 6 bytes by printing the fetched character value outright.

– Jo King – 2019-09-07T15:51:07.530

0

Batch, 28 bytes

@cmd/cset/a1+2*!(%~z0%%3)
:

Conveniently the file length is not a multiple of 3 until the file is tripled, so it remains to take the remainder modulo 3, take the logical not, double that, and add 1.

Neil

Posted 2018-03-12T13:24:54.130

Reputation: 95 035

0

SmileBASIC, 18 bytes

X=X+1CLS?(X>2)*2+1

12Me21

Posted 2018-03-12T13:24:54.130

Reputation: 6 110

0

Charcoal, 10 bytes

P§311L⊞Oυω

Try it online! Based on my answer to I double the source, you double the output! but I cyclically look up the appropriate character to print instead of just printing the current length cast to string. (The 3 is first because the iteration numbers are 1-indexed but Charcoal's string indexing is 0-indexed.)

Neil

Posted 2018-03-12T13:24:54.130

Reputation: 95 035

0

sed, 37 bytes

g
s/^/1/
H
s/\n//g
s/1111/3/
s/11/1/

Using the pattern space to preserve state is the key here.

Try it online!

GammaFunction

Posted 2018-03-12T13:24:54.130

Reputation: 2 838

0

05AB1E, 4 bytes

X3Ur

Try it online!, doubled, tripled.

X           # push X on the stack (X defaults to 1)
 3U         # set X = 3
   r        # reverse the stack

Grimmy

Posted 2018-03-12T13:24:54.130

Reputation: 12 521