# Third time the charm

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

• Is our code allowed to read its own source code? – AdmBorkBork Mar 12 '18 at 13:45
• @AdmBorkBork I'd assume so, since this isn't tagged as quine. – Erik the Outgolfer Mar 12 '18 at 13:47
• @AdmBorkBork Yes. – workoverflow Mar 12 '18 at 14:06
• I 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 Mar 13 '18 at 1:42
• @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 Mar 16 '18 at 16:54

# C (gcc), 53 52 bytes

Note the space after #endif.

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


Try it online!

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

# 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


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

# 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!

• 29 bytes, though I think I could get it lower if I actually understood it :P – Jo King Dec 19 '18 at 5:47
• @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 Dec 19 '18 at 14:16

## SQL, 2524 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

• PRINT instead of SELECT would save an extra byte. – Razvan Socol Mar 24 '18 at 9:07

# Perl 5, 10 bytes

say+0|++$i  Try it once! Try it twice! Try it thrice! # Charcoal, 10 bytes Ｐ§311Ｌ⊞Ｏυω  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.) # Ruby, 31 bytes $><<?\b;a||=0;a+=1;$><<a/3*2+1;  This uses \b which doesn't work everywhere (tio). ## 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. # 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. • 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 Mar 14 '18 at 19:14 • Ahh that's probably it – chuy Mar 14 '18 at 20:33 • Back to the drawing board – chuy Mar 14 '18 at 20:33 • According to Wolfram Sandbox, directly executing the tripled line outputs a 1. – Weijun Zhou Mar 15 '18 at 0:34 • Yes I need to modify – chuy Mar 15 '18 at 0:48 # Python 2, 38 bytes 1 id=id<3and-~id exec"print id|1"*0**0  Outputs 1, 1, or 3. # Python 2, 33 bytes 1 True+=1 exec"print 7%True"*0**0  Outputs 1,1,3. Uses the True-modifying idea by Aidan F. Pierce from Xcoder's solution. # 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 # 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 • $[...] instead of $((...)) saves 2 bytes. I don't know if this works for zsh too... – Digital Trauma Mar 14 '18 at 21:41 # 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. • – Jo King Dec 19 '18 at 4:49 • @joking unfortunately I'm stuck on mobile for the time being. Could you post it as an answer with an explanation? – Sriotchilism O'Zaic Dec 19 '18 at 21:28 • Posted – Jo King Dec 19 '18 at 22:26 # ><>, 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. • 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 Mar 13 '18 at 17:04 • @Notatree Got the 8 byte with an unprintable – Jo King Mar 14 '18 at 9:20 # 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. # Haskell (runhaskell), 46 bytes -8 bytes thanks to BMO. main=do x<-readFile"a";print$length xgcd3 --


Try it online!

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

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

# R, 3731 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).

• 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 Mar 12 '18 at 21:41
• @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 Mar 12 '18 at 21:47
• oh weird I musta had a trailing newline, but if you do %/%2 it should work – Giuseppe Mar 12 '18 at 21:48

# Perl 5, 18151312 11 bytes

-3 bytes thanks to nwellnhof

say 1|
+.7#


Once Try it online!

Twice Try it online!

Thrice Try it online!

• n;$_=$a++|1;say (15 bytes) – nwellnhof Mar 13 '18 at 4:25
• @nwellnhof Of course!. Thanks. – Ton Hospel Mar 13 '18 at 6:31

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

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


# 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

• Thanks for the idea of using a comment - saved me 6 bytes. – AdmBorkBork Mar 12 '18 at 14:40
• print open(__file__,"a").tell()/79*3|1# perhaps? – Kevin Cruijssen Mar 12 '18 at 14:48
• @KevinCruijssen Great, thanks! – Mr. Xcoder Mar 12 '18 at 14:51
• One fewer byte, no source code reading: tio.run/##K6gsycjPM/r/… Prints with leading spaces for two and three repetitions, but that appears to be allowed. – Aidan F. Pierce Mar 13 '18 at 3:28
• @AidanF.Pierce Thank you so much! I golfed it a bit more and posted it as an alternate solution. – Mr. Xcoder Mar 13 '18 at 5:30

# Perl 6, 17 bytes

0 x 0;say .++ +|1


Try it online!

Try it doubled!

Try it tripled!

# Japt, 86 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

• Rearranging a little, I think you can do °U-v (plus newline) to save on the second U. – ETHproductions Mar 13 '18 at 2:27
• @ETHproductions Thanks! v is perfect for this challenge :-) – Oliver Mar 13 '18 at 2:52

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

• I really enjoyed your explanation. +1 – workoverflow Mar 16 '18 at 16:56

# SmileBASIC, 18 bytes

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


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

# ><>, 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


# C (gcc), 9591 85 bytes

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

`

Try it online!

• Then I suggest that you edit the correct TIO link in. – Erik the Outgolfer Mar 12 '18 at 15:58
• I sometimes can't connect to TIO, I don't know why – l4m2 Mar 12 '18 at 15:59