4
I'm a code golf novice, but I'm eager to give it a go.
My challenge is: Write the shortest program in C which takes in a date D
as input and outputs the date D + 1 day
Rules:
- program must compile only with
gcc progname.c -o progname
So no compiler/makefile tricks (if there are any) - compiler warnings don't matter - The input and output format must be
day/month/year
zero padding of single digit values does not matter:1/3/1998
is equal to01/03/1998
- It doesn't matter how you give the the program it's input date, it can be a program argument,
scanf
whatever. Output must be tostdout
however - February has 29 days when the year is divisible by 4
- No date validation necessary, i.e. assume the input is always a valid date.
- any integer year should be supported, i.e.
INT_MIN -> INT_MAX
Shortest code length wins, and there are extra points for extra bastardised C.
My solution uses 126 123* 121 characters
Good luck
*s="%i/%i/%i",m,d;main(y){scanf(s,&d,&m,&y);printf(s,d,m,(d=d%((m>7^m&1)+27+(m-2?3:y%4?1:2))+1)-1?y:(m=m%12+1)-1?y:y+1);}
*because of an idea Gareth's code gave me
3February doesn't have 29 days when the year is divisible by 100 unless it is also divisible by 400. Or is your last rule there to simplify matters slightly? – Gareth – 2011-08-09T19:16:50.267
@Gareth I think so. – FUZxxl – 2011-08-09T19:20:14.703
@Gareth, yes it's a simplification. – Griffin – 2011-08-09T19:20:33.937
3Code golf is usually open for all languages, except if the problem can only be solved in one language. Please lift the restriction on using C only. – FUZxxl – 2011-08-09T19:21:12.050
@FUZxxl "Usually"... Not this time :) – Griffin – 2011-08-09T19:26:13.773
@Griffin Would you mind reading this?
– FUZxxl – 2011-08-09T19:49:05.150@FUZxxl, I read it but I don't agree, if it's any language, then really it's to who knows the least verbose language for the problem. This way, everyone who wants to play, is on a level playing field, it's also a test of how much you can push the given language and the restraint should force more creativity. Anyway, as the code-golf tag states: "Code-golf is a competition to solve a particular problem in the fewest bytes of source code." - Doing it in C is part of the problem. No one is forcing you to play though :) – Griffin – 2011-08-09T19:58:31.393
To whomever voted me down, care to explain it? You either take part in the challenge or you don't. What possible objective reason was there for the down-vote? – Griffin – 2011-08-09T21:06:01.467
@Griffin The downvoter probably don't likes the idea of having a C-only competition. – FUZxxl – 2011-08-09T21:13:50.927
@FUZxxl, so what, I should be down-voted? Or should they just not take part instead? What a joke, thought this site was for a bit of fun. – Griffin – 2011-08-09T21:17:35.507
@Griffin I did not vote down. I accept the conditions you give, but suggest changes. I just want to explain what reasoning (s)he has. – FUZxxl – 2011-08-09T21:18:54.570
Further clarification needed: a) Should the input get checked for validity, b) what is the year range to handle (e.g should BC years get handled)? – Harry K. – 2011-08-09T21:49:34.143
@Harry, I've added that info, thanks for catching that. – Griffin – 2011-08-09T21:55:08.767
I downvoted later, but not for the language restriction, but for the idea of leap-years. @FUZxxl: You can't explain the reasons somebody else had, if you don't know them. – user unknown – 2011-08-09T23:05:33.487
On a 2nd thought, the problem doesn't make sense now! For example, how will the program respond on an input like this: -300/20000/-32768 – Harry K. – 2011-08-09T23:10:08.660
@Harry, That's not a valid date format, the range I have was for years. days can only be from 1 to 30 and months from 1-12, clearly there is no restriction on number of years - Just saw my edit mistake, sorry harry, I've fixed it now. – Griffin – 2011-08-09T23:11:49.420
@Griffin: Still, the best way I know to handle such tasks is to convert first a Gregorian date to a Julian Day Number, but there are several issues to consider for really early years, which I really doubt they fit in just 126 chars (before Gregorian year 400 for example) . – Harry K. – 2011-08-09T23:29:07.943
@Herry, man its just a simple, if you go into all of the intricacies of dates, you'll never finish defining the challenge. If you reach the last day of the month, you go to the next month, same with last day of the year, you go to the next year. Take into account my simplified leap year and thats it. – Griffin – 2011-08-09T23:41:52.453
@Griffin: Nah, I think I'll skip this one altogether, it's too (over)simplified and tailored for my taste after all. But no down-votes from me ;) Good luck to all who'll participate though! – Harry K. – 2011-08-09T23:48:47.740
i think more efficient count bite not characters. – None – 2014-07-07T12:59:21.333
@Harry, sorry to hear it doesn't interest you enough, but it's pretty hard to get it down to so little characters. – Griffin – 2011-08-11T20:41:32.317
You can still have the best C-language entry in a challenge that is open to other languages. I don't see the purpose of restricting it. – recursive – 2011-08-19T21:35:55.527