7
Goal is to write the shortest possible C89 and C99-compliant single-module C program which will compute and print out a single-line string whose sort order will correspond with the date given by the predefined __DATE__
macro (in other words, later dates will yield later-sorting strings). The programmer is free to arbitrarily select the mapping between dates and strings, but every entry should specify the mapping and it should be obvious that it will sort correctly (e.g. a programmer could decide to compute (day + month*73 + year*4129) and output that as a number, though it's likely that particular encoding would probably require a longer program than some others).
The program should yield identical results on any standards-compliant compiler on which 'int' is 32 bits or larger and both source and target character sets are 7-bit ASCII, and should not rely upon any implementation-defined or undefined behavior nor print any characters outside the 32-126 range except for a single newline at the end. The program should contain the following aspects indicated below (replacing «CODE» with anything desired):
♯include <stdio.h> «CODE»int main(void){«CODE»}
All output shall be produced by the printf
at the end (i.e. the correct value will be in an int
called z
). The required elements will be included in the character total for each entry.
Code should operate correctly for all future dates through Dec 31 9999
. Libraries which are standard in both C89 and C99 may be used, provided that appropriate headers are included. Note that standard date libraries may not be assumed to operate beyond the Unix limits.
Note: Code is permitted to perform Undefined Behavior if and only if the __DATE__
macro expands to a macro other than a date between Feb 11 2012
and Dec 31 9999
(expressed in that format, using C-standard abbreviated English month names Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec)
Note 2: For compliance with C99 standards, 'main' should return an arbitrary, but defined value, and the newline is required. The first requirement, btw, adds 7 characters to my best effort; the single new-line adds 5.
4This seems kind of pointless because there is already a UNIX timestamp that you can perform operations on to determine the date, and is constantly increasing. also, is this code golf? you should tag it as such – Blazer – 2012-02-10T23:38:25.343
Unix date does not match criteria to reach 2099:
LC_ALL=C date -d "2060/02/13" date: invalid date
2060/02/13'` – user unknown – 2012-02-11T00:02:22.817@Blazer: I would consider the use of a Unix timestamp to violate the prohibition against date-parting routines. The primary goal is to figure out an efficient way of converting "Jan/Feb/Mar/etc" into a sortable number. – supercat – 2012-02-11T00:17:00.677
@userunknown: Depends on the unix. 32 bit unix time runs out in 2038, I think, but 64 bit unix time...Scientific Linux 5.3 gives
$ LC_ALL=C date -d "2060/02/13" Fri Feb 13 00:00:00 CST 2060
on x86_64. – dmckee --- ex-moderator kitten – 2012-02-11T00:47:24.7101That said, this question need to be formed as a CodeGolf.Se compliant puzzle (with the winning condition specified and the like). Supercat, please feel welcome here, but also read the FAQ and examine some of the other questions here. I'll be happy to reopen this when you've tuned it up a little. Just submit a flag for help. – dmckee --- ex-moderator kitten – 2012-02-11T00:49:15.427
so is this for any language, or is it only limited to C? – Blazer – 2012-02-11T08:03:23.697
@Blazer: I'd be interested in seeing approaches for approaching similar problems in other languages, but for purposes of scoring I think it's probably easiest to restrict "official entries" to C programs which are compliant with C89/C99. – supercat – 2012-02-11T08:15:36.073
@supercat: I'll submit my Python answer anyways, then! :) – Blazer – 2012-02-11T08:18:23.440
I have a 185 char C program but I can't post it. This makes me sad :-( – Gareth – 2012-02-11T11:11:51.487
Thanks @supercat, that's just the kind of thing we're looking for. Personally I'm not a big fan of language limits, but there seems to be a consensus to allow them (or at least no consensus to not allow them). – dmckee --- ex-moderator kitten – 2012-02-11T18:59:44.670
@dmckee: I think the issue is that different language have different amounts of unavoidable baggage, and the number of characters required to solve a problem in one language may be less than the number of characters to 'get out the starting gate' in another. My original concept for this problem was to have a function return an 'int', but there seems to be a preference for standalone programs. Using a function rather than a program could have eliminate the #include, for example, though for another puzzle I might specify a 'test' program with which the function must work. – supercat – 2012-02-11T22:28:35.907
@Gareth: The topic is open now, if you'd like to come back. My program, including the trailing newline and a defined return value, is 115 characters and I wouldn't be at all surprised if it could shrink a tiny bit. – supercat – 2012-02-11T22:29:23.897
Challenges on the site come in all varieties, there are those that specify functions and those that specify whole programs. As for the differences between language, there is no consensus on who to deal with it: see Language Handicap, should imports/includes count in golf, What programming language should we consider for the code-golf solution ? and may other questions on meta.
– dmckee --- ex-moderator kitten – 2012-02-11T22:38:25.533@supercat 115! I'm embarrassed by my pitiful attempt now... – Gareth – 2012-02-11T22:41:53.477
@Gareth: I'll admit I have something of an unfair advantage, since I was trying to figure out a way to have an embedded system convert
__DATE__
into a version number, stumbled upon a really nice trick, and then decided it would make a cute puzzle. Admittedly my goal was smallest compiled code size, but I think my trick works out well by any metric. – supercat – 2012-02-11T23:39:28.253@dmckee: Wow. The first time in my live I'm thinking about a 64 bit system. :) – user unknown – 2012-02-12T02:06:57.010