Tell me the Lunar Phase!

10

2

Challenge

Given an image of the Moon as input, output the phase of the Moon.

Phases of the Moon

Your program will be supplied one of these images in png format and you must output the phase exactly as given:

new moon

hexdump

waxing crescent

hexdump

first quarter

hexdump

waxing gibbous

hexdump

full moon

hexdump

waning gibbous

hexdump

third quarter

hexdump

waning crescent

hexdump

Input

The input will be the path to a 240 px by 240 px png file and will be the one of the images above.

The image bytes are guaranteed to be the same.

Winning

Shortest code wins

Beta Decay

Posted 2018-07-20T15:43:39.397

Reputation: 21 478

1

As a bonus, check out this cool gif: https://upload.wikimedia.org/wikipedia/commons/b/ba/Lunar_libration_with_phase_Oct_2007_450px.gif

– Beta Decay – 2018-07-20T15:45:24.867

Answers

9

Node.js, 145 bytes

p=>'third/waning/first/full/waxing/new'.split`/`[(s=require('fs').statSync(p).size)%418%6]+' '+'quarter/crescent/gibbous/moon'.split`/`[s%12%9%4]

Try it online! (generates dummy files with the same sizes)

How?

We just look at the size of the file and convert it to indices in two lookup tables.

First part:

 phase | file size | mod 418 | mod 6 | mapped to
-------+-----------+---------+-------+-----------
   0   |    3451   |    107  |    5  | new
   1   |    6430   |    160  |    4  | waxing
   2   |    5144   |    128  |    2  | first
   3   |    7070   |    382  |    4  | waxing
   4   |    5283   |    267  |    3  | full
   5   |    7067   |    379  |    1  | waning
   6   |    4976   |    378  |    0  | third
   7   |    6337   |     67  |    1  | waning

Second part:

 phase | file size | mod 12 |  mod 9 |  mod 4 | mapped to
-------+-----------+--------+--------+--------+-----------
   0   |    3451   |     7  |     7  |    3   | moon
   1   |    6430   |    10  |     1  |    1   | crescent
   2   |    5144   |     8  |     8  |    0   | quarter
   3   |    7070   |     2  |     2  |    2   | gibbous
   4   |    5283   |     3  |     3  |    3   | moon
   5   |    7067   |    11  |     2  |    2   | gibbous
   6   |    4976   |     8  |     8  |    0   | quarter
   7   |    6337   |     1  |     1  |    1   | crescent

Arnauld

Posted 2018-07-20T15:43:39.397

Reputation: 111 334

7

Python 2, 223 222 bytes

-1 byte thanks to OMᗺ

lambda p:'new moonzzfull moonzzfirst quarterzzwaxing crescentzzwaning gibbouszzwaxing gibbouszthird quarterzwaning crescent'.split('z')[sum(n*Image.open(p).getpixel((n*48,99))[2]for n in[1,2,3,4])%13]
from PIL import Image

getpixel((x,y)) - will return the RGBA pixel at x,y
getpixel((n*48,99))[2]for n in[1,2,3,4] - will return the blue channel of the middle line, where n*48 ... for n in 1,2,3,4 will be 4 points where the sunlight may cover
n*getpixel(...) - will generate a different value for each column
sum(...)%13 - these values are added together and %13 is used to get an unique value to each phase, that will be used as index for the phase list
The pixels are approximately inside the red circles:
moon picture with pixels highlighted

Rod

Posted 2018-07-20T15:43:39.397

Reputation: 17 588

5

Ruby, 131 bytes

->f{f=open(f,'rb').read;%w[first third waxing new full waning][f[699].ord%7]+' '+%w[x moon gibbous quarter crescent][f[998].ord%5]}

Byte offsets found by brute force -- taking the 699th byte of the file modulo 7, for instance, gives an index into the first lookup table.

Doorknob

Posted 2018-07-20T15:43:39.397

Reputation: 68 138

2

Python 2, 196 165 bytes

lambda f:'first quarter|new moon|waning crescent|waxing gibbous|third quarter|full moon|waxing crescent|waning gibbous'.split('|')[sum(map(ord,open(f).read()))%41%9]

Try it online!

ბიმო

Posted 2018-07-20T15:43:39.397

Reputation: 15 345

1

PHP (>=5.4), 199 197 bytes

(-2 bytes by more golfing)

<?$s=strlen(file_get_contents($argv[1])).'';echo strtr([waning_crescent,waning_gibbous,new_moon,0,waxing_crescent,waxing_gibbous,full_moon,first_quarter,third_quarter][($s[0]+$s[3])%11-2],'_',' ');

To run it:

php -d error_reporting=0 -d short_open_tag=1 <filename> <image_path>

Example:

php -d error_reporting=0 -d short_open_tag=1 lunar_phase.php https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Moon_phase_6.svg/240px-Moon_phase_6.svg.png

Notes:

  • The -d error_reporting=0 option is used to not output notices/warnings.
  • The -d short_open_tag=1 is required to allow short tags.
  • If you are using a https URL like the example above, OpenSSL should also be enabled.

How?

Gets file size (bytes) and creates a unique number for it by this formula:

((<first_bytes_digit> + <fourth_bytes_digit>) % 11) - 2

This formula generates numbers from 0 to 8 with only a 3 missing.

┌─────────────────┬───────┬─────────┬─────┬────────────────────────┐
│      Phase      │ Bytes │ 1st+4th │ %11 │ -2 (position in array) │
├─────────────────┼───────┼─────────┼─────┼────────────────────────┤
│ new moon        │  3451 │ 3+1=4   │   4 │                      2 │
│ waxing crescent │  6430 │ 6+0=6   │   6 │                      4 │
│ first quarter   │  5144 │ 5+4=9   │   9 │                      7 │
│ waxing gibbous  │  7070 │ 7+0=7   │   7 │                      5 │
│ full moon       │  5283 │ 5+3=8   │   8 │                      6 │
│ waning gibbous  │  7067 │ 7+7=14  │   3 │                      1 │
│ third quarter   │  4976 │ 4+6=10  │  10 │                      8 │
│ waning crescent │  6337 │ 6+7=13  │   2 │                      0 │
└─────────────────┴───────┴─────────┴─────┴────────────────────────┘

Previous approaches:

PHP (>=5.4), 251 bytes

<?foreach([4,8,16,20]as$w){$a+=imagecolorat(imagecreatefrompng($argv[1]),$w*10,120)>1e7;$a&&$w<5?$b=-2:0;}$x=explode('_','full moon_waning gibbous_third quarter_waning crescent_new moon_waxing crescent_first quarter_waxing gibbous');echo$x[$a*++$b+4];

To run it:

php -d error_reporting=0 -d short_open_tag=1 <filename> <image_path>

Example:

php -d error_reporting=0 -d short_open_tag=1 lunar_phase.php https://upload.wikimedia.org/wikipedia/commons/thumb/c/c9/Moon_phase_6.svg/240px-Moon_phase_6.svg.png

Notes:

  • The -d error_reporting=0 option is used to not output notices/warnings.
  • The -d short_open_tag=1 is required to allow short tags.
  • PHP must have GD and it should be enabled.
  • If you are using a https URL like the example above, OpenSSL should also be enabled.

How?

Checks for the color of 4 pixels in the image at 40,120, 80,120, 160,120 and 200,120 and decides on the moon phase from those colors.

Night2

Posted 2018-07-20T15:43:39.397

Reputation: 5 484