38
5
Background
Some holidays have fixed, easy-to-remember dates, like Oct 31, Dec 25, etc. Some, however, want to be troublesome. They're specified as things like "the first Monday in September" or "the fourth Thursday in November". How am I supposed to know when that is?
All I know is that Thanksgiving is fast approaching, so I need a program to help me figure out when it is. Some people even say that it's tomorrow, so your program needs to be as short as possible to make sure I can re-type it in time.
The Challenge
Create a program or function that, given an up-to-four-digit year (e.g. 2015 or 1984), outputs or returns the date of the United States' Thanksgiving in that year. Thanksgiving is defined as the fourth Thursday of November according to the Wikipedia page. (Hint: that page also includes some interesting information on the date pattern.)
Input: a decimal number with a maximum of four digits representing a year in the Common Era (C.E.). Examples: 987, 1984, 2101
Output: the date, including month and day, on which Thanksgiving falls, or would fall if it existed, in that year. This may be in any reasonable format; use your best judgment. Use the Gregorian Calendar in all cases, even if it was not in use at the time.
(Note: Make sure to handle leap years correctly!)
Test cases Input 1:
2015
Output 1:
Nov 26
Input 2:
1917
Output 2:
Nov 22
Scoring
Submissions will be scored in bytes. I recommend this website to keep track of your byte count, though you can use any counter you like.
Bonuses
-25% to your score if you handle B.C.E. dates as negative numbers (e.g. -480 would be the year of the battle of Thermopylae).
Negative test case input:
-480
Corresponding output:
Nov 25
This is code-golf, so the lowest score wins!
Edit: I am marking Thomas Kwa's TI-BASIC submission as accepted. Don't let this discourage you from submitting new entries!
Leaderboards
Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.
To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:
# Language Name, N bytes
where N
is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:
# Ruby, <s>104</s> <s>101</s> 96 bytes
If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:
# Perl, 43 + 2 (-p flag) = 45 bytes
You can also make the language name a link which will then show up in the leaderboard snippet:
# [><>](http://esolangs.org/wiki/Fish), 121 bytes
var QUESTION_ID=64785,OVERRIDE_USER=45162;function answersUrl(e){return"http://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(e,s){return"http://api.stackexchange.com/2.2/answers/"+s.join(";")+"/comments?page="+e+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){answers.push.apply(answers,e.items),answers_hash=[],answer_ids=[],e.items.forEach(function(e){e.comments=[];var s=+e.share_link.match(/\d+/);answer_ids.push(s),answers_hash[s]=e}),e.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(e){e.items.forEach(function(e){e.owner.user_id===OVERRIDE_USER&&answers_hash[e.post_id].comments.push(e)}),e.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(e){return e.owner.display_name}function process(){var e=[];answers.forEach(function(s){var r=s.body;s.comments.forEach(function(e){OVERRIDE_REG.test(e.body)&&(r="<h1>"+e.body.replace(OVERRIDE_REG,"")+"</h1>")});var a=r.match(SCORE_REG);a&&e.push({user:getAuthorName(s),size:+a[2],language:a[1],link:s.share_link})}),e.sort(function(e,s){var r=e.size,a=s.size;return r-a});var s={},r=1,a=null,n=1;e.forEach(function(e){e.size!=a&&(n=r),a=e.size,++r;var t=jQuery("#answer-template").html();t=t.replace("{{PLACE}}",n+".").replace("{{NAME}}",e.user).replace("{{LANGUAGE}}",e.language).replace("{{SIZE}}",e.size).replace("{{LINK}}",e.link),t=jQuery(t),jQuery("#answers").append(t);var o=e.language;/<a/.test(o)&&(o=jQuery(o).text()),s[o]=s[o]||{lang:e.language,user:e.user,size:e.size,link:e.link}});var t=[];for(var o in s)s.hasOwnProperty(o)&&t.push(s[o]);t.sort(function(e,s){return e.lang>s.lang?1:e.lang<s.lang?-1:0});for(var c=0;c<t.length;++c){var i=jQuery("#language-template").html(),o=t[c];i=i.replace("{{LANGUAGE}}",o.lang).replace("{{NAME}}",o.user).replace("{{SIZE}}",o.size).replace("{{LINK}}",o.link),i=jQuery(i),jQuery("#languages").append(i)}}var ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,]*[^\s,]),.*?([\d.]+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:290px;float:left}table thead{font-weight:700}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"> <div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table> </div><div id="language-list"> <h2>Winners by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table> </div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table> <table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody> </table>
26For anyone who is wondering when Thanksgiving will be coming up this year: Thanksgiving will be tomorrow . – TheNumberOne – 2015-11-25T18:07:09.173
6But I had thanksgiving on October 10th? I'm sorry to say your question is late. – JimmyJazzx – 2015-11-25T18:17:28.967
2@JimmyJazzx brings up a good point. In Canada, Thanksgiving is on the second Monday in October. You should probably specify that you are talking about United States' Thanksgiving. – TheNumberOne – 2015-11-25T18:23:20.533
@TheNumberOne Thanks! I've edited the challenge to clarify. – jqblz – 2015-11-25T18:31:47.287
Must we use that byte counter or may we use that byte counter or may we use our own? Some languages like APL, have their own character set, which counts bytes differently – Downgoat – 2015-11-25T18:43:25.680
5@Downgoat Use whichever byte counter you want. That's just the one I use personally and would recommend. – jqblz – 2015-11-25T18:44:24.727
Do you want the de facto Thanksgiving dates or the dates that follow your rule? Since 1789 (in the US), Thanksgiving Day was observed on inconsistent days. From 1863 to 1940 it was on the last Thursday of November. Since then it has been celebrated on the 4th Thursday of that month. https://www.archives.gov/legislative/features/thanksgiving/
– DavidC – 2015-11-25T19:05:11.460@DavidCarraher The legal holiday is on the fourth Thursday.
– LegionMammal978 – 2015-11-25T19:07:03.2601@LegionMammal978, only since 1941 has Thanksgiving been taken to be on the 4th Thursday, even though it was officially celebrated before then. – DavidC – 2015-11-25T19:12:35.693
23"Some holidays have fixed, easy-to-remember dates, like Oct 31, Dec 25" Those examples are easy to remember because they're really the same day: octal 31 == decimal 25. ;-) – Adrian McCarthy – 2015-11-25T19:26:59.823
@Adrian McCarthy That wasn't unintentional. – jqblz – 2015-11-25T19:36:38.427
What should be the output for
0
? – ETHproductions – 2015-11-25T19:47:01.537@Eth I've got Nov 22 with my program, probably posting it soon. – nicael – 2015-11-25T19:52:24.690
@nicael I get that for
0
and1
. – LegionMammal978 – 2015-11-25T19:52:54.750@AdrianMcCarthy Whenever I forget when Christmas is, I always use this method of converting octal 31 to decimal :) – user41805 – 2015-11-25T20:09:15.527
7For an answer that does the bonus, should there be a year 0 between -1 and 1 or not? – feersum – 2015-11-25T21:07:33.470
What about Easter? – Édouard – 2015-11-25T22:37:28.997
2
Most answers using buildin date functions apply the Julian calendar before 1582 and implement the date jump from Oct 4 to Oct 15 in 1582. The challenge states that we should "use the Gregorian calendar in all cases, even if it was not in use at the time.", so for example 1400 isn't a leap year. Also, the gap of 10 days in 1582 (or 11 days in 1752, which is more accurate for America) makes 1582 and 1752 good test cases. I'd say the correct answer for 1752 is Nov 23. 1582 would be Nov 25 and 1400 is Nov 27.
– agtoever – 2015-11-25T22:55:49.537Is outputting the day of the year (e.g.
329
forNov 26
in a common year) acceptable? – Mego – 2015-11-26T04:15:20.777@Mego No, you need the month and day. – jqblz – 2015-11-26T04:20:23.650
Are we allowed to output in the format
MMDD
(e.g.1126
for Nov. 26)? – lirtosiast – 2015-11-26T21:21:25.360@ThomasKwa Sure, you can do MMDD. – jqblz – 2015-11-26T22:06:58.777