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.

Challenge

Given that Christmas is

  • December
  • Month 12
  • Day 25

every year: determine today's date, and whether or not today is Christmas. If it is Christmas, you must print "It's Christmas". If it is not Christmas, you must somehow wait until Christmas and then print "It's Christmas".

Example

From this StackOverflow Question

import time
while time.strftime("%b, %d", time.localtime()) != "Dec, 25":
    time.sleep(60)
print "It's Christmas"

Python in 115 Characters

Rules

Here are the rules:

  • Assume that the computer's clock is always right.
  • Your code must be able to be started at any time.
  • Your code must print "It's Christmas" on Christmas.
  • Looping is certainly not necessary, but once started your code should not stop until it has printed.
  • Shortest code wins.
share|improve this question
    
I thought the input is in Julian date before I read this post –  SHiNKiROU Dec 21 '11 at 9:51

31 Answers 31

up vote 12 down vote accepted

Perl + Unix, 40 chars

1until`date`=~/c 25/;say"It's Christmas"

This is the same as J B's Perl solution, except that I save a few chars by using the external date command instead of Perl's localtime.

share|improve this answer

Bash, 39

For those who just can't wait:

sudo date 12250000
echo It\'s Christmas

This follows all of the rules, especially the first one.

share|improve this answer
18  
+1 for abusing the rules –  FUZxxl Dec 5 '11 at 20:54
4  
Clever. Not what I meant, but damned clever. –  pcperini Dec 5 '11 at 21:56
2  
“...and if the computer's clock isn't always right enough, make it.” Good show! –  J B Dec 6 '11 at 8:49
4  
You can save 5 characters if you're root. :D –  Wug Oct 8 '12 at 21:11

Unix at: 40 chars:

echo echo "It's christmas"|at -t12252359
share|improve this answer
    
+1 for pure simplicity and ingenuity. –  Polynomial Dec 12 '11 at 13:22
    
This violates »Looping is certainly not necessary, but once started your code should not stop until it has printed.«, though. –  Joey Dec 7 '13 at 17:59
    
I don't understand your critique. Do you claim this is looping? That it stops before printing? –  user unknown Dec 7 '13 at 19:30

PowerShell, 45 46 chars

for(;(date).date-ne'12/25'){}"It's Christmas"

It's certainly not very power-efficient, so a laptop battery might die before Christmas (reason to wish for a new one, maybe). But not sleeping is definitely shorter.

This is also locale-agnostic. And thanks to Jaykul for a nice trick in reducing this further.

Abusing the rules a bit, 45 chars

for(){"It's Christmas"*((date)-like'12/25*')}

This will print empty lines until it's Christmas, upon which it will print “It's Christmas”.

It ...

  • ... can be started at any time.
  • ... prints “It's Christmas” on Christmas. Several times. The whole day long. (The rules didn't say anything about how often it may be printed.)
  • ... does not print “It's Christmas” on not-Christmas (although it prints an empty line in that case; can be rectified by sacrificing another character, but then this gains nothing over the more sane solution above).
  • ... does not ever stop (not even after it has printed “It's Christmas” but definitely not before).
share|improve this answer
    
+1 for being locale-invariant. –  Peter Taylor Dec 5 '11 at 11:02
7  
+1 for abusing rules –  JiminP Dec 5 '11 at 13:21
2  
for(;(date).date-ne"12/25"){}"It's Christmas" # 45 chars –  Jaykul Dec 6 '11 at 6:32
    
Jaykul: Thanks ... that's something I never would have thought to be working. –  Joey Dec 6 '11 at 9:54
    
The "abusive" script could also use -match, and do without the wildcard, for no gain/loss in characters. –  Iszi Dec 7 '13 at 7:38

PostScript, 90

(%Calendar%)currentdevparams begin{Month 12 eq Day 25 eq and{exit}if}loop(It's Christmas)=

Don't run on a printer, it doesn't print a page, and it will only DoS your printer until Christmas day. Then again, getting your printer back would be a nice present.

share|improve this answer
2  
Don't forget to tape a sign to the printer saying: DO NOT KILL JOB UNTIL CHRISTMAS! –  Joey Adams Dec 6 '11 at 16:58
    
I guess most printers won't even have a working hardware clock as that's an optional feature. Very nice, though :) –  Joey Dec 7 '11 at 9:38

Mathematica, 47

While[Date[][[2;;3]]!={12,25}];"It's Christmas"
share|improve this answer

Perl, 44 45

perl -E'1until localtime=~/c 25/;say"It's Christmas"'

Wouldn't GMT time be sufficient? (3 characters off ;-)

share|improve this answer
    
1until localtime=~/c 25/; would save you one char. :) –  Ilmari Karonen Dec 5 '11 at 15:33
    
I thought I'd already tried that and it failed, but it turns out I actually forgot -E at the time. Thanks! –  J B Dec 5 '11 at 15:49
    
No problem. (Ps. I just posted a version of your solution using backticks instead of localtime below. Feel free to steal it if you like, but I felt the extra dependency justified a separate answer.) –  Ilmari Karonen Dec 5 '11 at 15:59
    
Naaaw, shame's on me for not thinking of it again. The separate answer is perfectly justified IMO (and upvoted). –  J B Dec 6 '11 at 8:54

Perl, 45

{localtime=~/c 25/&&die"It's Christmas";redo}

Perl, 44

using ternary operator (Thanks to Ilmari Karonen).

{localtime=~/c 25/?say"It's Christmas":redo}
share|improve this answer
    
? : instead of && ; would save you one char too. (And I'd use say instead of die for prettier output.) –  Ilmari Karonen Dec 5 '11 at 15:35
    
@Ilmari Karonen: thanks. But by using say instead of die, the script never finish. –  M42 Dec 5 '11 at 15:43
    
It will, if you use the ternary operator. –  Ilmari Karonen Dec 5 '11 at 15:49
    
@Ilmari Karonen: yes, of course. May be i'm too tired !!! –  M42 Dec 5 '11 at 16:01

Javascript, 51 chars

It's a CPU killer:

while(!/c 25/.test(Date()));alert("It's Christmas")
share|improve this answer

R (47)

while(!grepl("c 25",date())){};"It's Christmas"
share|improve this answer

Batch file, 68 chars

:l
@date/t|findstr/c:"-12-25">nul&&echo It's Christmas&&exit
@goto l

Not usable interactively, as it kills the session. Solving that would require 5 more characters.

Also locale-sensitive. This works on my locale which uses ISO 8601 date format.

But hey, it's a batch file (by most not even regarded as a programming language). And shorter than Javascript (and on par with Python).

share|improve this answer

Groovy, 55

while(!new Date()==~/.*c 25.*/);
println"It's Christmas"

Think it works, but still waiting for output.

share|improve this answer
    
If Groovy's regexps are anything like in most other languages, those .* are entirely unnecessary. (Ps. You can test it by waiting for Dec 5 instead of 25.) –  Ilmari Karonen Dec 5 '11 at 18:54
1  
Ilmari, Groovy is a JVM language and Java's regexes are anchored by default. –  Joey Dec 5 '11 at 19:19
1  
Ilmari, I'd check but my program is still running –  Alison Dec 6 '11 at 16:56

I wanted to do this without parsing strings. Subsequently, there's a lot of magic numbers in my code.

I did some approximation to account for leap years. No one said that it had to print it out right on 00:00:00, Dec. 25!

Perl, 80 69 57 characters

{(time-30931200)%31557600<86399?die"It's Christmas":redo}

Edited for more concise looping!

share|improve this answer

(pdf)eTeX - 180 chars only December 1-25.

\lccode`m`~\let\e\expandafter\def~{\ifdim900pt<\pdfelapsedtime
sp\pdfresettimer\else\e~\fi}\lowercase\e{\e\scantokens\e
{\romannumeral\numexpr (25 - \day)*96000}}It's Christmas!\bye

TeX only has a way to access the date when the program starts, and the time elapsed since the start, capped at 32768 seconds, so I need to compute the number of seconds to wait, and for each second do a loop which waits for the elapsed time to reach 1s and reset the time. (Precisely, I'm doing blocks of 900 seconds.)

Working for any month requires more work: 355 chars.

\lccode`m=`~\let\o\or\let\y\year\let\n\numexpr\let\e\expandafter
\def\b#1{\ifnum#1=\n(#1/4)*4\relax+1\fi}\def~{\ifdim
900pt<\pdfelapsedtime sp\pdfresettimer\else\e~\fi}\lowercase
\e{\e\scantokens\e{\romannumeral\n(25-\day+\ifcase\month\o334\b\y
\o303\b\y\o275\o244\o214\o183\o153\o122\o91\o61\o30\o0\ifnum25<\day
365\b{\n\y+1}\fi\fi)*96000}}It's Christmas!\bye
share|improve this answer

MySQL, 180 chars

Because what are you using your database engine for, anyway?

DELIMITER $$
CREATE FUNCTION c() RETURNS CHAR(14) BEGIN a: LOOP IF DAY(NOW())=25 && MONTH(NOW())=12 THEN RETURN 'It\'s Christmas'; END IF; END LOOP a; END$$
DELIMITER ;
SELECT c();

Not very competitive lengthwise, but hey, it's doable!

share|improve this answer

Ruby, 53

until Time.now.to_s=~/c 25/
end
puts"It's Christmas!"
share|improve this answer

Python (36 chars)

In the spirit of abusing the rules:

  • Definitely prints "It's Christmas" on Christmas

  • If it is not Christmas, pass the time by printing "It's Christmas"

    while True:
        print "It's Christmas"
    
share|improve this answer

Javascript, 93 89 78 77 chars

function x(){Date().match("c 25")?alert("It's Christmas"):setTimeout(x,1)}x()

share|improve this answer
    
@Joey Thanks! I forgot about that. –  JiminP Dec 5 '11 at 9:52
2  
Another non-blocking version: setInterval('/c 25/.test(Date())&&alert("It\'s Christmas")',9) at 61 chars ... the only drawback is that it will alert() all day on Christmas. –  David Murdoch Dec 6 '11 at 3:14
    
Why setInterval to 1? setInveral to 1000*60*60*24 and it both won't make the process suffer and will notify only once on christmas. –  George Mauer Feb 12 at 20:24

D, 130

import std.datetime,std.stdio;
main(){
do{
auto t = Clock.currTime();
}while(t.month!=12||t.day!=25);
writeln("It's Christmas");
}
share|improve this answer
    
You can probably save two characters in the assignment. And a few more by reducing the number of lines. –  Joey Dec 5 '11 at 16:28
    
I can also save some by using t.month^12|t.day^25 (if I get my priorities right) –  ratchet freak Dec 5 '11 at 18:38

Q, 63 chars

system"t 1000";.z.ts:{$["12.25"~-5#-3!.z.d;1"It's Christmas";]}

will work for christmas day on every year

share|improve this answer

SQL*Plus + PL/SQL - 100

EXEC LOOP EXIT WHEN TO_CHAR(SYSDATE,'DDMM')='2512';END LOOP;DBMS_OUTPUT.put_line('It''s Christmas');
  • Assuming SERVEROUTPUT ON
  • Shorter then the MySql solution (eat that, MySql!)
  • Too late for last year, but in time for this year
  • Tried DBMS_OUTPUT.put instead of DBMS_OUTPUT.put_line but that doesn't print anything.
share|improve this answer

Python, 66 68

import time
while'c 25'not in time.ctime():1
print"It's Christmas"
share|improve this answer
    
That 1 at the end of the second line looks quite suspicious to me. :P –  Trimsty Jan 7 at 15:10
    
@Trimsty: The 1 provides a body for the while-loop. It's basically a busy wait until December 25. It has the similar effect to while 1:if'c 25'in time.asctime():break. –  Steven Rumbalski Jan 7 at 15:39
    
Ah, okay, thanks for clearing that up. –  Trimsty Jan 8 at 8:02
    
I think time.ctime() would do. –  gnibbler Feb 12 at 6:14
    
@gnibbler: Indeed it would. Nice catch. –  Steven Rumbalski Feb 12 at 15:41

bash-n-date: 69 chars:

sleep $(($(date -d"12/25" +%s)-$(date +%s))) && echo "It's X-Ray    "

But it will fail on Dec. 26th to Dec. 31th.

share|improve this answer

Q (60)

 system"t 1000";.z.ts:{if[.z.d~2012.12.25;1"It’s Christmas"]}
share|improve this answer
    
> If it is not Christmas, you must somehow wait until Christmas and then print "It's Christmas". –  slackwear Mar 7 '12 at 13:55
    
My apologies, corrected to check every second if it's Christmas...so this will print "It's Christmas" 86,400 times every 25th December. Obviously you can alter this by increasing the timer, which is in milliseconds. –  sinedcm Mar 7 '12 at 14:20

R

RingTheBells <- function() cat("\nIt's Christmas!\n")
cd <- xmas <- as.POSIXlt(Sys.Date())
xmas$mon <- 11
xmas$mday <- 25
if(cd > xmas) xmas$year <- xmas$year + 1
Sys.sleep(difftime(xmas, cd, units="secs"))
RingTheBells()
share|improve this answer

PHP - 49 Characters

I got this quite short, so yeah.

<?while(date("dm")!="2512");echo"It's Christmas";

EDIT: Whoops, I printed something else than It's Christmas x3

share|improve this answer

Rebol (53 chars)

wait difference 25-Dec-14 now
print "It's Christmas"

Of course the above will only wait till Xmas 2014 :)

Here's a version that will wait for next Xmas day:

next-xmas: does [
    d: now
    make date! reduce [25 12 either all [d/month = 12 d/day > 25][d/year + 1][d/year]]
]

wait difference next-xmas now
print "It's Christmas"
share|improve this answer

Befunge-98 (36 chars)

Requires a Unicode-capable interpreter ('𐀀 is used for a literal 65536 and 'ఙ for 3097):

aa+y'𐀀%'ఙ-!#v_
s Christmas"<@,kd"It'

Explanation:

  • Use y with an argument of 20 to get the date field (aa+y)
  • Mask off the month & day portion by reducing mod-65536 ('𐀀%)
  • Subtract 3097 (= month * 256 + day) ('ఙ-)
  • If zero, print the string (abuses edge wrapping)
  • Otherwise repeat
share|improve this answer

C# (126)

using System;class P{static void Main(){while(DateTime.Now.Date.ToString("Md")!="1225");Console.WriteLine("It's Christmas");}}

Nicer for your battery:

C# (163)

using s=System;class P{static void Main(){s.Threading.Thread.Sleep(s.DateTime.ParseExact("1225","Md",null)-s.DateTime.Now);s.Console.WriteLine("It's Christmas");}}

edit

The second ("nicer for your battery") version does have a bit of an issue dec. 26th to dec. 31st I just thought of :P

Both versions can probably be shortened a bit more.

share|improve this answer

J, 54

'It''s Christmas'[(-^:(3 :'-.12 25-:1 2{6!:0''''')^:_)1
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.