บอกขั้นตอนทางจันทรคติ!


10

ท้าทาย

รับภาพดวงจันทร์เป็นสัญญาณเข้าให้ส่งออกเฟสของดวงจันทร์

ขั้นตอนของดวงจันทร์

โปรแกรมของคุณจะได้รับหนึ่งในภาพเหล่านี้ในรูปแบบ png และคุณต้องส่งออกเฟสตามที่กำหนด:

new moon

hexdump

waxing crescent

hexdump

first quarter

hexdump

waxing gibbous

hexdump

full moon

hexdump

waning gibbous

hexdump

third quarter

hexdump

waning crescent

hexdump

อินพุต

อินพุตจะเป็นเส้นทางไปยังไฟล์ 240 px คูณ 240 px png และจะเป็นหนึ่งในภาพด้านบน

ไบต์รูปภาพรับประกันว่าจะเหมือนกัน

การชนะ

รหัสที่สั้นที่สุดชนะ


1
เป็นโบนัสให้ตรวจสอบ gif ที่ยอดเยี่ยมนี้: upload.wikimedia.org/wikipedia/commons/b/ba/…
Beta Decay

คำตอบ:


9

Node.js , 145 ไบต์

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]

ลองออนไลน์! (สร้างไฟล์จำลองที่มีขนาดเท่ากัน)

อย่างไร?

เราแค่ดูขนาดของไฟล์และแปลงเป็นดัชนีในตารางค้นหาสองตาราง

ส่วนที่หนึ่ง:

 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

ส่วนที่สอง:

 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

7

Python 2 , 223 222 ไบต์

-1 ไบต์ขอบคุณ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))- จะคืนพิกเซล RGBA ที่x,y
getpixel((n*48,99))[2]for n in[1,2,3,4]- จะคืนช่องสีฟ้าของเส้นกลางซึ่งn*48 ... for n in 1,2,3,4จะเป็น 4 จุดที่แสงแดดอาจปกคลุม
n*getpixel(...)- จะสร้างค่าที่แตกต่างกันสำหรับแต่ละคอลัมน์
sum(...)%13- ค่าเหล่านี้จะถูกรวมเข้าด้วยกันและ%13ใช้เพื่อให้ได้เอกลักษณ์ ค่าของแต่ละเฟสที่จะใช้เป็นดัชนีสำหรับรายการเฟส
พิกเซลนั้นอยู่ภายในวงกลมสีแดงโดยประมาณ:
ภาพดวงจันทร์พร้อมไฮไลต์พิกเซล


5

ทับทิมขนาด 131 ไบต์

->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 พบโดย brute force - การใช้ 699th byte ของโมดูโลไฟล์ 7 ให้ดัชนีในตารางการค้นหาแรก



1

PHP (> = 5.4), 199 197 ไบต์

(-2 ไบต์โดยการเล่นกอล์ฟมากขึ้น)

<?$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],'_',' ');

วิธีเรียกใช้:

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

ตัวอย่าง:

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

หมายเหตุ:

  • -d error_reporting=0ตัวเลือกที่จะใช้ในการประกาศไม่เอาท์พุท / คำเตือน
  • -d short_open_tag=1จะต้องอนุญาตให้แท็กสั้น
  • หากคุณใช้httpsURL เหมือนตัวอย่างด้านบนOpenSSLก็ควรเปิดใช้งานเช่นกัน

อย่างไร?

รับขนาดไฟล์ (ไบต์) และสร้างหมายเลขเฉพาะสำหรับสูตรนี้:

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

สูตรนี้สร้างตัวเลขตั้งแต่ 0 ถึง 8 โดยมีเพียง 3 หมายเลขเท่านั้น

┌─────────────────┬───────┬─────────┬─────┬────────────────────────┐
│      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 │
└─────────────────┴───────┴─────────┴─────┴────────────────────────┘

แนวทางก่อนหน้า:

PHP (> = 5.4), 251 ไบต์

<?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];

วิธีเรียกใช้:

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

ตัวอย่าง:

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

หมายเหตุ:

  • -d error_reporting=0ตัวเลือกที่จะใช้ในการประกาศไม่เอาท์พุท / คำเตือน
  • -d short_open_tag=1จะต้องอนุญาตให้แท็กสั้น
  • PHP ต้องมีGDและควรเปิดใช้งาน
  • หากคุณใช้httpsURL เหมือนตัวอย่างด้านบนOpenSSLก็ควรเปิดใช้งานเช่นกัน

อย่างไร?

ตรวจสอบสีของ 4 พิกเซลในภาพที่ที่40,120, 80,120, 160,120และ200,120และตัดสินใจในระยะดวงจันทร์จากสีเหล่านั้น

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.