10
1
Introduction
tl;dr
In this challenge you have to calculate the moon's phase for a given date.
This challenge is inspired by the game psycho social audiovisual experiment "Superbrothers: Sword & Sworcery EP". In S:S&S EP the moon's phases are important to the outcome of the adventure as some events occur only at a specific point in time.
The question is: Which lunar phase is present on a specific date. Each main phase – from new moon to first quarter to full moon to third quarter – is about 7.38 days long. The whole lunar cycle is roughly 29.52 days. Based on these values various methods of calculation exist.1
Input
- A date based on the Gregorian calendar, between 1 January 1970 and 31 December 2116.
- You can choose one of the following formats:
yyyy-mm-dd
,dd.mm.yyyy
,dd/mm/yyyy
,yyyymmdd
orddmmyyyy
.
Output
Output the index [0-7]
of the lunar phase based on this zero-indexed array:
['New moon', 'Waxing crescent', 'First quarter', 'Waxing gibbous', 'Full moon', 'Waning gibbous', 'Third quarter', 'Waning crescent`]
Requirements
- You can write a program or a function. If you go with an anonymous function, please include an example of how to invoke it.
- Input is accepted from
STDIN
, command line arguments, as function parameters or from the closest equivalent. - This is code-golf so shortest answer in bytes wins.
- Built-ins or external libraries that calculate the moon phase are not allowed.2
- Standard loopholes are disallowed.
Tests
The values are: date | index of the phase | illumination | name
A full lunar cycle:
08.02.2016 | 0 | 0% | New moon
07.02.2016 | 7 | 2% | Waning crescent
07.02.2016 | 7 | 2% | Waning crescent
06.02.2016 | 7 | 6% | Waning crescent
05.02.2016 | 7 | 12% | Waning crescent
04.02.2016 | 7 | 19% | Waning crescent
03.02.2016 | 7 | 28% | Waning crescent
02.02.2016 | 7 | 37% | Waning crescent
01.02.2016 | 6 | 47% | Third quarter
31.01.2016 | 5 | 56% | Waning gibbous
30.01.2016 | 5 | 65% | Waning gibbous
29.01.2016 | 5 | 74% | Waning gibbous
28.01.2016 | 5 | 82% | Waning gibbous
27.01.2016 | 5 | 89% | Waning gibbous
26.01.2016 | 5 | 94% | Waning gibbous
25.01.2016 | 5 | 98% | Waning gibbous
24.01.2016 | 4 | 100% | Full moon
23.01.2016 | 3 | 100% | Waxing gibbous
22.01.2016 | 3 | 97% | Waxing gibbous
21.01.2016 | 3 | 93% | Waxing gibbous
20.01.2016 | 3 | 86% | Waxing gibbous
19.01.2016 | 3 | 77% | Waxing gibbous
18.01.2016 | 3 | 67% | Waxing gibbous
17.01.2016 | 3 | 56% | Waxing gibbous
16.01.2016 | 2 | 45% | First quarter
15.01.2016 | 1 | 33% | Waxing crescent
14.01.2016 | 1 | 23% | Waxing crescent
13.01.2016 | 1 | 14% | Waxing crescent
12.01.2016 | 1 | 7% | Waxing crescent
11.01.2016 | 1 | 2% | Waxing crescent
10.01.2016 | 0 | 0% | New moon
Random test cases:
14.12.2016 | 4 | 100% | Full moon
16.10.1983 | 3 | 75% | Waxing gibbous
04.07.1976 | 2 | 47% | First quarter
28.11.1970 | 0 | 0% | New moon
As most methods aren't accurate to a scientific level and you also get mixed results on different websites for a couple of these days, it is acceptable if your results are within a ± 1 day range.
Bonus
Reduce your byte count and withdraw:
- 15% – Print the actual name of the phase as listed in section Output instead of its index.
- 25% – Print the dates of the upcoming new and full moon separated by a whitespace or newline on empty input.
1 For example: Calculating phase on Wikipedia.
2 Sorry Mathematica.
My money's on Japt. – lirtosiast – 2016-01-19T16:51:15.353
How long does each phase last? You refer to four main phases lasting approximately 7 days, but there are 8 phases to deal with. – Sherlock9 – 2016-01-19T18:16:31.660
1I think to help me understand how long each phase should be, can you post a test case of about five consecutive days, or however long it takes to change from, say, "waxing gibbous" to "waning gibbous" by your reckoning? I am having trouble with the definitions because, for example, quarter moons are the instant of 50% illumination, so "first quarter" should only be on the day itself, with "waxing crescent" and "waning crescent" on the days before and after. But I'm not sure. – Sherlock9 – 2016-01-19T19:40:49.530
Alright then, I'll get started on my solution. Thanks for clearing some of this up. – Sherlock9 – 2016-01-19T19:59:07.150
@Sherlock9 I've updated the test cases with a full lunar cycle and some random values, including illumination of each day. Hopefully this is helpful. – insertusernamehere – 2016-01-20T14:03:07.967