127
\$\begingroup\$

Your task, if you wish to accept it, is to write a program that outputs a positive integer (higher than 0). The tricky part is that if I duplicate your source code, the output must be double the original integer.

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.

  • 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 be 8.

Leaderboard

This uses uses @manatwork's layout.

/* Configuration */

var QUESTION_ID = 132558; // Obtain this from the url
// It will be like https://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 8349457; // This should be the user ID of the challenge author.

/* App */

var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;

function answersUrl(index) {
  return "https://api.stackexchange.com/2.2/questions/" +  QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}

function commentUrl(index, answers) {
  return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}

function getAnswers() {
  jQuery.ajax({
    url: answersUrl(answer_page++),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      answers.push.apply(answers, data.items);
      answers_hash = [];
      answer_ids = [];
      data.items.forEach(function(a) {
        a.comments = [];
        var id = +a.share_link.match(/\d+/);
        answer_ids.push(id);
        answers_hash[id] = a;
      });
      if (!data.has_more) more_answers = false;
      comment_page = 1;
      getComments();
    }
  });
}

function getComments() {
  jQuery.ajax({
    url: commentUrl(comment_page++, answer_ids),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      data.items.forEach(function(c) {
        if (c.owner.user_id === OVERRIDE_USER)
          answers_hash[c.post_id].comments.push(c);
      });
      if (data.has_more) getComments();
      else if (more_answers) getAnswers();
      else process();
    }
  });  
}

getAnswers();

var SCORE_REG = /<h\d>\s*([^\n,]*[^\s,]),.*?(-?\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;

var OVERRIDE_REG = /^Override\s*header:\s*/i;

function getAuthorName(a) {
  return a.owner.display_name;
}

function process() {
  var valid = [];
  
  answers.forEach(function(a) {
    var body = a.body;
    a.comments.forEach(function(c) {
      if(OVERRIDE_REG.test(c.body))
        body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
    });
    
    var match = body.match(SCORE_REG);
    if (match)
      valid.push({
        user: getAuthorName(a),
        size: +match[2],
        language: match[1],
        link: a.share_link,
      });
    
  });
  
  valid.sort(function (a, b) {
    var aB = a.size,
        bB = b.size;
    return aB - bB
  });

  var languages = {};
  var place = 1;
  var lastSize = null;
  var lastPlace = 1;
  valid.forEach(function (a) {
    if (a.size != lastSize)
      lastPlace = place;
    lastSize = a.size;
    ++place;
    
    var answer = jQuery("#answer-template").html();
    answer = answer.replace("{{PLACE}}", lastPlace + ".")
                   .replace("{{NAME}}", a.user)
                   .replace("{{LANGUAGE}}", a.language)
                   .replace("{{SIZE}}", a.size)
                   .replace("{{LINK}}", a.link);
    answer = jQuery(answer);
    jQuery("#answers").append(answer);

    var lang = a.language;
    if (! /<a/.test(lang)) lang = '<i>' + lang + '</i>';
    lang = jQuery(lang).text().toLowerCase();

    languages[lang] = languages[lang] || {lang: a.language, user: a.user, size: a.size, link: a.link, uniq: lang};
  });

  var langs = [];
  for (var lang in languages)
    if (languages.hasOwnProperty(lang))
      langs.push(languages[lang]);

  langs.sort(function (a, b) {
    if (a.uniq > b.uniq) return 1;
    if (a.uniq < b.uniq) return -1;
    return 0;
  });

  for (var i = 0; i < langs.length; ++i)
  {
    var language = jQuery("#language-template").html();
    var lang = langs[i];
    language = language.replace("{{LANGUAGE}}", lang.lang)
                       .replace("{{NAME}}", lang.user)
                       .replace("{{SIZE}}", lang.size)
                       .replace("{{LINK}}", lang.link);
    language = jQuery(language);
    jQuery("#languages").append(language);
  }

}
body { text-align: left !important}

#answer-list {
  padding: 10px;
  width: 290px;
  float: left;
}

#language-list {
  padding: 10px;
  width: 290px;
  float: left;
}

table thead {
  font-weight: bold;
}

table td {
  padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/Sites/codegolf/all.css?v=617d0685f6f3">
<div id="answer-list">
  <h2>Leaderboard</h2>
  <table class="answer-list">
    <thead>
      <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>
    </thead>
    <tbody id="answers">

    </tbody>
  </table>
</div>
<div id="language-list">
  <h2>Winners by Language</h2>
  <table class="language-list">
    <thead>
      <tr><td>Language</td><td>User</td><td>Score</td></tr>
    </thead>
    <tbody id="languages">

    </tbody>
  </table>
</div>
<table style="display: none">
  <tbody id="answer-template">
    <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr>
  </tbody>
</table>
<table style="display: none">
  <tbody id="language-template">
    <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td><a href="{{LINK}}">{{SIZE}}</a></td></tr>
  </tbody>
</table>

\$\endgroup\$
  • 2
    \$\begingroup\$ @Mr.Xcoder Then I'll just have to include one in my own source. \$\endgroup\$ – steenbergh Jul 15 '17 at 17:55
  • 3
    \$\begingroup\$ @Mr.Xcoder I think that you should have prevented reading your own source code. \$\endgroup\$ – caird coinheringaahing Jul 15 '17 at 18:09
  • 1
    \$\begingroup\$ It only needs to work when doubled once? We don't need to support n many doublings? \$\endgroup\$ – Cody Gray Jul 16 '17 at 8:36
  • 6
    \$\begingroup\$ @Daniel Let's say your source is `` (empty program) and it produces 5. If you double it, your source is `` (empty program) and that produces 5 as well, no matter what you do. That being said, an empty program duplicated is still the empty program, and always produces the same output, except for the case where the empty program means something else (a random number generator, for example), which could not be valid anyway. \$\endgroup\$ – Mr. Xcoder Jul 17 '17 at 9:11
  • 1
    \$\begingroup\$ This shouldn't be hard for esolangs that automatically dump the top of stack upon program termination. \$\endgroup\$ – MD XF Jul 18 '17 at 22:26

130 Answers 130

5
\$\begingroup\$

JavaScript (ES6), 63 bytes

Prints either 1 or 2 through an alert dialog.

Original

clearTimeout((t=this).x),t.x=setTimeout(`alert(${t.n=-~t.n})`);

Doubled

clearTimeout((t=this).x),t.x=setTimeout(`alert(${t.n=-~t.n})`);clearTimeout((t=this).x),t.x=setTimeout(`alert(${t.n=-~t.n})`);

\$\endgroup\$
  • 1
    \$\begingroup\$ I guess this would work also: window.i=++window.i||1; in the browser console. It ouputs 1. Browser refresh, window.i=++window.i||1;window.i=++window.i||1; ouputs 2. \$\endgroup\$ – Christiaan Westerbeek Jul 17 '17 at 13:13
  • 1
    \$\begingroup\$ @ChristiaanWesterbeek True. But then it's a REPL answer (and you can actually just do this.i=++this.i||1;). \$\endgroup\$ – Arnauld Jul 17 '17 at 13:23
  • 1
    \$\begingroup\$ I don't know what that means, REPL answer \$\endgroup\$ – Christiaan Westerbeek Jul 17 '17 at 13:26
  • \$\begingroup\$ @ChristiaanWesterbeek REPL stands for Read-Eval-Print Loop. In that case, the final result is not explicitly printed by the code but by the shell it's running in (like the browser console). \$\endgroup\$ – Arnauld Jul 17 '17 at 13:31
  • 1
    \$\begingroup\$ @ChristiaanWesterbeek (Actually, just +1 would work in REPL -- like this Python REPL answer does.) \$\endgroup\$ – Arnauld Jul 17 '17 at 13:33
5
\$\begingroup\$

PHP, 30 bytes

Original

<?=strlen(file(__FILE__)[0]);#

Try it online!

PHP, 60 bytes

doubled

<?=strlen(file(__FILE__)[0]);#<?=strlen(file(__FILE__)[0]);#

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Port of the Python solution? \$\endgroup\$ – Mr. Xcoder Jul 15 '17 at 16:57
  • \$\begingroup\$ @Mr.Xcoder after your comment I have saw the python solution. It is the same way \$\endgroup\$ – Jörg Hülsermann Jul 15 '17 at 17:07
  • 1
    \$\begingroup\$ Why not <?=strlen(file(__FILE__)[0]);# \$\endgroup\$ – Darren H Jul 15 '17 at 17:34
  • \$\begingroup\$ @DarrenH I have not thinking about it \$\endgroup\$ – Jörg Hülsermann Jul 15 '17 at 17:45
  • \$\begingroup\$ this is 23 bytes: <?=filesize(__FILE__);# - and doubled is 46 bytes. \$\endgroup\$ – hanshenrik Jul 23 '17 at 13:27
5
\$\begingroup\$

J, 2 bytes

+1

+1 is 1, and +1+1 is 2.

Fun fact: since J's operator precedence is just right-to-left, this is interpreted as +(1+1)

\$\endgroup\$
  • 3
    \$\begingroup\$ This is a real Polyglot: Python REPL, APL, J, Pip \$\endgroup\$ – Mr. Xcoder Jul 15 '17 at 21:30
  • \$\begingroup\$ @Mr.Xcoder To be fair it works in pretty much every non-esoteric language with a REPL. \$\endgroup\$ – Cyoce Jul 16 '17 at 22:54
  • 1
    \$\begingroup\$ I thought J's operator precedence was right to left? e.g. 3*6+5 = 33 \$\endgroup\$ – Richard Donovan Jul 21 '17 at 18:38
  • \$\begingroup\$ @RichardDonovan right. I goofed \$\endgroup\$ – Cyoce Jul 22 '17 at 0:37
5
\$\begingroup\$

Befunge-98 (PyFunge), 3 bytes

This answer draws heavy inspiration from this one, go vote them up too!

".@

Try it online! or Try it online doubled!

Explanation

When wrapping back around to the beginning of the source in quote mode, this Befunge-98 interpreter pushes at least 1 space (ASCII 32). If we print this value and exit, we get 32.

If we double the source code, we don't wrap back around, but rather end quote mode with the duplicated ". This means that the last character pushed is not a space, but a @, with (drumroll) ASCII 64! A complete coincidence that the character that ends the program in Befunge is twice that of a space.

\$\endgroup\$
4
\$\begingroup\$

Pyth, 3 bytes

s[1

sums a list

Try it!

Double-try it

\$\endgroup\$
4
\$\begingroup\$

SOGL V0.12, 1 byte

I

Try it Here!, or try the duplicated version
I is the increase command, and as no input is provided, it increases 0 (and then in the duplicated program, 1 to 2)

\$\endgroup\$
  • \$\begingroup\$ Can you provide a working link for me to test it on my mac as well (the classic problem)? EDIT: Thanks. \$\endgroup\$ – Mr. Xcoder Jul 15 '17 at 15:19
4
\$\begingroup\$

Charcoal, 7 bytes

PIL⊞Oυω

Try it online! Appends the empty string to the empty list and prints the length of the result (1) in decimal without moving the cursor. When doubled, the empty string gets appended twice so the length is now 2, which then overwrites the 1. Try it online! In verbose syntax, this is Multiprint(Cast(Length(PushOperator(u, w))));.

\$\endgroup\$
  • 6
    \$\begingroup\$ When I look at the code, it looks like it says "pillow"! \$\endgroup\$ – Cort Ammon Jul 16 '17 at 5:43
4
\$\begingroup\$

Hexagony, 4 bytes

[@!)

Try it online! or Try it doubled online!

This solution was found using brute force by Martin and prints 002 regularly and 4 when doubled. This is the only 4 byte solution, but there are several hundred 5 byte solutions.

Expanded versions:

Regular:

 [ @
! ) .
 . .

Doubled:

  [ @ !
 ) [ @ !
) . . . .
 . . . .
  . . .

Hexagony has 6 instruction pointers, one starting at each corner of the hexagon and initially moving "clockwise." The [ instruction causes the interpreter to change IP one left. The first program leads us on a bit of a merry go around, executing: [![.!.)[...)!@ which results in the 002 output. The other program is a bit more tame, executing: [))[..)...[.......)!@ which results in the clean 4.

\$\endgroup\$
4
\$\begingroup\$

Python, 15 bytes

1or exit(2)
0/0

Outputs via exit code. Divides by zero and exits with code 1. Doubled:

1or exit(2)
0/01or exit(2)
0/0

Calls exit(2) and exits with code 2.

Python?, 9 bytes

';exit(2)

Throws a syntax error and exits with code 1. Doubled:

';exit(2)';exit(2)

Calls exit(2) and exits with code 2.

\$\endgroup\$
4
\$\begingroup\$

JavaScript (REPL), 2 bytes

So long as putting code in the console counts as a full program:

+1
\$\endgroup\$
  • \$\begingroup\$ Putting the code in the console does not count as a full program. This is a snippet and therefore, unfortunatly, invalid. \$\endgroup\$ – Pavel Jul 17 '17 at 17:05
  • \$\begingroup\$ Even if I make as REPL? \$\endgroup\$ – Jake Taylor Jul 18 '17 at 9:19
  • 2
    \$\begingroup\$ I'd say mark it as REPL and run with it. People who have sour grapes over "JavaScript REPL" but not over the various golfing languages which get such things down to one byte are being oddly specific with their moralistic tendencies. The interpreter which runs JS REPL code is a lot more straightforward than, say, Bubblegum. \$\endgroup\$ – CR Drost Jul 18 '17 at 21:31
  • \$\begingroup\$ Marked as REPL. \$\endgroup\$ – Jake Taylor Jul 19 '17 at 10:36
4
\$\begingroup\$

Little Man Computer, 20 bytes (source)

LDA 1
ADD 5
OUT
HLT

Online Emulator (Flash)

\$\endgroup\$
  • 1
    \$\begingroup\$ Wowee never thought i'd have to see LMC again after I finished my a levels ... lovely little answer \$\endgroup\$ – space junk Jul 19 '17 at 11:09
4
\$\begingroup\$

Python 2, 24 bytes

'+1#';exec'print 1''' ''

Abusing string syntax.

\$\endgroup\$
3
\$\begingroup\$

V, 4 bytes

øß.

Try it online!

Or Try it doubled!

\$\endgroup\$
3
\$\begingroup\$

Cubix, 6 bytes

@1u.2O

When run, this pushes 1, u-turns, Outputs as a number, and h@lts:

  @
1 u . 2
  O

When doubled, the cube is

    @ 1
    u .
2 O @ 1 u . 2 O
. . . . . . . .
    . .
    . .

which simply pushes 2, Outputs as a number, and h@lts.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ While looking at the duplicate question for a bit of fun I came up with this ^5O@N. Also has the twist of printing out it's length :) \$\endgroup\$ – MickyT Aug 30 '17 at 2:10
  • \$\begingroup\$ @MickyT you should post that as your own answer! That's a super cool one :) \$\endgroup\$ – Giuseppe Dec 15 '17 at 19:54
  • \$\begingroup\$ Done, but found another byte which made it a bit more boring \$\endgroup\$ – MickyT Dec 17 '17 at 17:47
3
\$\begingroup\$

R, 2 bytes

+1

prints 1 and when doubled

+1+1

prints 2

\$\endgroup\$
  • 1
    \$\begingroup\$ or +T or +2 or... \$\endgroup\$ – Giuseppe Jul 17 '17 at 19:21
  • \$\begingroup\$ This is exactly the same as the J answer. \$\endgroup\$ – Zacharý Aug 23 '17 at 23:31
3
\$\begingroup\$

Fourier, 3 bytes

@^o

Try it Online!

Try it Doubled!

Explanation:

Undoubled:

@    - Clear screen
 ^   - Increment the accumulator (initialised to zero)
  o  - Output the value of the accumulator

Doubled:

@       - Clear screen
 ^      - Increment the accumulator (initialised to zero)
  o     - Output the value of the accumulator
   @    - Clear screen
    ^   - Increment the accumulator
     o  - Output the value of the accumulator
\$\endgroup\$
3
\$\begingroup\$

Ruby (REPL), 3 2 bytes

Must be run interactively for the implicit output:

+2

Outputs 2.

Doubled:

+2+2

Outputs 4.

2 and 4 are more interesting than 1 and 2

\$\endgroup\$
  • 3
    \$\begingroup\$ @MorganThrapp REPL answers are allowed, as long as they're clearly labeled as such. \$\endgroup\$ – Dennis Jul 17 '17 at 21:08
  • \$\begingroup\$ My bad. I haven't been actively recently and missed that. \$\endgroup\$ – Morgan Thrapp Jul 17 '17 at 21:10
  • 1
    \$\begingroup\$ +2 is a byte shorter. \$\endgroup\$ – m-chrzan Jul 19 '17 at 4:38
3
\$\begingroup\$

PHP, 26 bytes

<?=ob_clean()+ob_start()?>

Try it online!

Try it doubled!

Explanation

PHP can use an output buffer. This buffer has the facility to be cleaned without its contents ever being sent.

When the script is run just once, the output buffer has not initially been started and so cannot be cleaned, and ob_clean() returns false. The buffer is then successfully started and ob_start() returns true. false + true is really 0 + 1, and so the script echoes 1 into the buffer and ends with the buffer being outputted.

If the script is doubled then this first 1 is not output but is still in the buffer when the second half of the doubled code starts. The output buffer is now cleaned, the 1 is lost, and ob_clean() this time returns true. Another output buffer is then started (they are, in fact, stacked) and ob_start() again returns true, so this time we have true + true, which is 1 + 1, and so 2 is sent to the output buffer and then on to the actual output when the script ends.

\$\endgroup\$
3
\$\begingroup\$

PHP, 16 bytes

Does not actually satisfy the rules because of PHP warnings.

ob_start(bcadd);

Test it online.

\$\endgroup\$
  • \$\begingroup\$ Not really 16 bytes as you need to turn errors off to pass OP's rules \$\endgroup\$ – IsThisJavascript Aug 8 '17 at 13:00
  • 1
    \$\begingroup\$ And if you include the tags, opening and closing because you cannot double the source without the closing tag, it comes in at 61 bytes. sandbox.onlinephpfunctions.com/code/… ! Quite far from 16!! \$\endgroup\$ – IsThisJavascript Aug 8 '17 at 13:02
  • 3
    \$\begingroup\$ The OP’s rules actually say that warnings are OK, BUT, strangely, and I don't understand what's going on here, if the warnings are not turned off then the single code and the doubled code both return 9, and so the warnings are an essential part of your code. The shortest I can get your script down to is 56 bytes (sandbox.onlinephpfunctions.com/code/…), 40 more than the 16 you are falsely claiming. How about changing your answer? \$\endgroup\$ – WebSmithery Aug 13 '17 at 15:20
3
\$\begingroup\$

C++, 158 138 123 bytes

20 byte savings thanks to @zbw

I'm sure there's a more efficient way to do it in C++ but this is all I could think of.

#include <iostream>
#ifdef m
#define m a r;
#else
int v;
#define m struct a{a(){++v;}}q;int main(){std::cout<<v;}
#endif
m

How it works: On the first instance, it declares a global int v initialized to 0 and a struct a which increments every time an a is instanced. It instances an a, and prints the value of the member.

On the second instance of the source, it just instances another a.

\$\endgroup\$
  • \$\begingroup\$ You could change a to a struct, where it'll default to public, saving 6 bytes (explanation here). Also, remove the unneeded #undef, for 9 bytes, and reorder the cases in the #ifndef so it becomes #ifdef for another. You can remove the int before main. Try it online! \$\endgroup\$ – zbw Jul 20 '17 at 15:47
  • \$\begingroup\$ @zbw Good catches! Thanks. (Can't believe I forgot the struct thing in particular.) The #undef was to avoid a compiler warning but I guess that's not an issue for code golf. :) \$\endgroup\$ – fluffy Jul 21 '17 at 15:49
3
\$\begingroup\$

Proton, 63 53 52 bytes

+(x? !print(x=2):0)
while(x? x-2? print(1):0:(x=1))x

Prints 1.

Try it online!

Doubled:

+(x? !print(x=2):0)
while(x? x-2? 0:print(1):(x=1))x+(x? !print(x=2):0)
while(x? x-2? 0:print(1):(x=1))x

Prints 2.

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Removing the spaces seems to give the desired results \$\endgroup\$ – Mr. Xcoder Jul 12 '18 at 12:50
3
\$\begingroup\$

Python 3, 33 bytes

Cheating a little:

id=print('\r',1+(not id),end='');
\$\endgroup\$
  • \$\begingroup\$ On a full console, you should only see 1 or 2; or did you mean that the whole output stream contains more than the number itself? (I'm not sure how this behaves in Windows, but on Debian or a Mac, this looks like a single output). \$\endgroup\$ – lungj Jul 22 '17 at 19:38
  • 1
    \$\begingroup\$ Oh, yeah, I did a test in TIO before and saw that it didn't work there. In a console, the \r gives a carriage return, but not a newline. Thus, when the second copy of the code is run, it actually starts overwriting the content from the first copy of the code. \$\endgroup\$ – lungj Jul 22 '17 at 19:43
3
\$\begingroup\$

bash, 16 bytes

a simple one:

wc -c<$0;exit 0;

First program: Try it online!

Second program: Try it online!

\$\endgroup\$
  • \$\begingroup\$ Thanks @Step-hen for the 2 TIO, I tried to use the site myself while writing the answer, but nothing happened when I pressed the run button (no output on my own attempts to set it up... weird as it was identical to yours...). Thanks for putting those 2 working links. \$\endgroup\$ – Olivier Dulac Jul 19 '17 at 15:31
  • \$\begingroup\$ No problem, I believe something is going wonky with TIO's backend right now (Dennis was talking about DOS attempts) so it's working on and off, sometimes it acts like it is infinitely looping. \$\endgroup\$ – Stephen Jul 19 '17 at 15:33
  • \$\begingroup\$ @StepHen: this looks like what was happening to me indeed (play button was looping). Thanks fo the info. Maybe not a DOS but just someone putting a script there that requires too much processing power? (Hanlon's razor : "Never attribute to malice that which is adequately explained by stupidity" ^^ ) \$\endgroup\$ – Olivier Dulac Jul 19 '17 at 15:34
  • \$\begingroup\$ wc is not part of bash, so should be mentioned in the title line. And I see no bashism so think, this will run on sh+wc too. \$\endgroup\$ – yeti Mar 16 '18 at 4:03
3
\$\begingroup\$

Rust, 62 bytes

fn main(){let a=include_str!("a.");print!("{}",a.len()-1)}//

What's happening is the file is including itself as a string during compiletime, and getting the length of itself.

When appended, the extra code is commented out, but still effects the length of the file.

Only works if the file name is 'a.'

Secondary Solution, 63 bytes

fn main(){let a=include_str!(file!());print!("{}",a.len()-1)}//

This solution doesn't use a hardcoded filename, losing 1 byte.

\$\endgroup\$
  • \$\begingroup\$ If the file name is needed, then you have to add the file name to the bytecount. Meta post \$\endgroup\$ – Jo King Feb 11 '18 at 22:01
  • \$\begingroup\$ Done. I still gain a byte. ^_^ (I'd remove the dot, but otherwise rustc complains) \$\endgroup\$ – moonheart08 Feb 12 '18 at 2:28
3
\$\begingroup\$

Underload, 15 13 8 bytes

-2 thanks to Martin Ender

-5 thanks to Ørjan Johansen

(1)\r2)S(

The \r in the code should be a literal carriage return character.

There's no way for spec-complaint Underload to actually solve this challenge. This submission uses several tricks to do it:

  • stringie, the TIO interpreter, ignores unmatched close brackets and segfaults on unmatched open brackets, which is allowed.
  • Using \r allows us to overwrite whatever has already been output with new output. This requires a terminal for which \r clears the line, rather than just allowing it to be overwritten with new text.
\$\endgroup\$
3
\$\begingroup\$

Implicit, 2 1 byte

-1 thanks to ASCII-only

.

:)

\$\endgroup\$
  • \$\begingroup\$ Link's broken for me \$\endgroup\$ – Aidan F. Pierce Mar 27 '18 at 4:17
  • \$\begingroup\$ @AidanF.Pierce fixed. \$\endgroup\$ – MD XF Mar 27 '18 at 18:32
  • \$\begingroup\$ . seems to work? \$\endgroup\$ – ASCII-only May 14 '18 at 10:35
3
\$\begingroup\$

Brachylog, 8 bytes

w₆-₇ℕ~l"

Try it online! Try it online!Try it online!

Although Brachylog has implicit function output, full programs won't automatically print without being given arguments, making this challenge a bit more interesting.

w₆          Declaratively write a number
  -₇        which minus 7
    ℕ       is a whole number (so length won't throw an error)
     ~l     equal to the length of
       "    an (implicitly terminated) empty string.

Doubling the code turns the empty string into a string of length 7, changing the printed output from 7 to 14.

\$\endgroup\$
3
\$\begingroup\$

Keg, 2 bytes

This indeed works. (Works in the old TIO version, I don't know which version is this)

1+

A version that I can understand, 4 bytes

1(+)

Explanation:

1#    Push 1 onto the stack
 (+)# Add the whole stack together
\$\endgroup\$
  • \$\begingroup\$ This probably has something to do with why it works. I can't quite tell what the stack is doing, though, because TIO seems to be running an older and significantly different version of Keg.py where line 67 is return self.content.pop(). \$\endgroup\$ – Unrelated String Sep 13 at 3:08
  • 1
    \$\begingroup\$ @UnrelatedString, I haven't asked Dennis to update Keg on TIO yet because we're still testing to see that I haven't broken everything by implementing functions \$\endgroup\$ – Jono 2906 Sep 13 at 22:46
2
\$\begingroup\$

><>, 24 20 bytes

\0r+n;
"

"
0
8
p
1

Try it online!

Uses the same trickery as my original 24 byte answer, just outputs 1 and 2 instead of 2/4.


\0r+n;
"

"
0
a
p
1
1
+

Try it online!

And try the double!

This starts at the \, diverts code execution down, does its trick*, pushes two 1's, then wraps around again. It hits the \ again, but from a different angle now, so the 0r+n gets executed and ; terminates the program.

(*) Note that the trick is to alter the source code by turning the instruction at 0, 10 into a space instead of a \ when the code is doubled.

When the code hits n; (print number and quit), the top of the stack is either 2 or 2, 2. In each case, a 2 gets printed, So we need to add 2 and 2 in case of the double source code, but we don't have a second stack item on the regular run. This is solved by 0r: push a zero and reverse stack. We now either sum 2 and 2 and ignore the (now bottom) 0, or we add 2 and 0.

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

Vim, 5 bytes

i0<esc><C-a>:

Since V is backwards compatible, you can Try it online! or Try it doubled!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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