47
\$\begingroup\$

In as few Unicode characters as possible, print the following (144 characters):

1, 2, 3 o'clock, 4 o'clock rock,
5, 6, 7 o'clock, 8 o'clock rock,
9, 10, 11 o'clock, 12 o'clock rock,
We're gonna rock around the clock tonight.

You must not use any numbers in your code, except the digit 1.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ There are spaces at the end of first 3 lines, do those count? \$\endgroup\$ Commented Jul 17, 2014 at 12:46
  • \$\begingroup\$ @aditsu: These are due to editing, as m.buettner forgot to remove them when editing back to a code block. I would amend this, but the edit is considered too minor. \$\endgroup\$
    – Wrzlprmft
    Commented Jul 17, 2014 at 12:56
  • 1
    \$\begingroup\$ I removed them now \$\endgroup\$ Commented Jul 17, 2014 at 13:30
  • 4
    \$\begingroup\$ Several answers use 11. Does the instruction mean you can only use the number 1 or the digit 1? \$\endgroup\$
    – mfvonh
    Commented Jul 17, 2014 at 16:32
  • 10
    \$\begingroup\$ @mfvonh I said you could use 1. I didn't specify how many times, so the fact that people have used 11 is a clever loophole, and I'm all for it. \$\endgroup\$
    – rybo111
    Commented Jul 17, 2014 at 17:17

50 Answers 50

48
\$\begingroup\$

Javascript - 140 132

k=1,s=", ",t=" o'clock",x="";while(k<11)x+=k+++s+k+++s+k+++t+s+k+++t+" rock,\n";alert(x+"We're gonna rock around the clock tonight.")

The fact, that 11 is made out of two 1's let me pass the rules I guess.

Explanation

The k+++s is equal to (k++)+s, so it adds k first to x, then increments k and then it adds s to x. This process will be done three times before it adds the last sentence.

\$\endgroup\$
16
  • \$\begingroup\$ Why do you {} ? It's not needed for the while, you have only 1 instruction. But as you have to use ";" you will win only 1 char. ^^ \$\endgroup\$
    – antoinestv
    Commented Jul 17, 2014 at 13:18
  • 3
    \$\begingroup\$ What does k+++s do? Is it (k++) + s or k + (++s) or (k++) + (++s)? \$\endgroup\$
    – seequ
    Commented Jul 17, 2014 at 15:04
  • 31
    \$\begingroup\$ +1 for this nonsense: x+=k+++s+k+++s+k+++t+s+k+++t+u \$\endgroup\$
    – Cruncher
    Commented Jul 17, 2014 at 16:48
  • 2
    \$\begingroup\$ @trlkly usually golfing in JS you have to use an output statement and alert is shorter than console.log. Using the auto display of the last calculation in console is like cheating. \$\endgroup\$
    – edc65
    Commented Jul 18, 2014 at 5:03
  • 15
    \$\begingroup\$ Are you sure that's not Brainfuck? \$\endgroup\$ Commented Jul 20, 2014 at 15:58
37
\$\begingroup\$

C# - 186

Best golf language or best golf language?

class P{static void Main(){var s="";for(int i=1;i<11;)s+=i+++", "+i+++", "+i+++" o'clock, "+i+++" o'clock rock,\n";System.Console.Write(s+"We're gonna rock around the clock tonight.");}}
\$\endgroup\$
8
  • \$\begingroup\$ I wonder why it gained so many votes, too. People like non-golfing-friendly-language entries, maybe \$\endgroup\$ Commented Jul 17, 2014 at 14:02
  • 7
    \$\begingroup\$ He inspired me to do it in C++, so he gets a vote. Real Men(tm) don't use woosy interpreted languages for code golf! \$\endgroup\$
    – Darren
    Commented Jul 17, 2014 at 14:56
  • \$\begingroup\$ you don't actually need the spaces in the i++ + bits \$\endgroup\$
    – DLeh
    Commented Jul 17, 2014 at 15:23
  • \$\begingroup\$ I wrote it on dotnetfiddle and it didn't compile without the spaces. Thanks for pointing it. \$\endgroup\$ Commented Jul 17, 2014 at 15:42
  • \$\begingroup\$ You can shave a couple more by doing WriteLine -> Write, and using a literal \n at the end of the string. \$\endgroup\$
    – Alconja
    Commented Jul 18, 2014 at 9:54
27
\$\begingroup\$

Brainfuck (1574)

Not a serious submission, but follows the rules.

-[----->+<]>--.-----.------------.[-->+++<]>++.------.------------.++[-->+++<]>.[--->++<]>--.+++++[->+++<]>.[--->+<]>++.-[--->+<]>+.+++++++++.+++.------------.++++++++.+++[----->++<]>.------------.++[-->+++<]>+.-[--->++<]>--.+++++[->+++<]>.[--->+<]>++.-[--->+<]>+.+++++++++.+++.------------.++++++++.-[++>---<]>+.---[----->++<]>.---.------------.++++++++.+++[----->++<]>.>++++++++++.-[----->+<]>.---------.------------.-----[->++<]>.----------.------------.----[->++<]>-.+[-->+<]>++++.+++++[->+++<]>.[--->+<]>++.-[--->+<]>+.+++++++++.+++.------------.++++++++.+++[----->++<]>.------------.----[->++<]>.[-->+<]>++++.+++++[->+++<]>.[--->+<]>++.-[--->+<]>+.+++++++++.+++.------------.++++++++.-[++>---<]>+.---[----->++<]>.---.------------.++++++++.+++[----->++<]>.>++++++++++.+[->+++++<]>++.-------------.------------.[-->+++<]>+.-.----.------------.[-->+++<]>+..-[--->++<]>.+++++[->+++<]>.[--->+<]>++.-[--->+<]>+.+++++++++.+++.------------.++++++++.+++[----->++<]>.------------.[-->+++<]>+.+.--[--->++<]>.+++++[->+++<]>.[--->+<]>++.-[--->+<]>+.+++++++++.+++.------------.++++++++.-[++>---<]>+.---[----->++<]>.---.------------.++++++++.+++[----->++<]>.>++++++++++.[------>+<]>.++[->++++<]>+.---[->+++<]>+.-[->+++<]>.-------------.--[--->+<]>-.++[->+++<]>+.++++++++.-..-------------.-[->+++<]>.---[----->++<]>.---.------------.++++++++.-[++>---<]>+.[->+++<]>+.--[--->+<]>---.---.++++++.-------.----------.-[--->+<]>-.---[->++++<]>.------------.---.--[--->+<]>-.+[->+++<]>.+++++++++.+++.------------.++++++++.-[++>---<]>+.---[->++++<]>.-----.-.-----.--.+.++++++++++++.[++>---<]>.

Can be tested here.

\$\endgroup\$
2
  • 12
    \$\begingroup\$ Brainfuck answers always deserve respect. \$\endgroup\$
    – Pharap
    Commented Jul 19, 2014 at 11:37
  • 1
    \$\begingroup\$ Perhaps we should give him the "longest answer" award? #RESPECT \$\endgroup\$
    – Jamie
    Commented Jul 22, 2014 at 13:31
23
\$\begingroup\$

Ruby, 100

printf"%i, %i#{", %i o'clock"*r=-~1} rock,
"*-~r+"We're gonna rock around the clock tonight.",*1..$$
\$\endgroup\$
3
  • 4
    \$\begingroup\$ You can save another few characters with s="%i, %i#{", %i o'clock"*-~1} rock,\n" (replace the \n with a literal linebreak). Great idea to use $$! \$\endgroup\$
    – Ventero
    Commented Jul 17, 2014 at 19:04
  • 3
    \$\begingroup\$ Expanding on my previous comment: If you assign r=-~1 inline, you can then use "..."*-~r instead of s+s+s, allowing you to drop the assignment to s. \$\endgroup\$
    – Ventero
    Commented Jul 17, 2014 at 19:41
  • \$\begingroup\$ Very nice, will make those edits. \$\endgroup\$
    – histocrat
    Commented Jul 17, 2014 at 19:44
18
\$\begingroup\$

Perl 123

$s="*, *, * o'(, * o'( ),";print"$s
$s
$s
We're gonna ) around the ( tonight."=~s![(*)]!(clock,rock)[1+1+1&ord$&]||++$i!reg

The regular expression matches (, ) and *. I used the fact that the ASCII code of ( is 40, of ) is 41 and * is 42.

The r flag of the regular expression enables "in place substitution" and the e flag enables code evaluation (similar to x.replace(/.../, function(m){...}) in JavaScript).

(clock,rock) is an array of two "bare words". $& is the current match of the regular expression and ord$& is it's ASCII value.

Masked by 3 or 1+1+1 the ASCII values are now 0, 1 and 2, so I can use them to retrieve the corresponding array element. As 2 is out of the array, the "short circuit" operator || evaluates ++$i. In the first match $i is undef so incrementing it I get 1, then 2 and so on...

In simple words. This replaces * with it's current occurrence, ( with "clock" and ) with "rock".

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Does. Not. Compute. Explanation please? \$\endgroup\$
    – seequ
    Commented Jul 17, 2014 at 17:29
  • 1
    \$\begingroup\$ @TheRare Explanation added ;) \$\endgroup\$
    – core1024
    Commented Jul 17, 2014 at 18:09
  • 6
    \$\begingroup\$ It's black magic. +1 \$\endgroup\$
    – seequ
    Commented Jul 17, 2014 at 18:20
  • \$\begingroup\$ This is the kind of thing Perl was made for, unsurprising it does so well. \$\endgroup\$
    – Pharap
    Commented Jul 19, 2014 at 11:35
16
\$\begingroup\$

Bash+coreutils, 120 ASCII, 92 Unicode

ASCII:

c=clock
f(){ echo $[++i], $[++i], $[++i] o\'$c, $[++i] o\'$c rock,;}
f
f
f
echo We\'re gonna rock around the $c tonight.

Unicode:

iconv -t unicode<<<挊挽潬正昻⤨⁻捥潨␠⭛椫ⱝ␠⭛椫ⱝ␠⭛椫⁝屯␧Ᵽ␠⭛椫⁝屯␧⁣潲正㬬㭽㭦㭦㭦捥潨圠履爧⁥潧湮⁡潲正愠潲湵⁤桴⁥挤琠湯杩瑨ਮ|sed 1d|bash

base64 encoded Unicode version (in case unicode renderings get messed up):

aWNvbnYgLXQgdW5pY29kZTw8POaMiuaMvea9rOato+aYiuKkqOKBu+aNpea9qOKQoOKtm+akq+Kx
neKQoOKtm+akq+KxneKQoOKtm+akq+KBneWxr+KQp+Kxo+KQoOKtm+akq+KBneWxr+KQp+KBo+a9
suato+OsrOCpveCppuCppuCppuaNpea9qOWcoOWxpeeIp+KBpea9p+a5ruKBoea9suato+aEoOa9
sua5teKBpOahtOKBpeaMpOeQoOa5r+adqeeRqOCornxzZWQgMWR8YmFzaAo=
\$\endgroup\$
7
  • 2
    \$\begingroup\$ 120, as the semicolon after the function declaration's closing brace is not needed. \$\endgroup\$
    – manatwork
    Commented Jul 17, 2014 at 16:44
  • \$\begingroup\$ @manatwork Oops good catch - I don't know how that one snuck in there. Thanks! \$\endgroup\$ Commented Jul 17, 2014 at 16:53
  • 1
    \$\begingroup\$ I'm on Windows now, but what is the reason to use eval instead of pipeing to bash or sh? \$\endgroup\$
    – core1024
    Commented Jul 17, 2014 at 17:34
  • \$\begingroup\$ @core1024 No reason other than me forgetting the possibility ;-) Thanks for the tip! \$\endgroup\$ Commented Jul 17, 2014 at 17:38
  • 1
    \$\begingroup\$ Oops, didn't count the $rs! \$\endgroup\$
    – Shahbaz
    Commented Jul 18, 2014 at 17:14
13
\$\begingroup\$

Brainfuck, 1299

Bleuuuurgh. That was terrible. I don't know why I did this, it seemed quite easy at the start. First and probably last Brainfuck script. Try it here.

With some help from an automated code generator, I was able to write a script which reused most of the characters instead of generating each one from scratch. It's only 275 characters shorter than the one automatically generated, posted here.

The output isn't newline-terminated.

I might put a bit of explanation in later on. Essentially, any long line which isn't something like >>>>>>> or <<<<<<< or >>.>>.>>.>>. generates a character or a series of them.

-[----->+<]>--.
>
++[------>+<]>+.------------.
[-]<<+.>
++[------>+<]>+.------------.
[-]<<+.>
++++[->++++++++<]>.
>
+[------->++<]>+.
>
+[------->+++<]>++.
>
--[----->+<]>---.
>
+[------->++<]>--.
>
+[------->++<]>+.
>
--[----->+<]>---.
>
+[----->+++<]>++++.
>
++[------>+<]>+.
>
++++[->++++++++<]>.
<<<<<<<<<<<<<<<<<<<<+.
>>.>>.>>.>>.>>.>>.>>.>>.>>>>.
>
+[--------->++<]>.
>
+[------->++<]>+.
>
--[----->+<]>---.
>
+[----->+++<]>++++.
<<<<<<<<<<.
<++++++++++.
<<<<<<<<<<
<<<<<<<
+.
>>>>>>>>>>
>>>>>>>>.
<<<<<<<<<<
<<<<<<
.<<+.
>>>>>>>>>>
>>>>>>>>.
<<<<<<<<<<
<<<<<<
.<<+.
>>.>>.>>.>>.>>.>>.>>.>>.>>.
<<<<<<<<<<
<<<<<<
.<<+.
>>.>>.>>.>>.>>.>>.>>.>>.>>>>.
>>.>>.>>.>>.
<<<<<<<<<<.
<.
<<<<<<<<<
<<<<<<<<
+.[-]<
++[------>+<]>+.
>>.
[-]<<[-]<
-[----->+<]>--.-.
[-]<
++[------>+<]>+.------------.
[-]<
-[----->+<]>--..
>
++++[->++++++++<]>.
>>.>>.>>.>>.>>.>>.>>.>>.>>.
>>>>>>>>>
-[----->+<]>--.+.
<<<<<<<<<
<<<<<<<<<
<<<<<<<<<
<.
>>.>>.>>.>>.>>.>>.>>.
>>>>.
>>.>>.>>.>>.
>>[-]
++[------>+<]>+.>++++++++++.
>
+[--->++<]>+.++[->++++<]>+.---[->+++<]>+.-[->+++<]>.-------------.--[--->+<]>-.++[->+++<]>+.++++++++.-..-------------.
<<<<<<<<<<
<<<<<<<<<.
>>.>>.>>.>>.
<<[-]<<[-]<<[-]<<.
>
--[----->+<]>-----.--[--->+<]>---.---.++++++.-------.----------.
[-]<<<.
>
--------[-->+++<]>.------------.---.
<<.
<<<<<<<<<<
<<.
>>.>>.>>.>>.
>>>>.
>>[-]
--------[-->+++<]>.-----.-.-----.--.+.++++++++++++.[++>---<]>.
\$\endgroup\$
0
10
\$\begingroup\$

Lua - 217 164 154 151 149 143

Modified the old one to use a loop instead, saves a boat-load of characters. Thanks to TeunPronk for helping me shave off another 10 characters and to WilliamBarbosa for shaving off another 3 characters. 2 more characters can be saved by using the variable k. 6 more characters are saved by eliminating the variable r=" rock," since it is used once.

o=" o'clock"c=", "w=io.write k=1+1 for i=1,11,k+k do w(i,c,i+1,c,i+k,o,c,i+k+1,o," rock,\n")end w("We're gonna rock around the clock tonight.")

Ungolfed,

o=" o'clock"
c=", "
w=io.write
k=1+1
for i=1,11,k+k do 
   w(i,c,i+1,c,i+k,o,c,i+k+1,o," rock,\n")
end
w("We're gonna rock around the clock tonight.")
\$\endgroup\$
7
  • \$\begingroup\$ I wonder if it might be shorter if you use a loop instead of j and k. \$\endgroup\$
    – Teun Pronk
    Commented Jul 17, 2014 at 14:20
  • \$\begingroup\$ @TeunPronk: haha, I was just changing that as your comment pinged me \$\endgroup\$
    – Kyle Kanos
    Commented Jul 17, 2014 at 14:20
  • 1
    \$\begingroup\$ You can bring it down to 157. You dont need the comma after every 3rd number. Put the space infront of O, remove O from R, place a space in R and replace R by O,R in your io.write :) \$\endgroup\$
    – Teun Pronk
    Commented Jul 17, 2014 at 14:29
  • 1
    \$\begingroup\$ @TeunPronk: Updated. Thanks for your help! \$\endgroup\$
    – Kyle Kanos
    Commented Jul 17, 2014 at 14:32
  • 1
    \$\begingroup\$ You can store io.write in a variable (f=io.write) and reduce this even further \$\endgroup\$ Commented Jul 17, 2014 at 15:48
8
\$\begingroup\$

CJam - 90 ASCII / 54 Unicode

ASCII:

{{T):T}:F~", ":CFCF" o'clock":OCFO" rock,
"}Z*"We're gonna rock around the clock tonight."

Unicode:

"ξ漰㠬༳ᆧᶃ㸩씪咦⏚騤䖫퍃᰽薂ᴤਥ궋씻㱗㽫Ƶꐥ勋䎔䃱魠ꝯ朐酠礢璿狮꓈执낦덋觫ᥛ琚"GZ)#bBC*b:c~

Try them at http://cjam.aditsu.net/

\$\endgroup\$
1
  • \$\begingroup\$ Your unico... "This answer has been edited." Oh, nevermind. \$\endgroup\$
    – seequ
    Commented Jul 17, 2014 at 12:57
8
\$\begingroup\$

Python (123)

print((1+1+1)*"%i, %i, %i o'clock, %i o'clock rock,\n"%tuple(range(1,11+1+1))+"We're gonna rock around the clock tonight.")

PYG (112)

P((1+1+1)*"%i, %i, %i o'clock, %i o'clock rock,\n"%T(R(1,11+1+1))+"We're gonna rock around the clock tonight.")
\$\endgroup\$
7
\$\begingroup\$

Brainfuck - 680

test it here: ideone.com/Wi9ftB

>>>++++[>+++++++++[>+++[<<<<+<+>>>>>-]<<<+<<<+<+>>>>>>-]<-]+<++++++++<+++<---------<++++++++
++++<----<++++++++++<+++[>>>+.>>>.<<<<.>>>>>>[>[<<<<<<--------.-.>>>.<<<<.>+..<.>>>.>-----.<
------------.+++++++++.+++.------------.++++++++.++++>+++++.<<<<.>.+.>>>>->>-]+<<+>-]+<[<<<<
+.>>>.<<<<.>+.<.>>>.>-----.<<.>---.+++.<.>----.++++>+++++.<<<<.>+.>>>>-]<<<<<.>>>.>-----.<<.
>---.+++.<.>----.<<<.>>>+++++++.---.<.>----.++++>+++++.<<<<<.<-]>[>>++++<<-]>>---.[-]<[>+<-]
>>++.>>-----.<+++.<.<.>++.>---.-..<------.<.>>++++.---.<++.>----.<<.>--.>+++++++.---.++++++.
-------.<+++.<.>>++++++.<++++.---.<.>--.>--------.+++.<.>----.<<.>>+++++++++.-----.-.-----.-
-.+.>[>+++<-]>-.<<<---[>>+<<--]>>--.

as i promised, here's a better version. my brain feels like.. ooh so that's where the name comes from.

explanation: (because it is unmaintainable and i don't want to forget how it works)

firstly you have to choose the best data layout for this challenge. i came up with

-4 outmost loop -3 newline char -2 space -1 numbers 1 lowercase #1 2 lowercase #2 3 comma, apostrophe, period 4 condition (outmost loop < 3) 5 inner loop 6 second inner loop

We have to belive this is optimal unless there will be too many shifts (which case you have to rearrange the layout)

After that I used a 4x9x3 loop to set the starting values for newline, number, the two lowercase letter and the comma. (all the smaller ascii codes got 4x9=36 and the two lowercase letter got 4x9x3=108, then I added and substracted some to get their real value)

When the preset is done, the code enters the outmost loop that cycles 3 times. I explain it by pseudocode.

for 3 to 1 where i = numbers
    print '++i, '
    while inner_loop != 0              # this is true after the first loop
        while second_inner_loop != 0   # and this is true after the second
            print '10, 11 o'clock, 12' # prints the difference
            condition = 0              # it prevents the printing below
        second_inner_loop += 1
    inner_loop += 1
    while condition != 0
        print '++i, ++i o'clock, ++i'
    print ' o'clock rock,\n'

When this section is done I just have to print the last line. But we have an uppercase W that must be produced without using too many characters. At this point we are here:

Address  Value  Pointer
     -4      0  <--
     -3     10
     -2     32
     -1     50
      1     99
      2    111
      3     44

so I put [-3] 4 times to [-1] and substract 3 to get 87 (W): >[>>++++<<-]>>---.
then erase it [-]
and move [-2] to this location [-1] so space will be close to the lowercase letters. <[>+<-]

After that it just prints the letters. lowercase #1 is for the lower part 97-107 and lowercase #2 is for the above region.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ +1 for being 300 characters shorter than the other BF answers \$\endgroup\$
    – durron597
    Commented Jul 21, 2014 at 13:13
  • \$\begingroup\$ i was thinking about going even lower. conditions are awful to create without a decent tutorial (esolangs is down) but it can be shorter by ~200 chars more \$\endgroup\$
    – bebe
    Commented Jul 21, 2014 at 13:26
6
\$\begingroup\$

JavaScript, 111 107 chars, without using any number.

Execute these snippets in your browser's JS console:

107 (inspired by http://xem.github.io/obfuscatweet/ plus the other JS answer):

eval(unescape(escape('𩡯𬠨𬰽𘠬𘀢𛁴🐢𘁯𙱣𫁯𨱫𘠬𪐽𮀽𙰧𞰫𚱩🀽𜐱𞱸𚰽𪐫𚰫𬰫𪐫𚰫𬰫𪐫𚰫𭀫𬰫𪐫𭀫𘠠𬡯𨱫𛁜𫠢𚐻𮀫𘡗𩐧𬡥𘁧𫱮𫡡𘁲𫱣𪰠𨑲𫱵𫡤𘁴𪁥𘁣𫁯𨱫𘁴𫱮𪑧𪁴𛠢').replace(/uD./g,'')))

111 (just packing all the string and removing the eval around):

unescape(escape('𜐬𘀲𛀠𜰠𫰧𨱬𫱣𪰬𘀴𘁯𙱣𫁯𨱫𘁲𫱣𪰬𒠵𛀠𝠬𘀷𘁯𙱣𫁯𨱫𛀠𞀠𫰧𨱬𫱣𪰠𬡯𨱫𛀊𞐬𘀱𜀬𘀱𜐠𫰧𨱬𫱣𪰬𘀱𜠠𫰧𨱬𫱣𪰠𬡯𨱫𛀊𥱥𙱲𩐠𩱯𫡮𨐠𬡯𨱫𘁡𬡯𭑮𩀠𭁨𩐠𨱬𫱣𪰠𭁯𫡩𩱨𭀮').replace(/uD./g,''))
\$\endgroup\$
2
  • 4
    \$\begingroup\$ anyone else see a turtle in there...? wtf \$\endgroup\$
    – Dunno
    Commented Jul 21, 2014 at 19:38
  • \$\begingroup\$ hehe, yeah, the "random" Unicode characters generated by obfuscatweet sometimes lead to emoji :) \$\endgroup\$
    – xem
    Commented Jul 22, 2014 at 6:55
5
\$\begingroup\$

C - 145 - try me

main(i){i<11?main(i-~1+1,printf("%d, %d, %d o'clock, %d o'clock rock,\n"
,i++,i,i-~1,i-~1+1)):puts("We're gonna rock around the clock tonight.");}

C - 172 - try me

#define l ,__LINE__+~1
#define f ;printf("%d, %d, %d o'clock, %d o'clock rock,\n"
main(){f l
l
l
l)f
l
l
l
l)f
l
l
l
l);puts("We're gonna rock around the clock tonight.");}
\$\endgroup\$
11
  • 1
    \$\begingroup\$ I'm not much of a C programmer, so forgive me if I am being naive, but don't you need #include <stdio.h> to use printf? \$\endgroup\$
    – Kyle Kanos
    Commented Jul 17, 2014 at 18:55
  • 1
    \$\begingroup\$ default libraries are linked to the source in vc++ and gcc (so if you don't need something special, you can freely start a golfing challenge by typing main(){... \$\endgroup\$
    – bebe
    Commented Jul 17, 2014 at 19:00
  • \$\begingroup\$ Ah, I see. That's strange that it spits out a warning if it's accepted by default (just checked with my gcc & icc, no vc++ to test). \$\endgroup\$
    – Kyle Kanos
    Commented Jul 17, 2014 at 19:05
  • \$\begingroup\$ it also spits sequence point warning so...-w :) \$\endgroup\$
    – bebe
    Commented Jul 17, 2014 at 19:13
  • \$\begingroup\$ @Kyle include files defines how external functions work (return type and parameters). Nothing to do with linking libraries. A standard C compiler will passs whatever parameters you like to any function (with warnings) and return values are often ignored, so it works anyway. \$\endgroup\$
    – edc65
    Commented Jul 17, 2014 at 20:21
5
\$\begingroup\$

PHP - 125 129

Version 1 (129)

$i;while($i<11)echo++$i.", ".++$i.", ".++$i." o'clock, ".++$i." o'clock rock,\n";echo"We're gonna rock around the clock tonight";

Version 2 (125)

while($i++<11)echo"$i, ".++$i.", ".++$i." o'clock, ".++$i." o'clock rock,\n";echo"We're gonna rock around the clock tonight";

Clean version:

while($i++<11)
    echo "$i, ".++$i.", ".++$i." o'clock, ".++$i." o'clock rock,\n";
echo "We're gonna rock around the clock tonight";
\$\endgroup\$
5
\$\begingroup\$

Perl, 114 111 110 108

$s=", X o'clock";$_="X, X$s$s rock,
"x(1+1+1)."We're gonna rock around the clock tonight.";s/X/++$i/eg;print

110:

$s=", X o'clock";print(("X, X$s$s rock,
"x(1+1+1)."We're gonna rock around the clock tonight.")=~s/X/++$i/egr)

111:

print(("X, X, X o'clock, X o'clock rock,
"x(1+1+1)."We're gonna rock around the clock tonight.")=~s/X/++$i/egr)

114:

($s="X, X, X o'clock, X o'clock rock,
"x(1+1+1)."We're gonna rock around the clock tonight.")=~s/X/++$i/ge;print$s
\$\endgroup\$
0
4
\$\begingroup\$

C++ 261 203

#include<iostream>
#define x i++;
#define y std::cout<<
#define z y x y
void m(){for(int i=1;i<11;){z", ";z", ";z" o'clock, ";z" o'clock rock, "<<"\n";}y"We're gonna rock around the clock tonight.";}

Edited my first attempt; it worked when I first tested it, but that seems to be a fluke; post-increment in a stream is "undefined behaviour", and it did weird things (including blue screening Windows 7!) when I tried re-running it. I think that using printf instead of std::cout could be used with post-increment to get a shorter program.

Edited again, got it down to 231 by re-implementing the loop. Edited yet again, now down to 203...those #defines weren't all useful in the looping version.

For those wondering how C++ can run a function "m", the entry point can be defined as any function with just a compiler option, as can setting all funcs to be stdcall; I used both switches when compiling the above. If you don't like the redefined entry point, add 3 to my character count.

\$\endgroup\$
7
  • \$\begingroup\$ Which compiler are you using? gcc (4.7.1) doesn't allow me to call the main function m(). You can however drop the void and save 5 characters (or 2 if you name the method main) \$\endgroup\$ Commented Jul 17, 2014 at 17:18
  • \$\begingroup\$ how on earth did it get any upvotes? firstly you wrote a function called 'm' that will not compile by default, secondly every compiler shows the hours in reverse order. this needs a BIG correction \$\endgroup\$
    – bebe
    Commented Jul 17, 2014 at 18:59
  • 1
    \$\begingroup\$ @HackerCow I can't seem to make VS2013 work without the void return type defined; is there a compiler switch that would allow that? \$\endgroup\$
    – Darren
    Commented Jul 17, 2014 at 23:57
  • 1
    \$\begingroup\$ @bebe I explain the m() entry point in my edit; as for the output hours being printed in reverse order, I never managed to duplicate that, but I did manage to get it to print correctly (the first time I tried it), then on retesting all 1's, 5's and 12's, then on another retest it crashed the OS. So, technically it ran once, but relying on undefined behaviour is probably a 'bad idea', so I rewrote it :) \$\endgroup\$
    – Darren
    Commented Jul 18, 2014 at 0:00
  • 1
    \$\begingroup\$ @bebe I'm cool either way; in no way am I going to "win" any code golf puzzle with C++, so adding 3 characters is no biggie. I agree that -D switch macro definitions would be cheating, but pretty much any other compiler flags seem legit to me, as they also seemed legit to folks in the thread you linked. Any advantage you squeeze out of simple compiler switches won't exactly even the playing field between C++ and Python/Bash/Perl etc. \$\endgroup\$
    – Darren
    Commented Jul 18, 2014 at 2:55
4
\$\begingroup\$

Swift - 136 134

var x=1
for;x<11;{print("\(x++), \(x++), \(x++) o'clock, \(x++) o'clock rock,\n")}
print("We're gonna rock around the clock tonight.")
\$\endgroup\$
3
\$\begingroup\$

Java, 228

A hardcoded solution would have probably been shorter.

public static void main(String[] a){
        String s = "";
        int i=1,j=1;
        for(i--,j--;i<=11;){
            s += ++i + (j > 1 ? " o'clock rock, ":", ");
            if(j == "one".length()){
                j -= j;
                s +="\n";
            }else
                j++;
        }
        System.out.println(s+"We're gonna rock around the clock tonight");
}
\$\endgroup\$
4
  • 3
    \$\begingroup\$ +1 for the realization that hard-coding would be shorter. \$\endgroup\$
    – Kyle Kanos
    Commented Jul 18, 2014 at 13:52
  • \$\begingroup\$ 1+1+1 is 9 characters shorter than "two".length()". And is String[] a necessary? \$\endgroup\$
    – Kyle Kanos
    Commented Jul 18, 2014 at 13:53
  • \$\begingroup\$ That kinda felt like cheating, and yes, String[] a is necessary. \$\endgroup\$
    – Undeserved
    Commented Jul 18, 2014 at 14:07
  • \$\begingroup\$ It's not cheating, several people have used it :D \$\endgroup\$
    – Kyle Kanos
    Commented Jul 18, 2014 at 14:09
3
\$\begingroup\$

Befunge-98 (402 268)

Now a unefunge!

".thginot kcolc eht dnuora kcor annog er'eW"a11+/a*1-> #;:,1+" ,",,:,1+" ,",,:,1+" ,kcolc'o "a1-k,:,1+a",kcor kcolc'o "ek,:a11+/a*b1111+++-+-#;_:,a-1+" ,",,:a111++-:*,,1+" ,",,:a111++-:*,,1+" ,kcolc'o "a1-k,:a111++-:*,,1+a",kcor kcolc'o "ek,:a11+/a*b1111+++-+$$$aa*k,@
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Random bits of backward text, ,,:,1+, a11+/a*b1111+++-+w:,, stray characters underneath the main line of code... typical Befunge. Most of the online interpreters are having problems with the line length (it's way over the limit), do you know of one that doesn't mind? \$\endgroup\$
    – user16402
    Commented Jul 17, 2014 at 18:24
  • \$\begingroup\$ I couldn't find any Befunge-98 compilers online, but you could use pyfunge which is available in PyPI. \$\endgroup\$
    – waylon531
    Commented Jul 17, 2014 at 19:51
3
\$\begingroup\$

Groovy - 140 139 chars

Golfed, influenced by William Barbosa's answer:

s="ock"
i=1
f={->println "${i++}, ${i++}, ${i++} o'cl$s, ${i++} o'cl$s r$s,"}
f();f();f()
println "We're gonna r$s around the cl$s tonight"

Ungolfed:

s = "ock"
i = 1
f = { ->
    println "${i++}, ${i++}, ${i++} o'cl$s, ${i++} o'cl$s r$s,"}
}

f();f();f()

println "We're gonna r$s around the clock tonight"
\$\endgroup\$
2
\$\begingroup\$

Mathematica - 153

i=1;
StringReplace[
  StringJoin[
    Riffle[{s="X, X, X o'clock, X o'clock rock,",s,s},"\n"]]<>
    "\nWe're gonna rock around the clock tonight.","X":>ToString[i++]]
\$\endgroup\$
1
  • \$\begingroup\$ +1, I think this is the best strategy but it can be shorter. \$\endgroup\$
    – user11030
    Commented Jul 19, 2014 at 1:59
2
\$\begingroup\$

C++ 252

#define P(x) cout<<x
#define Q cout<<", "
#define N cout<<endl
#define C P(" o'clock")
#define R P(" rock")
#define F P(++i);Q;P(++i);Q;P(++i);C;Q;P(++i);C;R;N;
int i;
main()
{
    F F F
    P("We're gonna rock around the clock tonight.");
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ Unless my math sucks, using P(", ") in place of Q saves you 2 characters. \$\endgroup\$
    – Kyle Kanos
    Commented Jul 17, 2014 at 19:46
  • 2
    \$\begingroup\$ N and R only appear once: it would be better to inline them than to make a macro. \$\endgroup\$
    – user16488
    Commented Jul 18, 2014 at 1:31
  • \$\begingroup\$ Using the above comments and more this can be condensed to 200 (even with the include and namespaces my compiler wants): #include<iostream> #define P std::cout<<++i<< #define C" o'clock" #define F P", ";P", ";P C<<", ";P C<<" rock"<<std::endl; int i;main(){F F F std::cout<<"We're gonna rock around the clock tonight.";} \$\endgroup\$ Commented Jul 23, 2014 at 14:10
2
\$\begingroup\$

Cobra - 193

class P
    var i=1-1
    def main
        print[.f,.f,.f,"We're gonna rock around the clock tonight."].join("")
    def f as String
        return"[[.i+=1,.i+=1,.i+=1].join(", ")] o'clock, [.i+=1] o'clock rock,\n"
\$\endgroup\$
2
\$\begingroup\$

Java (v2) - 250 241 232 chars

This is a complete running program, influenced by William Barbosa's answer.

Golfed:

public class R{
static int o=1;
static String c(){return o+++", "+o+++", "+o+++" o'clock, "+o+++" o'clock rock,\n";}
public static void main (String[] a){System.out.println(c()+c()+c()+"We're gonna rock around the clock tonight");}}

Ungolfed:

public class R {
    static int o = 1;
    static String c() { 
        return o+++", "+o+++", "+o+++" o'clock, "+o+++" o'clock rock,\n";
    }

    public static void main (String[] a) {
        System.out.println(c()+c()+c()+"We're gonna rock around the clock tonight");
    }
}
\$\endgroup\$
2
\$\begingroup\$

ECMAScript6 - 136 135

Doesn't even use the allowed 1 digit:

alert([,...a=[s=", ",s,(c=" o'clock")+s,c+" rock,\n"],...a,...a].map((x,i)=>i+x).join("")+"We're gonna rock around the clock tonight.")

Tested in Firefox console.

\$\endgroup\$
2
  • \$\begingroup\$ or 114 chars using xem.github.io/obfuscatweet ;) \$\endgroup\$
    – xem
    Commented Jul 18, 2014 at 16:21
  • 4
    \$\begingroup\$ @xem, I know it fits in with the letter of the law, but I'm not a fan of Unicode compression (or any other compilers/generators). In my mind, golf is a test of skill and all code should be hand crafted... but that's just me. \$\endgroup\$
    – Alconja
    Commented Jul 18, 2014 at 22:43
2
\$\begingroup\$

Golflua -125

Basically a direct translation of my Lua solution:

k=1+1o=" o'clock"c=", "~@i=1,11,k+k I.w(i,c,i+1,c,i+k,o,c,i+k+1,o," rock,\n")$w("We're gonna rock around the clock tonight.")
\$\endgroup\$
2
\$\begingroup\$

PowerShell, 123 118 109

1,1,1|%{"$('',''," o'clock"," o'clock rock"|%{"$((++$a))$_,"})"}
"We're gonna rock around the clock tonight."

After a horrible start (167) I got rid of a few idioms I didn't even need and at least got it shorter than the reference.

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

Haskell -- 138 137 chars

As a standalone program:

r n=show n++s!!n
s=" o'clock rock,\n":c:c:" o'clock, ":s
c=", "
main=putStr$(r=<<)[1..11+1]++"We're gonna rock around the clock tonight."

Edit: I used to have a helper function o used to define s by

s=o" rock,\n":c:c:o c:s;o=(" o'clock"++)

but it turns out to cost 1 extra character. Is there any way to compress all of the "o'clock"s / "rock"s / "ock"s?

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

JavaScript, 140

a=", x o'clock",b="x, x"+a+a+" rock,\n",b+=b+=b+"We're gonna rock around the clock 
tonight.";for(i=1;i<=11+1;)b=b.replace("x",i++);alert(b)

I make a string like "x, x, x o'clock x o'clock rock,\n..." Then I replace the "x's" with numbers.

\$\endgroup\$
1
\$\begingroup\$

PHP 150

No numbers, including the 11 which would not be two 1's stuck together, but the number value eleven.

function f(){static $i;echo++$i.", ".++$i.", ".++$i." o'clock, ".++$i." o'clock rock,\n";}f();f();f();echo"We're gonna rock around the clock tonight";
\$\endgroup\$
1
  • \$\begingroup\$ It's missing the trailing full-stop. \$\endgroup\$
    – Joey
    Commented Jul 19, 2014 at 10:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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