r/dailyprogrammer • u/nint22 1 2 • Jan 16 '13
[01/16/13] Challenge #117 [Intermediate] Mayan Long Count
(Intermediate): Mayan Long Count
The Mayan Long Count calendar is a counting of days with these units: "* The Maya name for a day was k'in. Twenty of these k'ins are known as a winal or uinal. Eighteen winals make one tun. Twenty tuns are known as a k'atun. Twenty k'atuns make a b'ak'tun.*". Essentially, we have this pattern:
1 kin = 1 day
1 uinal = 20 kin
1 tun = 18 uinal
1 katun = 20 tun
1 baktun = 20 katun
The long count date format follows the number of each type, from longest-to-shortest time measurement, separated by dots. As an example, '12.17.16.7.5' means 12 baktun, 17 katun, 16 tun, 7 uinal, and 5 kin. This is also the date that corresponds to January 1st, 1970. Another example would be December 21st, 2012: '13.0.0.0.0'. This date is completely valid, though shown here as an example of a "roll-over" date.
Write a function that accepts a year, month, and day and returns the Mayan Long Count corresponding to that date. You must remember to take into account leap-year logic, but only have to convert dates after the 1st of January, 1970.
Author: skeeto
Formal Inputs & Outputs
Input Description
Through standard console, expect an integer N, then a new-line, followed by N lines which have three integers each: a day, month, and year. These integers are guaranteed to be valid days and either on or after the 1st of Jan. 1970.
Output Description
For each given line, output a new line in the long-form Mayan calendar format: <Baktun>.<Katun>.<Tun>.<Uinal>.<Kin>.
Sample Inputs & Outputs
Sample Input
3
1 1 1970
20 7 1988
12 12 2012
Sample Output
12.17.16.7.5
12.18.15.4.0
12.19.19.17.11
Challenge Input
None needed
Challenge Input Solution
None needed
Note
Bonus 1: Do it without using your language's calendar/date utility. (i.e. handle the leap-year calculation yourself).
Bonus 2: Write the inverse function: convert back from a Mayan Long Count date. Use it to compute the corresponding date for
14.0.0.0.0.
11
u/domlebo70 1 2 Jan 16 '13 edited Jan 16 '13
I decided to take a different approach. I wanted to create a nice, fluent, DSL like API that takes advantages of some of the cool shit you can do in Scala, enabling some nice use cases. Here is an example:
I use Scala implicits to allow pimping of the Int class. So these methods exist on the Int class, and can be chained together. We can create a Mayan time like so:
We can subtract time from this time:
This allows fluent creation of Mayan dates. What's more, is we can now convert directly to a JodaTime/Java Date object:
And back again (note, the asMayan method exists transparently on the DateTime object. Pretty awesome):
We also have the usual toString methods like so:
Answering the bonus 2 challenge questions:
Here is my code: https://gist.github.com/4546077
4
u/srhb 0 1 Jan 16 '13 edited Jan 16 '13
Haskell, including bonus 1, doing leap year calculations by walking through julian days. This might mess up dates if extended beyond 1582 -- I think the leap year calculation is wrong by then. For the same reason, conversion back to gregorian (bonus 2) is omitted. Because PAIN.
6
u/pdewacht 0 1 Jan 16 '13
C has a division function! Isn't that neat?
3
u/skeeto -9 8 Jan 16 '13
Conversion including inverse function (bonus 2).
JavaScript,
Usage:
Emacs Lisp,
3
u/foxlisk Jan 16 '13
okay, here it is in (hilariously verbose) python. both bonuses complete. dealing with dates sucks.
2
u/Unh0ly_Tigg 0 0 Jan 16 '13
For some reason, I believe that 'uinal' should be 'Winal', but that is mostly due to 'Winal' being what's used in the Wikipedia page for Mayan Calendar Long Count.
2
u/aredna 1 0 Jan 16 '13
It looks like your challenge input is actually for Bonus 2 instead of the challenge.
2
u/foxlisk Jan 16 '13
So I'm working ont his and I was comparing my sample output to your example output. Is the given sample output supposed to be correct for the given sample input? Because if so, you have violated your own premises by having the output of 1 1 1970 be 12.0.0.0.0
edit: and, if not, can we have some sample output to check ourselves against? besides jan 1 1970
2
u/DannyP72 Jan 16 '13
Ruby
4
u/RainbowNowOpen Jan 16 '13 edited Jan 16 '13
I think your sample input of "1 1 1970" should result in sample output of "12.17.16.7.5", per the question description. (Not "12.0.0.0.0" as it reads right now.)
4
u/[deleted] Jan 16 '13
First intermediate challenge. I'm gonna give it a shot coach! Java btw.
4
u/drch 0 1 Jan 16 '13 edited Jan 16 '13
C# with Bonus 2
4
u/lawlrng 0 1 Jan 16 '13 edited Jan 16 '13
Python. Not the cleanest solution, but I absolutely loathe dealing with date stuff. Bonus 1 and Bonus 2 are done, however.
Output:
4
u/[deleted] Jan 16 '13
Ruby
1
u/kcoPkcoP Jan 16 '13
Here's some ugly, ugly code. Bonus 1 done.
Any and all comments and criticism are very welcome, I'm a complete newbie and need the feedback.
The classes:
Date: http://pastebin.com/FsBpPuTS
MayanDate: http://pastebin.com/eP6XvGq7
3
u/aredna 1 0 Jan 16 '13
MSSQL, No Bonus. One day the posting bug that prevents SQL CTEs will be fixed, I hope.
This was purposely written to be easy to read in case anyone wants to follow the logic through. Bonuses to come later.
3
u/TheSpaceRat Jan 16 '13
I just discovered this sub yesterday, I love the concept. This is my first submission.
It uses Jan 1 1970 as a reference date and does not calculate anything before this date.
I did not use any intrinsic time/date functions. My approach was simply to count the days and divide amongst the time units. I'm sure there are better methods, but this is the first thing that came to mind.
I did not implement Bonus 2 or any type of error checking.
Anyhow, here is my Fortran 95/2003 attempt. Yes, Fortran, and yes there is a special place in hell for me. I made a point to use some of the more modern features (basic OO, pointers, recursion, freeform) to try and demonstrate that this aint quite your great grandpas Fortran.
2
u/[deleted] Jan 16 '13 edited Jan 16 '13
My solution in C++11, hilarious verbose too but with the 2 bonuses
2
u/adzeitor 0 0 Jan 16 '13 edited Jan 16 '13
haskell (only bonus 2, I'm too lazy to write diffGregorian :) )
2
u/jeff303 0 2 Jan 16 '13 edited Jan 17 '13
Here's my solution, in Python. I used a generator to handle the date duration calculations because it felt cleaner than the other approaches I could envision. It handles both bonuses (input formats may be intermixed), but doesn't handle any inputs that occur before the epoch (1 Jan, 1970) in either format.
I'm getting a different answer, however, for the Bonus 2 answer than some other people here so I may need to check that out.Fixed the bug in the leap year calc thanks to below comments.Input
Output
2
u/exor674 Jan 17 '13
C++, I believe this satisfies both bonuses.
Bonus 2:
2
u/bheinks 0 0 Jan 17 '13 edited Jan 17 '13
Python (with first bonus)
Probably could be a bit cleaner, but hey, it's my first intermediate. I enjoyed myself :)
Edit: takes input via console instead of file (missed that part)
2
u/math_geek Jan 17 '13
Java, both challenges.
Output:
2
u/supergenius1337 Jan 18 '13
I was able to complete the main challenge and I did the first bonus myself but I mildly screwed up the second challenge. Language is C++. If there's any way I could make my code not such a cluster, please tell me. As I said, the second bonus is about a week off for some reason. I think it has something to do with my attempt at handling leap years.
Like I said, any tips on shortening and/or getting bonus 2 right?
2
u/Toizi Jan 20 '13
C
code:
Sample input & output:
2
u/OniTux Jan 20 '13
In Java 7 (for new number notation) : https://gist.github.com/4582110
Unreliable for date before 1st January 1970.
Output :
2
u/marekkpie Jan 21 '13
Lua, represent!
Implicitly covers bonus 1 as Lua has no date class. A bit longer than usual, but intermediate challenges will do that:
2
u/deds_the_scrub Mar 13 '13
My solution in Python:
Output:
3
u/RainbowNowOpen Jan 16 '13 edited Jan 16 '13
Python, no bonus
The challenge input section currently reads "None needed". (I think a mod is working on this.) I will paste my output when the input appears again. But my code is above. :)
1
u/[deleted] Jan 17 '13
In Go:
1
u/[deleted] Jan 17 '13
Java, object oriented approach
1
u/[deleted] Jan 17 '13
In ugly Python:
1
u/Sonnenhut Jan 18 '13
Java:
1
u/Gotler Jan 19 '13
C#, no challenges.
1
u/jpverkamp Jan 25 '13
I've just discovered this subreddit and this problem in particular struct my fancy. Here's my solution in Racket, with a more detailed write up my blog: Gregorian/Mayan conversion
To start with, we need to convert from Gregorian dates to a number of days past 1 January 1970.
That's nice and closed form (except for the loop for leap years, is there a better way to do that?). Next is the reciprocal (so I can do the bonus):
With those two out of the way, the Mayan functions are almost trivial. To convert to days, it's just a matter of multiplication.
Finally, converting back. Multiple value returns and quotient/remainder make this really nice.
Finally, tie it all together:
And that's all she wrote. There's a testing framework on my blog post or you can see the entire source on GitHub.
This is a great resource for interesting programming projects. I'll definitely be checking back. :)