46
\$\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\$
  • 1
    \$\begingroup\$ There are spaces at the end of first 3 lines, do those count? \$\endgroup\$ – aditsu Jul 17 '14 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 Jul 17 '14 at 12:56
  • 1
    \$\begingroup\$ I removed them now \$\endgroup\$ – aditsu Jul 17 '14 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 Jul 17 '14 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 Jul 17 '14 at 17:17

50 Answers 50

1
\$\begingroup\$

Haskell -- 155/141 147/133 chars

147 characters as a program:

main=putStrLn.(++"We're gonna rock around the clock tonight.").concat.zipWith((++).show)[1..11+1].cycle$[", ",", "," o'clock, "," o'clock rock,\n"]

133 characters as String expression:

(++"We're gonna rock around the clock tonight.").concat.zipWith((++).show)[1..11+1].cycle$[", ",", "," o'clock, "," o'clock rock,\n"]
\$\endgroup\$
1
\$\begingroup\$

Scala, 156

for(i<-1 to 11 by 1+1+1+1){println(i+", "+(i+1)+", "+(i+1+1)+" o'clock, "+(i+1+1+1)+" o'clock rock,")};println("We're gonna rock around the clock tonight.")
\$\endgroup\$
1
\$\begingroup\$

MATLAB, 199 192 182

f=1+1+1+1;a=[1,1+1,f-1,f];b=' o''clock';c=' rock';
for i=1:f-1;fprintf('%g, %g, %g,%s, %g%s%s,\n',a(1:f-1),b,a(f),b,c);a=a+f;end
fprintf('We''re gonna%s around the clock tonight.',c)
\$\endgroup\$
  • \$\begingroup\$ Counting characters here (and using just a single space/tab for indentation) yields 198 characters, actually. Also I wonder whethe indentation is even necessary at all because the loop is bounded by for and end, which would save two characters. Then there is an unnecessary space between the comma and the \n in the string. \$\endgroup\$ – Joey Jul 18 '14 at 14:32
  • \$\begingroup\$ Another thing I wondered: Are semicolons needed after a line? If not, then you can save two of them as well (line 1 and 4). \$\endgroup\$ – Joey Jul 18 '14 at 14:34
  • \$\begingroup\$ Good observations, thanks! They aren't, but omitting a semicolon will print out the variables, which I think is not desired. \$\endgroup\$ – Takatam Jul 18 '14 at 14:38
  • \$\begingroup\$ You could use c=' rock', i.e. with a leading space to use a format string in the last string as well, saving another character. You can then do the same trick with b as well, saving two more characters in total. \$\endgroup\$ – Joey Jul 18 '14 at 15:27
1
\$\begingroup\$

PHP 258 220 only with Manatwork's help

EDIT: Thank you manatwork, again you did stuff there I didn't know you could do. Thanks!

<?php $a='clock';$b=" o'";$c="$b$a rock,";$d=1+1;$e=$d+$d;$f=", ";$g=$e+$e;echo"1$f$d$f",$d+1,"$b$a$f$e$c
",$e+1,$f,$e+$d,$f,$g-1,"$b$a$f$g$c
",$g+1,$f,$g+$d,$f,"11$b$a$f","1$d$c
We're gonna rock around the clock tonight.";

I must admit I thought this was going to be quite a dull question but it turned out to be quite fun. I thought I might have a chance with this one in php. Sadly not to be :-(

Edit: Actually it might not be PHP at fault, just that I need to learn more and do a bit practice at code golf - it is actually my own fault :-)

I enjoyed trying anyway.

Original 258 chars:

<?php $a='clock';$b=" o'";$c=$b.$a." rock,";$d=1+1;$e=$d+$d;$f=", ";$g=$e+$e;echo"1".$f.$d.$f.($d+1).$b.$a.$f.$e.$c."
".($e+1).$f.($e+$d).$f.($g-1).$b.$a.$f.($g).$c."
".($g+1).$f.($g+$d).$f."11".$b.$a.$f."1".$d.$c."
We're gonna rock around the clock tonight.";?>
\$\endgroup\$
  • \$\begingroup\$ How you got 258? The entire text is 262, the bare minimal is 257 (no closing PHP tag and short opening PHP tag), just the code part is 254. \$\endgroup\$ – manatwork Jul 17 '14 at 17:15
  • 1
    \$\begingroup\$ Just a quick review. Changed some concatenations to string interpolations and others to separate parameters to reduce to 223 characters: pastebin.com/k0sxN8nw \$\endgroup\$ – manatwork Jul 17 '14 at 17:29
  • 1
    \$\begingroup\$ PHP isn't that bad ;) I got 129, trick is to really see what you want to use variables for \$\endgroup\$ – Samuel Jul 17 '14 at 18:42
  • 1
    \$\begingroup\$ That is amazing. I love the use of WHILE for the first few lines. I didn't think of that. I also did not know you could miss out the braces in the case of a single statement and the php tags when quoting the program. Just checked on php.net and it is absolutely correct. I really must go and lookup some of the functions that I take for granted and see what else they can do. First it was FOR loops, now WHILE loops. I am going now to read about IF and see what I missed. Thanks for the great example. I shall do better next time. \$\endgroup\$ – Paul Drewett Jul 18 '14 at 8:19
  • \$\begingroup\$ @manatwork - thank you for that reworking, it was very interesting to see how you did that. As for the original count my openoffice text document says it is 258 characters. \$\endgroup\$ – Paul Drewett Jul 18 '14 at 15:52
1
\$\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\$
1
\$\begingroup\$

Python 3, 100 99 Unicode

Using Unicode:

t=1+1;eval('瀠楲瑮㌨⠪┢Ⱪ┠≩㈫∪‬椥漠挧潬正⬢•潲正尬≮┩畴汰⡥慲杮⡥ⰱ㌱⤩∫敗爧⁥潧湮⁡潲正愠潲湵⁤桴⁥汣捯潴楮桧⹴⤢'.encode('utf%i'%(t*t<<t))[t:])

Encodes that string as UTF-16, dropping the first 2 bytes, resulting in:

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

Note the initial space to make an even number of ASCII characters.

Checked if there were any illegal digits in there, such as from other languages, looks like I'm safe:

>>> set(c for c in """<code>""" if unicodedata.category(c)[0] == 'N')
{'1'}
\$\endgroup\$
1
\$\begingroup\$

ECMAScript: 127 characters

i=1;alert("%, %, % o'clock, % o'clock rock,\n".repeat(1+1+1).replace(/%/g,x=>i++)+"We're gonna rock around the clock tonight.")

Just to promote some ECMAScript 6 features.

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

AWK, 164 139

Boring, but my first AWK golf attempt.

BEGIN{OFS=", "
k=" o'clock "
r="rock"
print ++t,++t,++t k,++t k r,"\n"++t,++t,++t k,++t k r,"\n"++t,++t,++t k,++t k r",\nWe're gonna "r" around the clock tonight."}

OFS comes in handy here.

Shorter version:

func p(){print ++t,++t,++t k,++t k r OFS}BEGIN{OFS=", "
k=" o'clock"
r=" rock"
p()
p()
p()
print"We're gonna"r" around the clock tonight."}
\$\endgroup\$
1
\$\begingroup\$

Delphi, 186

Ungolfed:

var c:int32;

function i:int32;
begin
  c:=c+1;
  i:=c;
end;

begin
  while c<11 do
    writeln(i,', ',i,', ',i,' o''clock, ',i,' o''clock rock,');
  write('We''re gonna rock around the clock tonight.');
end.

Golfed:

var c:int32;function i:int32;begin c:=c+1;i:=c;end;begin while c<11do writeln(i,', ',i,', ',i,' o''clock, ',i,' o''clock rock,');write('We''re gonna rock around the clock tonight.')end.
\$\endgroup\$
1
\$\begingroup\$

Scala, 142

Based on Leonardo Scattola's answer.

for(i<-1 to(11,1+1+1+1))println(f"$i, ${i+1}, ${i+1+1} o'clock, ${i+1+1+1} o'clock rock,");print("We're gonna rock around the clock tonight.")
\$\endgroup\$
1
\$\begingroup\$

Clojure - 261 244 178 176 chars

I'm a newbie to Clojure... suggestions welcome.

Golfed:

(let[i(atom(dec 1))](defn f[](swap! i inc))(defn p[](printf "%d, %d, %d o'clock, %d o'clock rock,\n"(f)(f)(f)(f)))(p)(p)(p)(print"We're gonna rock around the clock tonight."))

Ungolfed:

(let [i (atom (dec 1))]
  (defn f [] (swap! i inc) )
  (defn p [] (printf "%d, %d, %d o'clock, %d o'clock rock,\n" (f)(f)(f)(f)) )
  (p)(p)(p)
  (print "We're gonna rock around the clock tonight.")
)
\$\endgroup\$
1
\$\begingroup\$

><> (Fish), 133

(Uses only printable ascii.)

".thginot kcolc eht dnuora kcor annog er'eW"11.
v da-
>:?!\1-&a",kcor kcolc'o y ,kcolc'o y ,y ,y"&
/&+1/
>l?!v:bb*-?!\o
\  &;!+1n:&~/

Method:

  • Push the last line once and the first one 3 times onto the stack with the numbers changed to the letter "y".
  • Print everything from the stack if that's not an "y" while printing the value of an increasing counter if the character is an "y".

2 bytes saved thanks to @Sp3000.

\$\endgroup\$
  • \$\begingroup\$ I don't know if b is allowed, but if it is then you might as well do da- instead of 11+1+ \$\endgroup\$ – Sp3000 Apr 23 '15 at 11:33
  • \$\begingroup\$ @Sp3000 I think it's allowed. Edited. \$\endgroup\$ – randomra Apr 23 '15 at 11:57
1
\$\begingroup\$

Rebmu, 117 chars

O{ o'}C{clock}R{rock}S{, }T1 Ndz[igTad11 1[p[{We're going to}r{around the}c{tonight.}]qt]++T]fv[pCB[nSnSnOcSnOcSPrS]]

Un-"mushed":

o: " o'" c: "clock" r: "rock" s: ", " t: 1 n: dz [ig t ad 11 1 [p ["We're going to" r "around the" c "tonight."] qt] ++ t] fv [p cb [n s n s n o c s n o c sp r s]]

Commented:

; Define some string constants
o: { o'}
c: {clock}
r: {rock}
s: {, }

t: 1 ; assign one to T

; define a function with no arguments N (N does "DZ" the following)
n: dz [
    ; if T is greater than adding 11 to 1 (IG = If Greater, AD = Add), 
    ; print the final message and then quit 
    ig t ad 11 1 [
        p [{We're going to} r {around the} c {tonight.}] qt
    ]

    ; increment t but return its old value
    ++ t
]

; loop forever (will be exited by a quit during an invocation of N
; once it gets bigger than 12)
fv [
    ; print the result of combining (CB = Combine) the progressively
    ; increased values with the defined strings (also, SP = space)
    p cb [n s n s n o c s n o c sp r s]
]

COMBINE is being incubated in Rebmu, but hopefully on the roadmap for main Rebol/Red, see this article.

\$\endgroup\$
0
\$\begingroup\$

Java - 219 215 212 208

I know there's some other Java answers, but this should be the shortest...

class A{public static void main(String[]z){String o="",c=" o' clock",r=c+" rock,\n",p=", ";for(int i=1;i<=11;)o+=i+++p+i+++p+i+++c+p+i+++r;System.out.println(o+"We're gonna rock around the clock tonight.");}}
\$\endgroup\$
  • \$\begingroup\$ Contains a 0 which isn't allowed. It's missing the comma after the first three lines. Also you can save a space in the arguments of main. And another one after the for. And yet another one after class A. Using System.out.print would save another two bytes. Did you golf this? \$\endgroup\$ – Joey Jul 18 '14 at 20:43
  • \$\begingroup\$ Not very well clearly :/ Fixed. Though removing the ln seems like cheating. \$\endgroup\$ – Rich Smith Jul 18 '14 at 21:25
  • \$\begingroup\$ The reference text is 144 characters. This does not include a trailing newline. So just using print is actually okay, I guess. \$\endgroup\$ – Joey Jul 18 '14 at 21:51
0
\$\begingroup\$

Python - 195 chars

b=", ";x=" o'clock";y=' rock,\n';c=1;s="";n=1+1+1
for i in range(n):
 for j in range(n):s+=str(c)+b;c+=1
 s=s[:-1-1]+x+b+str(c)+x+y;c+=1
print s+"We're gonna rock around the clock tonight."
\$\endgroup\$
0
\$\begingroup\$

Javascript, 179 174

This is not gonna be the shortest one, but just for fun!

Golfed:

a="";for(i=1;i<=11+1;i++){a+=i.toString()+(!((i+1)%4)?" o'clock":"")+(!(i%4)?" o'clock rock":"")+", "+(!(i%4)?"\n":"");}alert(a+"We're gonna rock around the clock tonight.");

Ungolfed:

a="";
for(i=1;i<=11+1;i++){
    a+=i.toString()
        +(!((i+1)%4)?" o'clock":"")
        +(!(i%4)?" o'clock rock":"")
        +", "
        +(!(i%4)?"\n":"");
}
alert(a+"We're gonna rock around the clock tonight.");
a="";
\$\endgroup\$
0
\$\begingroup\$

JavaScript - 138

with(console){for(b=1;11>b;)log("%d, %d, %d o'clock, %d o'clock rock,",b++,b++,b++,b++);log("We're gonna rock around the clock tonight.")}

Try it in the console (uses console.log).

Inspired by izlin's answer, I just shortened it using console.log instead of alert.

\$\endgroup\$
  • \$\begingroup\$ Why those a.calls? l=console.log;for(b=1-1;11>b;)l("%d, %d, %d o'clock, %d o'clock rock,",++b,++b,++b,++b);l("We're gonna rock around the clock tonight.") \$\endgroup\$ – manatwork Jul 18 '14 at 14:16
  • \$\begingroup\$ @manatwork Illegal invocation error, console.log needs to run in the context of console \$\endgroup\$ – soktinpk Jul 18 '14 at 14:18
  • \$\begingroup\$ Interesting. It works for me in Firefox 30.0, Firebug 2.0.2 console. \$\endgroup\$ – manatwork Jul 18 '14 at 14:19
  • \$\begingroup\$ Why not use alert instead? \$\endgroup\$ – Jamie Jul 22 '14 at 13:29
  • 1
    \$\begingroup\$ @Jamie because alert takes more space because you can't use "%d". \$\endgroup\$ – soktinpk Jul 22 '14 at 21:41
0
\$\begingroup\$

F# - 173 136

Version 2 - 136

for x in[1..4..12]do printfn"%d, %d, %d o'clock, %d o'clock rock"x (x+1)(x+2)(x+3)
printfn"We're gonna rock around the clock tonight."

Version 1 - 173

System.Console.WriteLine("1, 2, 3 o'{0}, 4 o'{0} {1},\n5, 6, 7 o'{0}, 8 o'{0} {1},\n9, 10, 11 o'{0}, 12 o'{0} {1},\nWe're gonna {1} around the {0} tonight.","clock","rock")
\$\endgroup\$
0
\$\begingroup\$

Julia - 134

r="rock"
for i=1:4:12
    println("$i, $(i+1), $(i+2) $r $(i+3) o'clock $r")
end
println("We're gonna rock around the clock tonight.")
\$\endgroup\$
  • \$\begingroup\$ The rules say 'you must not use any digits in your code, except for the digit 1.' \$\endgroup\$ – ASCIIThenANSI Apr 23 '15 at 14:42
0
\$\begingroup\$

Octave, 125 characters

d="%d, %d, %d o'clock, %d o'clock rock,\n";
printf([d d d "We're gonna rock around the clock tonight.\n"],v=1:(a=1+1)*a++*a);

I construct [1 .. 12] vector using only ones and assignment operator, and pass it as variable argument list to printf, taking advantage of fact that printf (fmt, a, a, a) and printf(fmt, [a a a]) are both correct ways of supplying variable argument list to function.

\$\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.