CJam, 1051 827 643 569 545 407 327 279 235 233 229
''"','#'C'J'a'm',' qYew::-Yf#:+e#'''''''''"
f{-ci'(*''2*\}'^,40>"*/:N"-"][ZZ[\^__`bcdgimpstsz{}~~~"
The above program generates the actual source code, which is 1,179,112 bytes long.
Testing
Using the Java interpreter, the source code can be generated and tested like this:
$ alias cjam='java -jar cjam-0.6.5.jar'
$ cjam gen.cjam > diff.cjam
$ cksum diff.cjam
896860245 1179112 diff.cjam
$ cjam diff.cjam < diff.cjam
#CJam, 229
Alternate version
At the cost of 36 points – for a final score of 265 – we can make the source code 99.92% shorter:
'''()))))(''''(((('''())))))))))))))))))))))))))))('''()))))))))))))))))))))))))))))))))))('''())))))))))))))))))))))))))))))))))))))))))))))))))))))))))('''())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))('''()))))(''''((((((('())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))('())))))))))))))))))))))))))))))))))))))))))))))))))('())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))('())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))('()))))))))))))))))))('()))))))))))))))))))('())))))('())))))))))))))))))))))))))))))))))))))))))))))))))('()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))(''(((('()))))))))))))))))))('())))('())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))(''((((''''''''''''''''''()+,-.0123456789;<=>?@ABCDEFGHIJKLMOPQRSTUVWXYZ[\]][ZZ[\^__`bcdgimpstsz{}~~~
You can try this version online in the CJam interpreter.
Idea
We want to execute the code
'#'C'J'a'm',' qYew::-Yf#:+
keeping the score as low as possible. To achieve this, we're going to build that string character by character (with a few no-ops before and after) and evaluate the result.
Fortunately, ' (push character literal), ( (decrement) and ) (increment) are consecutive ASCII characters, so pushing arbitrary characters is relatively inexpensive.
ASCII characters after ' can be pushed as '()…)(, where the number of ) depends on the code point.
For example, + can be pushed as '())))(. The distance between ' and (, and ( and ) is 1. The trailing )( cancel each other; their only function is to pave the way for the following ' (corresponding to the next character) with consecutive characters.
Characters pushed in this fashion will raise the score by 4 points.
ASCII characters before ' can be pushed as ''(…(, where the number of ( depends on the code point.
For example, # can be pushed as ''((((. The distance between ' and ( is 1.
Characters pushed in this fashion will raise the score by 2 points.
''(…( actually works for all ASCII characters, since Character is 16 bits wide and wraps around. For example, + can be pushed as '', followed by 65,532 (s.
This technique is used in the 1.2 megabyte version of the code.
The character ' can be pushed as '', leaving the score unaffected.
Code
e# Push these characters on the stack: ','#'C'J'a'm',' qYew::-Yf#:+e#'''''''''
''
'()))))(
''
''((((
''
'())))))))))))))))))))))))))))(
''
'()))))))))))))))))))))))))))))))))))(
''
'())))))))))))))))))))))))))))))))))))))))))))))))))))))))))(
''
'())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))(
''
'()))))(
''
''(((((((
'())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))(
'())))))))))))))))))))))))))))))))))))))))))))))))))(
'())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))(
'())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))(
'()))))))))))))))))))(
'()))))))))))))))))))(
'())))))(
'())))))))))))))))))))))))))))))))))))))))))))))))))(
'()))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))(
''((((
'()))))))))))))))))))(
'())))(
'())))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))(
''((((
''
''
''
''
''
''
''
''
''()
+ e# Concatenate the two topmost single quotes.
, e# Push the length of the resulting string (2).
-.0123456789 e# Push that number.
; e# Discard it from the stack.
< e# Compare a single quote with 2. Pushes 0.
= e# Compare a single quote with 0. Pushes 0.
> e# Compare a single quote with 0. Pushes 1.
? e# Ternary if. Discards a single quote and 1.
@ e# Rotate the remaining three single quotes.
ABCDEFGHIJKLMOPQRSTUVWXYZ e# Push 25 items on the stack.
[\] e# Swap the last two and wrap them in an array.
e# So far, we've pushed the elements of "','#'C'J'a'm',' qYew::-Yf#:+e#'''"
e# followed by the elements of [10 11 12 13 14 15 16 17 18 19 20]
e# and ["" "" "" 3.141592653589793 "" "" " " 0 0 0 -1 1 [3 2]].
] e# Wrap the entire stack in an array.
[ e# Begin an array. Does nothing.
ZZ e# Push 3 twice.
[ e# Begin an array. Does nothing.
\^ e# Swap both 3s and push the bitwise XOR. Pushes 0.
__ e# Push two copies.
` e# Inspect the last copy. Pushes the string "0".
b e# Convert "0" from base 0 to integer. Pushes 48.
cd e# Cast 48 to Character, then Double. Pushes 48.0.
gi e# Apply the sign function, then cast to integer. Pushes 1.
mp e# Check 1 for primality. Pushes 0.
s e# Cast the result to string. Pushes the string "0".
e# We now have three elements on the stack: an array, 0, and "0"
t e# Set the element at index 0 of the array to the string "0".
s e# Cast the array to string.
e# This pushes the string consisting of the characters
e# 0,'#'C'J'a'm',' qYew::-Yf#:+
e# and
e# e#'''10111213141516171819203.141592653589793 000-1132
e#
e# When evaluated this does the following:
e# 0, Push an empty array. Does not affect output.
e# '#'C'J'a'm',' Push the characters of "#CJam, ".
e# q Read all input from STDIN.
e# Yew Push the overlapping slices of length 2.
e# ::- Reduce each pair of characters to their difference.
e# Yf# Square each difference.
e# :+ Add the results.
e# e#… Comment. Does nothing.
z e# Zip. This wraps the string on the stack in an array.
{}~ e# Execute an empty block.
~ e# Unwrap the array.
~ e# Evaluate the string.
Can we assume that input is at least two characters long? – lirtosiast – 2015-09-29T17:45:06.500
Yes, unless you create a 1-byte entry. – Sanchises – 2015-09-29T17:46:01.140
I added a script that should calculate the score accurately, feel free to remove it if you don't like it – FryAmTheEggman – 2015-09-29T17:52:42.387
@FryAmTheEggman Thanks! I didn't really feel like looking up how to make a runnable stack snippet, but this works just as well. – Sanchises – 2015-09-29T18:47:25.400
1Will the program have to interpret anything other than its own code? And just to clarify, two consecutive equal characters are a value of 0? – Daniel M. – 2015-09-29T19:01:50.057
@DanielM. It should be capable of arbitrary strings - and the output would be just the same (except the score), since our sysadmins will want to see how languages perform on their testcases. And yes,
aawould result in a value of 0. – Sanchises – 2015-09-29T19:06:28.63010uh. – bopjesvla – 2015-09-29T19:08:22.807
1@bopjesvla Fine. Arbitrary strings, but you may assume them to be able to fit in the universe. Or on your computer, for that matter. – Sanchises – 2015-09-29T19:14:47.977
11
First person to create a valid answer in Unary wins!
– ETHproductions – 2015-09-29T19:24:49.9071@ETHproductions Yes, and zero upvotes will be given to an answer that requires zero skill to win. – Sanchises – 2015-09-29T19:27:09.283
They hooked the CPU data bus up to an Attack Damage Carry? Nice! – mbomb007 – 2015-09-29T21:09:06.283
1@mbomb007 Oops, wrong acronym. I meant the Danish Architecture Centre, – Sanchises – 2015-09-29T21:14:47.323
3Too late now, but one option to prevent Unary-style answers would have been to define the difference between equal characters as 1. Also would have made the logic slightly more interesting. – Reto Koradi – 2015-09-29T23:46:44.730
FWIW a 60Hz 1920*1080 32-bit monitor carries more than ten thousand times the baud rate of a stereo 192 kbps speaker system. – imallett – 2015-09-30T04:03:35.910
@FryAmTheEggman Your script does not seem to handle newlines in the input... – Sanchises – 2015-09-30T06:58:21.580
@sanchises If the best scoring answer requires 0 skill, that is usually a problem in the question not in the answer. You should defend against these kind of loopholes e.g. with RetoKoradi's suggestion in this case. – randomra – 2015-09-30T08:31:34.030
@randomra I know, but I feel that by the time that suggestion was made, it was too late to change the rules. – Sanchises – 2015-09-30T08:37:30.457
@sanchises True, I just mentioned it for the future. – randomra – 2015-09-30T08:52:13.723
@randomra Yeah, thanks - I quite honestly forgot about Unary and Lenguage. It would have prevented the CJam answer, too. Perhaps I'll make another challenge sometime with that scoring - or someone else will, I'm not copyrighting this scoring mechanism. – Sanchises – 2015-09-30T09:29:07.803
Your optional output format involving the newline and dash is somewhat confusing to me. Can you give an example? – Mwr247 – 2015-09-30T17:14:02.140
@Mwr247 It's just another way of formatting headers around here. It's literally
C++, 98\n-with of course\nan actual newline. – Sanchises – 2015-09-30T17:19:24.243Is it largest score or smallest score wins? – ASCIIThenANSI – 2015-09-30T19:29:36.977
@ASCIIThenANSI Read the start of the question. If you still can't figure it out, pick the one where you think you can do better than a bunch of monkeys mashing at a keyboard. – Sanchises – 2015-10-01T20:51:59.657