Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Inspired by this question on SO, your task is to produce a program that is valid in (at least) two major versions of your chosen language that produce different output.

Rules

  • Any language that has more than one major version can be used.
    • For the purposes of this challenge, I would suggest a "major version" is where the first number in the version number changes.
      • PHP 4 and PHP 5 are different, PHP 5.3 and PHP 5.4 are not.
    • However, since I don't know the versioning scheme for all languages, if you can argue your case well enough, I'm sure the community will determine if you have been fair in determining "major version" yourself.
  • The code must be compiled and run with the same flags and input each time
    • Except to change the version of the language if this is pertinent
  • Errors don't count as output and answers that produce errors are disqualified (rather than error output is just ignored)
  • The program should take no input other than that required to make it run.
  • For each given version, the output should always be the same
  • The intention is that the change should be a consequence of a change in the language spec rather than the VM spec or environmental metadata

Scoring

  • type scoring for length, so +1 for each char/byte
  • -1 for each character difference in the length of the output.
    • e.g. Version 1 outputs abcde (5 characters), Version 2 outputs 123abc (6 characters) = -1

Other Rules

  • Standard exceptions apply - no external programs,web requests etc.
  • Your program should terminate (within 2 seconds)
  • Lowest score wins.

"Better" Scoring

Keep your original answers, in the interest of fairness, I'll mark the winner based on the original rules.

Since my original scoring is fundamentally broken, why not re-score / retry with the following scoring system:

  • type scoring for length, so +1 for each char/byte
  • +1 for each character difference in length of output
    • abcde and 123456 -> +1
  • -1 for each unique character difference in the output (capped to length of shortest output)
    • abcde and 123456 -> -5
    • 12345 and 123456 -> -1
    • 12345 and 123455 -> 0
  • Scores closest to zero win
  • In the event of a second tie break, simple score wins.
share|improve this question
    
Note: in Ruby, the first number is the epoch, it signifies major events in Ruby's history, not a version number (0->1 was the initial release, 1->2 was Ruby's 20th birthday). The major number is the second number. So, Ruby 1.8->1.9 would be crossing a major release. –  Jörg W Mittag Apr 16 at 12:43
1  
I think the new scoring is too different from the original, it's practically a new question –  Tal Apr 16 at 14:14
    
I'd say the PHP example is arguable, 5.4 has quite a few basic features that would break on 5.3 (traits, []arrays, array dereferencing). It was going to be PHP6, but then decided to save the 6 to something more radical as unicode strings by default IIRC –  Einacio Apr 16 at 15:24
2  
What is the unique character difference? If I output zzzz and aaaa, does that give me -4? That's what it seems like to me. –  Quincunx Apr 16 at 17:29
1  
So, assuming that the program can detect in which version it runs, the winner is who can output the most characters in two seconds? I think this question would be better as a popularity-contest to encourage people to find interesting and subtle language bugs. –  Cephalopod Apr 17 at 9:03

23 Answers 23

up vote 30 down vote accepted

Revised answer for "better" scoring system

C89 / C99, Score: 0

My program is 52 characters long and uses the same mechanism as in my original answer to achieve the different output. This works because C89 doesnt treat // as a comment:

i=32;main(){putchar(i+++0//**/
+52)&&i<84&&main();}

The results:

$ ./diff2c89
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRS
$ ./diff2c99
TUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂Çüéâäàåç
$ ./diff2c99 | wc
      0       1      52
$ ./diff2c89 | wc
      0       1      52
$ wc diff2.c
      1       2      52 diff2.c

Old answer:

C89 / C99, Score: -Infinity?

I'm not entirely sure if this program doesn't break the rules, but never mind. This program exploits the fact that in C89 // is not a valid comment but /* ... */ is.

Using the comment trick an other function is executed. In C89 the function just prints "trolololol..." until the stack overflows (so it might finish within 2 seconds).

f1(){printf("ol");f1();}
f2(){printf("oll");}
main(){
    printf("tr");
    void (*f[])() = {f1,f2};
    f[0 //* trollololol */
      +1]();
}

C99

$ ./diffc99
troll

C89

$ ./diffc89
trolololololololololololololololololololololololololololololololololololololololololo
lolololololololololololololololololololololololololololololololololololololololololol
ololololololololololololololololololololololololol ....
share|improve this answer
1  
I'm not sure either whether it's against the rules, but I like it :) –  Tal Apr 16 at 12:33
7  
According to the "better rules", Scores closest to zero win so this is actually very far from zero. –  user80551 Apr 16 at 18:02
1  
Indexing an array of function pointers by either division or addition depending on the C standard's definition of comments... beautiful. –  Desty Apr 17 at 10:46

Python - 10 points less than the next best answer

print(range(100))

In Python 2, this will print the entire list of integers from 0 to 99.

In Python 3, range is a generator and so it will print only "range(0,100)".

Seeing as I've never run into a size limit on numbers in Python, I can replace that 100 with a much bigger number (2**1000, for example) and end up with a virtually infinite difference in the output.

Edited to reflect the fact that, while I can get a score that is infinitely low for any practical purpose, I cannot reach actual infinity with a program that terminates under 2 seconds

For the updated tie-breaker scoring system, I'd submit:

print(range(4))

Output:

Python 2: [0, 1, 2, 3]

Python 3: range(0, 4)

The first print has 5 unique characters ([123]), the second print has 8 unique characters (range(4)), the difference in length of output is 1, the code has 15 characters, the shortest output is 11 characters... these rules are pretty confusing but I think this brings me to a final score of 15+1-min(11,5+8) = 5.

share|improve this answer
    
However, "your program should terminate", even on a very (finitely) fast machine, your "much bigger" (finite) number is still nowhere near -∞ so I dispute your score (give or take ;] ) –  James Webster Apr 16 at 10:31
    
@JamesWebster Thank you for bringing this terrible technical mistake to my attention, I have edited my answer accordingly ;) –  Tal Apr 16 at 10:36
    
Other similar answers will surely come. I guess that scoring for these answers should be calculated on how fast they get to infinity. –  Vereos Apr 16 at 10:38
    
@Vereos I'd hope that, if other answers of this sort show up, the question will be edited with additional scoring details. –  Tal Apr 16 at 10:44
1  
@Tal I think everyone who reads the scoring rules will make up a solution that can produce negative scores, only limited by the language or hardware, before having read any answer to the problem ;) The scoring is just broken by design... –  foobar Apr 16 at 13:09

Python - 0 points

No idea how this one works :P Just stumbled upon it while trying out random code.

int

On Python 3, it's <class 'int'> and on Python 2, it's <type 'int'> (using interative console)
"Better" Score: 3 (length) + 1 (char diff.) - 4 (unique chars)

Older Python 1 - 7 points

print()

Big thanks to @grc for this version and helping me subtract four points !

In Python 2, this statement is interpreted as print () which prints the empty tuple ().
In Python 3, the print is a function and results in nothing being printed.
"Better" Score: 7 (length) + 2 (char diff.) - 2 (unique chars)

Older Python 2 - 13 points:

print(1,2)

"Better" Score: 12 (length) + 2 (char diff. o/p) - 1 (unique chars o/p)

I know this isn't going to win but still gave an answer, as this is my first Python try :)

share|improve this answer
    
I think this could be shortened to print(). –  grc Apr 16 at 12:39
    
@grc Thanks a lot for the advice ! Looking back at my compiler, I see that I had tried it, but was so lost in the docs, that I deleted it to try dict.itertools() :P –  Gaurang Tandon Apr 16 at 12:46

C#

I also changed the generic method type inference algorithms between C# 2, 3 and 4. For example:

using System;
class C
{
  static int M<T>(T x, T y) { return 1; }
  static int M(object x, object y) { return 2; }
  static void Main() 
  {
    Console.WriteLine(M(1, new int?()));
  }
}

In C# 2 method type inference says that T cannot be both int and int?, and so produces 2. In C# 3 method type inference says "the best compromise between int and int? is int?" and so chooses M<int?> and produces 1.

share|improve this answer
4  
> I also changed What do you mean you changed the... oh. OH. –  Bob Apr 20 at 6:00

Ruby, 4 characters + 0 char length difference - 3 unique char difference = score of 1

p ?d

On Ruby 1.9, it will print "d". On 1.8, it prints 100.

Explanation: ?d is "d" in 1.9 and 100 (the ASCII code for d) in 1.8. p x is equivalent to puts x.inspect. * is both string repetition and multiplication.


"Optimized" version for old scoring:

Ruby, 8 characters - 999999989 chars difference = score of -999999981

p ?!*1e9

Prints 33000000000.0 for 1.8 and "!!!!!!...!!!" for 1.9. (?! is 33 in 1.8 and "!" in 1.9, and * is both string repetition and multiplication.)

Really, you could go as far as you wanted with the multiplication, it just depends on how fast your computer is.

share|improve this answer
    
Could you explain what exactly is happening in that second line? –  Tal Apr 16 at 13:22
    
@Tal Okay, edited –  Doorknob Apr 16 at 13:28

Bash — -∞ (up to practical limits)

Effectively, however much memory you have. E.g. with about 10GB:

echo {0..999999999}

Bash 2: doesn't support ranges in brace expansion, so prints {0..999999999}.

Bash 3:


Any language — -∞ (up to practical limits)

You'll have this in pretty much any language, even if it ends up being a little more complex. As soon as you can make two different values, you can write code that produces arbitrarily different output. A better scoring method would ignore the differences in the output.

version = … # some arbitrarily weird stuff
if version = 2:
    while not timed_out():
        print "version2"
share|improve this answer
    
@foobar The in the “any language” version stands for whatever tricks you use to make your program depend on achange in the language spec. It is version detection, whether you do if $version >= 3 or if isinstance(range(0),list). –  Gilles Apr 16 at 13:02

C#

Following code would produce the different output for C# 5.0 and previous versions of C#

using System;
using System.Collections.Generic;

namespace TestConsoleAppClosure
{
    class Program
    {
        static void Main(string[] args)
        {
            var actions = new List<Action>();
            List<int> list = new List<int> { 10, 20, 30, 40 };
            foreach (var item in list)
            {
                  actions.Add(() => Console.WriteLine(item));
            }
            foreach (var act in actions) act();
        }
    }
}

Output: C# 5.0

10
20
30
40

Output: C# 4.0

40
40
40
40

The reason is explained in the blog post by Eric Lippert

Closing over the loop variable considered harmful

share|improve this answer
1  
@ace, thanks for the edit –  Habib Apr 16 at 21:38

Python, -14 Points ( 3 - 17 char length difference = -14)

2/3

Python 2 outputs: 0

Python 3 outputs: 0.6666666666666666

Better Scoring version, 5 points (3 + 2 char length difference = 5)

3/2

Python 2 outputs: 1

Python 3 outputs: 1.5

share|improve this answer

C#

I added covariance and contravariance to C# 4, so programs of the form:

using System;
using System.Collections.Generic;
class C
{
  static void Main() 
  {
    Console.WriteLine((new List<string>()) is IEnumerable<object>);
  }
}

Would produce false in C# 2 and 3 and true in C# 4.

However, one might argue that this does not count because the library containing the definition of IEnumerable<T> also had to change.

share|improve this answer

C++98/11 - "Better" Scoring (115 characters - 115 unique character differences in output = score of 0)

A slightly edited version to comply to the new scoring rules

Golfed:

#include<cstdio>
#define u8 "\x0B"
int main(){int i=116;char c[i];c[--i]=0;while(i-->0)c[i]=u8"\x7E"[0]+i;puts(c);}

Ungolfed version:

#include <cstdio>
#define u8 "\x0B"
int main() {
    int i = 116;
    char c[i];
    c[--i] = 0;

    while(i-- > 0)
        c[i] = u8"\x7E"[0] + i;

    puts(c);
}

The new solution doesn't differ much to the old one. In the new solution the output in both C++11 and C++98 is with 116 characters equally long, but the only character they have in common is the new line character appended by the puts function.

For C++98 the u8 in u8"\x7E"[0] will still be replaced, but now with "\x0B". So the resulting value after preprocessing will be "\x0B""\x7E"[0]. The two strings will be concatenated to "\x0B\x7E" and the subscript operator will access the first element, in this case the character with the value 11 in the character encoding. Additionally the value of i will be added, which intially is 114. So the character with the value 125 will be written to the resulting array. As i goes to zero all values from 125 to 11 will be written to the array and puts will print all characters with the values from 11 to 125, plus the trailing new line.

In C++11 u8"\x7E"[0] will be interpreted as an UTF-8 string consisting of the single character with the hexadecimal value 7E. The subscript operator will now access this character and the value of i is added to it, resulting in the decimal value 241 during the first iteration. While i goes to zero, all the values down to 126 will be written to the array and puts will print the characters with the values from 126 to 241, plus the trailing new line.

Depending on the used character set, this will produce different results, as most character sets only have the first 128 characters in common.

For ISO-8859-2 the output would be the following:

C++98: Output for C++98

C++11: Output for C++11

C++ (106 characters - 107 difference in output = score of -1) (OLD RULES)

Golfed:

#include<cstdio>
#define u8 "f"
int main(){int i=108;char c[i];c[--i]=0;while(i-->0)c[i]=u8""[0];puts(c);}

Ungolfed version:

#include <cstdio>
#define u8 "f"

int main() {
    int i = 108;
    char c[i];
    c[--i] = 0;

    while(i-- > 0)
            c[i] = u8""[0];

    puts(c);
}

Compiled with g++ -std=c++98 main.cpp and g++ -std=c++11 main.cpp.

Actually you can replace 108 with any positive number in integer range to achieve negative scores. As long as it's bigger than 108 ;)

In C++98 #define u8 "f" will cause the preprocessor to replace u8""[0] with "f"""[0]. This will result in "f"[0], which finally becomes the single character 'f', which is written to an array.

puts(c) will print the resulting array, consisting of i-1 'f'.

In C++11 u8""[0] will cause the empty string to be interpreted as an UTF-8 string, so no string concatenation is done. As this is a C-string, the subscript operator will access the terminating null-byte and write it to an array.

In the end puts(c) will print the resulting array, which consists only of null-bytes. But as puts stops reading the input as soon as it encounters a null-byte, it will only print a newline and nothing more.

share|improve this answer
    
I get your score to be 1. Both c++89 and c++11 output a ? (on my (Mac) system at least) –  James Webster Apr 16 at 16:55
    
Though I only count 115 characters in the solution. –  James Webster Apr 16 at 16:58
    
@James Webster true, wc -c lied to me ;) I'll append a newline to the end of my solution ;) ...Or just adjust it. –  foobar Apr 16 at 17:03
    
@James Webster I guess that you're using UTF-8 encoding in your terminal. As the most significant bit in a byte is reserved in UTF-8, it can't map the characters the program outputs in the range from 128 to 232. So you should see up to 104 '?' or less with binary trash in between. With WINDOWS-1256 encoding as an example, your terminal would display this for the C++11 version: tuvwxyz{|}~€پ‚ƒ„…†‡ˆ‰ٹ‹Œچژڈگ‘’“”•–—ک™ڑ›œ‌‍ں ،¢£¤¥¦§¨©ھ«¬­®¯°±²³´µ¶·¸¹؛»¼½¾؟ہءآأؤ‌​إئابةتثجحخدذرزسشصض×طظعغـفقكàلâمنهو –  foobar Apr 16 at 17:19
    
For the C++98 version it's possible that your terminal displays '?' for values below 32 as that are control characters. My terminal for example replaces most of them with a square and their hexadecimal value printed into it. Except for the new line and tab characters as examples. But now that I mention it myself. the C++98 version contains new line twice, but that's easy to fix ;) –  foobar Apr 16 at 17:24

CSS2 vs CSS3 48 points

<i style='font-feature-settings:"smcp" on;'>abcdefghijklmnopqrstuvwxyz</i>

Rendered as ABCDEFGHIJKLMNOPQRSTUVWXYZ (small caps) on CSS3 browsers

Rendered as abcdefghijklmnopqrstuvwxyz on non-CSS3 browsers

74 chars - 26 unique chars difference = 48

share|improve this answer
    
But this is an error in CSS2. I thought errors didn't count. –  Mr Lister Apr 19 at 11:36
    
@MrLister AFAIK font-feature-settings has been introduced in CSS3 dev.w3.org/csswg/css-fonts/#propdef-font-feature-settings –  Toni Toni Chopper Apr 21 at 14:22

Perl, 24 characters - (9*(10^9))-1 char difference = score of -((9*(10^9))-1)+24

print$]>=5?"a":"a"x9e9;

Prints 9e9-times a for all versions below 5, prints a for all versions above 5. You could make the score infinitely low by just adding more as to the second output.

share|improve this answer
2  
In the question: "The intention is that the change should be a consequence of a change in the language spec rather than the VM spec or environmental metadata" –  Doorknob Apr 16 at 12:11
    
I'm not using any VM spec or environmental metadata. Just the installed version. –  Vince Apr 16 at 12:35

Befunge, 36 - 378 = -342; 164 - 2576 = -2412

"v
"<v
" <v
"  <v
"   <v
 <v:,<
 ^_@

In Befunge 93, this would output 3 spaces, followed by <v, followed by 76 spaces, followed by <v, then 76 space, then <v, then 76 spaces, then <v followed by 77 spaces, then v followed by 78 spaces. Length: 3 + 2 + 76 + 2 + 76 + 2 + 76 + 2 + 77 + 1 + 78 = 395This is trivially extendable by adding extra lines similar to the first 5 lines.

In Befunge 98, this would output <v <v <v <v v.

The difference in length: 395 - 17 = 378. So the score would have been (by the old rules): -342

Note: I could have gotten an even bigger difference if I used . instead of ,; the difference would have been -684


Rule change:

This is a bit more tricky.

"  "-v>"Befunge 93 very long strings"v>"F"0g" "1-`!#v_   "F"0g1-"F"0pvz
     _^p0"F"-1g0"F"_v#    `-1" "g0"F"<^"j++a81zzzzzz]zzzzzzzzzzzzzzz"<
             _@#`0:,<

Befunge 93 output:

sgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeBsgnirts gnol yrev 39 egnufeB

Befunge 98 output:

j++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzzj++a81zzzzzz]zzzzzzzzzzzzzzz

Lengths: 2576. None of the characters between the strings are the same, so if I understood the challenge correctly, my score is 164 - 2576 = -2412 (yes I was supposed to aim for 0, but this was more fun). If I need to make it so that each character in each string are unique and different from each other, I can do so, please tell me.

share|improve this answer

Powershell, "Better" scoring, -163 (15 - 178 char diff = -163 )

$PSVersionTable

Powershell V2

Name                           Value                                                                   
----                           -----                                                                   
CLRVersion                     2.0.50727.5477                                                          
BuildVersion                   6.1.7601.17514                                                          
PSVersion                      2.0                                                                     
WSManStackVersion              2.0                                                                     
PSCompatibleVersions           {1.0, 2.0}                                                              
SerializationVersion           1.1.0.1                                                                 
PSRemotingProtocolVersion      2.1                                                                     

Powershell V3

Name                           Value                                                                   
----                           -----                                                                   
WSManStackVersion              3.0                                                                     
PSCompatibleVersions           {1.0, 2.0, 3.0}                                                         
SerializationVersion           1.1.0.1                                                                 
BuildVersion                   6.2.9200.16398                                                          
PSVersion                      3.0                                                                     
CLRVersion                     4.0.30319.1022                                                          
PSRemotingProtocolVersion      2.2 
share|improve this answer

PHP, Score: 0 (best case)

srand(2);echo rand();

Wow, this is going to be fun to explain.

According to this website, the srand() function appears to be broken from PHP 5.1.5 to PHP 5.3.14. Therefore, we are going to keep under consideration PHP 4.4.9 and one random version of PHP 5 that falls in the version interval specified above.

PHP 4.4.9 output: 1505335290

I don't think this is rule-breaking; Since this appears to be a bug, the output should be the same, but it's not. Our other PHP version will simply skip the srand() function and output a random number.

share|improve this answer

Java (around -2.000.000.000)

The Java versions are sometimes called 1.x, but I think it still is within the rules.

The easy way is to check whether a class exists that was introduced in a specific version.

try {
    Class.forName("java.lang.AutoCloseable");
    // Java 7 or later
    char[] text = new char[Integer.MAX_VALUE];
    Arrays.fill(text, 'a');
    System.out.println(new String(text));
} catch (Exception e) {
    // Java 6 or earlier
    System.out.println("-");
}

(Depends a bit on your terminal whether it is possible to output 2 billion characters in two seconds / For the new scoring, replace Integer.MAX_VALUE with the byte count of the program to achieve perfect zero score.)

This code depends on the version of the VM/JDK that is used (does that count?)

import java.lang.reflect.Field;
import java.util.Arrays;

{
    Field fValue = null;
    for (Field f: String.class.getDeclaredFields()) {
            if (f.getName().equals("value")) {
                    fValue = f;
            }
    }
    char[] text = new char[10];
    Arrays.fill(text, 'a');
    String s1 = new String(text);
    String s2 = s1.substring(1);
    fValue.setAccessible(true);
    text = (char[]) fValue.get(s2);
    Arrays.fill(text, 'z');
    System.out.println(s1);
}

It prints zs for Java 6 and earlier JDKs and as for recent versions.

share|improve this answer

JavaScript (ES3 vs ES5) - 9 points

length 10 + length difference 0 - output difference 1

[].map?1:0

Outputs 1 for modern browsers that support Array.prototype.map. Outputs 0 on older browsers. I tested this with IE8.

With old rules: 0 points

length 26 - length difference 26

Array([].map?27:0).join(0)

Outputs 00000000000000000000000000 on modern browsers. And empty string on old.

share|improve this answer
    
IE8 does really support ES4??? –  Bergi Apr 18 at 18:32
    
I rather was surprised how they did implement a non-existing standard –  Bergi Apr 18 at 18:40
    
Whoops! Fixed :) –  nderscore Apr 18 at 18:53

Python - 0

a='a=%r\ntry:print a%%a\nexcept:pass'
try:print a%a
except:pass

Python 2 prints a quine, while Python 3 prints nothing.

EDIT: Updated, fixed.

share|improve this answer
    
Can your computer really print ~9 billion digits in 2 seconds? –  James Webster Apr 21 at 8:19
    
@JamesWebster This is actually the return value of a function - thus, it only needs to exist in memory for the program to finish. It requires a /lot/ of RAM, but it would finish within 2 seconds on a well-specced computer. –  cjfaure Apr 21 at 9:10

APL (5 - (1988894 - 1) = -1988888)

In old-style APLs (like Dyalog if ⎕ML=0*), means mix, which, on a 1-dimensional vector does nothing. In APL2-style APLs, like GNU APL, (or Dyalog if ⎕ML=3), means first, which takes the first item of a vector.

Thus, the following 5 bytes (the APL charset does fit in a byte),

↑⍳3e5

will output 1988894 bytes (the space separated list of numbers from 1 to 3e5) in old-style APL dialects,

and 1 byte (just the first number in said list, which is 1 and therefore of length 1), in APL2-style APL dialects.

Notes:

  • ⎕ML means migration level. In Dyalog APL, the higher you set ⎕ML, the more APL2-style features are enabled. It defaults to 0. (And it's a global variable! Fun!)
  • 3e5 was the highest 3-character value Dyalog APL would accept with . 4e5 gave me a LIMIT ERROR. This restriction is probably interpreter-dependent. (GNU APL had no trouble with higher values.)
share|improve this answer

Bash 7 (14 bytes program length + 0 difference in output length - 7 difference in unique chars in output)

Related to @Gilles answer, but a different expansion feature and different versions. Scoring according to the edited question:

echo {1..9..2}

Output for bash 3.x:

{1..9..2}

Output for bash 4.x:

1 3 5 7 9
share|improve this answer

PHP: −134217684 (43 - 134217727)

echo str_pad("",ip2long("")&0x7ffffff,"a");

Usage:

time php -r 'echo str_pad("",ip2long("")&0x7ffffff,"a");' > /tmp/test
1.61user 0.17system 0:01.79elapsed 99%CPU (0avgtext+0avgdata 142272maxresident)k
0inputs+0outputs (0major+35922minor)pagefaults 0swaps

In PHP5+ this will print nothing since ip2long with invalid argument turns into false which casts to zero. In PHP4 it ip2long("") returns -1 and we pad the empty string with 128MB og a.

The mask is fitted so that it returns long before the 2 seconds on my machine. If you can't make it in 2s buy better hardware!

With new rules: 0 (40 - 40. You can't get any closer to zero.)

echo str_pad("",40,chr(97+ip2long("")));

Outputs:

In PHP4: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 
In PHP5: bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
share|improve this answer
    
With the new scoring rules your score is 39. Both outputs are equally long, so no penalty for that. But there's only one unique character difference. a is not equal to b, so you are allowed to subtract -1 from your score. –  foobar Apr 17 at 8:58

C89/C99 comment exploit, 45 character, 0 score

main(a){while(++a<47)putchar(79-a//**/~0
);}

c89 output

QRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|} - 45 char.

c99 output

MLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#"! - 45 char.

share|improve this answer
    
This in fact uses exactly the same logic as the other C answer. –  ace Apr 16 at 18:43
    
I mean, considering the fact that there is already an answer of this question using the exact same language and the exact same logic, this answer is not very interesting. –  ace Apr 16 at 19:06
    
This entry is superior to the initial 'other C answer' on multiple levels. It's shorter, scores perfect, completes in under two seconds, and doesn't crash or abort. As far as the updated version... in fairness you should probably leave a comment linking this code. –  Johnny Cage Apr 18 at 21:14
    
Your answer is better than the "initial" other C answer because that other C answer was submitted BEFORE the rule change. –  ace Apr 18 at 23:25
    
Maybe, but the updated version rips my implementation. Not only does it use the same offsets and switch from printf to putchar; most damning is that for r5 I misread the rules and accidentally combined new and old scoring. Output for [r5,r6] is an error. r3 in the other C answer contains the exact same error. If you look at the timestamps, you'll see it was r5 that was posted here when the other answer updated. Not that it matters, since this is code golf, and this entry satisfies the challenge in fewer characters, period. –  Johnny Cage Apr 19 at 15:57

C++98/C++11

#include <iostream>

int main()
{
  for (long i = 0; i < __cplusplus; ++i)
     std::cout << "x";
}

For a standard conforming C++98 compiler, this outputs 199711 times the letter 'x', while for a standard conforming C++11 compiler, this outputs 201103 times the letter 'x'. The length difference of the output is therefore 1392 characters. This means that actually golfing the source code is not worthwhile, since a much larger effect can be achieved by just replacing "x" with a longer string, or by multiplying __cplusplus with some number.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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