สุริยุปราคาผ่านกล้องรูเข็ม


28

ความท้าทายนี้เป็นความท้าทายแบบ ASCII ที่เรียบง่ายซึ่งได้รับแรงบันดาลใจจากสุริยุปราคาที่เกิดขึ้นเมื่อวันที่ 21 สิงหาคม 2017 เมื่อได้รับการป้อนข้อมูล0 <= n <= 4ให้ส่งออกระยะที่สอดคล้องกันของคราสที่อธิบายไว้ด้านล่าง:

n=0:
   *****
 **     **
*         *
*         *
**       **
  *******

n=1:
   *****
 **  *****
*   *******
*   *******
**   ******
  *******

n=2:
   *****
 *********
***********
***********
***********
  *******

n=3:
   *****
 *****  **
*******   *
*******   *
******   **
  *******

n=4:
   *****
 **     **
*         *
*         *
**       **
  *******

กฎระเบียบ

  • คุณสามารถดัชนี 0 หรือ 1 ระบุสิ่งที่คุณเลือก
  • ตัวอักษรที่ใช้คือช่องว่างและ*คุณสามารถใช้อักขระที่พิมพ์ได้สำหรับ*(นอกเหนือจากช่องว่าง)
  • ช่องว่างต่อท้ายเป็นตัวเลือก (คุณอาจมีหรือไม่มีก็ได้)
  • นี่คือไบต์ต่ำสุดคือผู้ชนะ

3
@ Mr.Xcoder ฉันต่อต้านการติดแท็กความท้าทายในการรับอินพุตเป็นkolmogorov ที่ซับซ้อนแม้ว่ามันจะขึ้นอยู่กับดุลยพินิจของ OP
Erik the Outgolfer

15
Bummer ว่ามันไม่สมมาตรจากบนลงล่าง
AdmBorkBork

นอกจากนี้คราสก็เริ่มขึ้นแล้ว ...
Erik the Outgolfer

@AdmBorkBork ใช่ฉันสามารถบันทึกไบต์แล้ว ...
Erik the Outgolfer

7
"คุณสามารถใช้อักขระใดก็ได้สำหรับ*" ... รวมถึงช่องว่างได้หรือไม่ ;)
Hagen von Eitzen

คำตอบ:


13

Python 2 , 161 149 142 135 ไบต์

lambda n,u=u' *':u'''   *****
 ****
**
**
****
  *******'''.translate({1:u[0<n<3],2:u[0<n<4],3:u[1<n<4]})

ลองออนไลน์!

-7 ต้องขอบคุณนาย Xcoder


9
การละเมิดที่ดีของ unprintables
Zacharý

ไม่เข้าใจคำตอบนี้อย่างเต็มที่จนกว่าฉันจะคัดลอกไปไว้ใน Emacs ยอดเยี่ยม!
Silvio Mayolo

@SilvioMayolo Umm, what did you do with Emacs?
Erik the Outgolfer

I was just looking at the answer on this page and didn't understand how it worked. Emacs shows all the hidden characters as ^A, ^B, ^C, etc.
Silvio Mayolo

@SilvioMayolo Oh, that's because unprintables have a representation that looks somewhat like Unicode chars.
Erik the Outgolfer

9

Charcoal, 82 81 55 43 bytes

-38 bytes thanks to Neil!

Nν”{“⟲FEd⧴_³⟲”‖O¿﹪ν⁴«F﹪ν²G↗³↑²↖²↙³*↑¤*¿⁼ν¹‖

Try it online! Link is to verbose version.

I did for the heck of it. :P I'll probably get out-golfed by 40 bytes. 26 38 bytes... Close enough?


1
I made some simplifications to your basic algorithm: Try it online!
Neil

2
I got out-golfed in my logic instead of my inability to use Charcoal. >_> Thanks!
totallyhuman

1
Looks like printing the outer "circle" works out shortest. I was also creative with the polygon for the odd inputs: Try it online!
Neil

1) Dammit, I thought I was being clever with PolygonHollow. :P 2) Ohh, nice. Thanks!
totallyhuman

5

Cinnamon Gum, 70 bytes

Hexdump:

0000000: 6c33 5053 5050 d002 012e 20a5 0002 4026  l3PSPP.... ...@&
0000010: 9001 0568 6c20 07a6 0648 4080 b521 8a19  ...hl ...H@..!..
0000020: 30a6 1644 1093 0de3 a098 6184 6206 422d  0..D......a.b.B-
0000030: 6136 c20c 6374 3380 3cb8 5aa0 1436 36ba  a6..ct3.<.Z..66.
0000040: 5f4c 280f 0f00                           _L(...

Try it online!

I have been waiting so long to find out how to use this language. :P

So, Cinnamon Gum is Bubblegum, but it's more of a "real" language than Bubblegum.

The first byte (l) sets the mode to dictionary mode. The rest of the bytes is the following string compressed.

0&   *****
 **     **
*         *
*         *
**       **
  *******;1&   *****
 **  *****
*   *******
*   *******
**   ******
  *******;2&   *****
 *********
***********
***********
***********
  *******;3&   *****
 *****  **
*******   *
*******   *
******   **
  *******;4&   *****
 **     **
*         *
*         *
**       **
  *******

This essentially makes a lookup table with each text assigned to a number. The program then takes input and outputs the respective text.


Can argument%4 or argument&3 save bytes?
Titus

5

JavaScript (ES6), 103 102 bytes

f=
n=>`   *****
 **66733**${s=`
*666777333*`}${s}
**6667333**
  *******`.replace(/\d/g,c=>" *"[c*2>>n&1])
<input type=number min=0 max=4 oninput=o.textContent=f(this.value)><pre id=o>

Edit: Saved 1 byte thanks to @darrylyeo.


1
-2 bytes by storing *666777333*\n in a variable.
darrylyeo

@darrylyeo I must be doing something wrong because I can only seem to save 1 byte...
Neil

My bad, it does indeed save just 1 byte.
darrylyeo


4

VI, 108 bytes

D:let@a=@"%2?@":@"%4?"X":"\\d"<CR>
3i <Esc>5a*<Esc>Yphr*$a*<Esc>O**1110333**<Esc>YPi <Esc>3lx3lx"0px4lyl2p$xYp
:%s/<C-r>a/ /g<CR>
:%s/\d/*/g<CR>

<CR> is the Enter stroke, <C-?> corresponds to Control + ?, and <Esc> to Escape obviously. Each of these count for 1 byte (see meta). The line breaks in the solution is for readability. Only <CR> represents real Enter strokes.

Input

The input file should contain only 1 character, representing n.

Launch

VI should be started like :

vi -u NONE input

Explanations

There are 3 parts in the solution. I will describe the 2nd part first (2nd line), since it is the easiest to explain.

Drawing the sun

The command to draw the sun is :

3i <Esc>5a*<Esc>Yphr*$a*<Esc>O**1110333**<Esc>YPi <Esc>3lx3lx"0px4lyl2p$xYp

The sun must be drawn with , *, 0, 1 and 3, like this :

   *****
 **11033**
*111000333*
*111000333*
**1110333**
  *******

A symmetry would have helped to reduce the bytes size of this part, but it's not that important. I will not explain the full line, but the pattern ***** is used to easily generate the last line, and the pattern **1110333** has been taken as a reference to generate the 3 other lines containing 0, 1 and 3.

It is important to use 0, 1 and 3 for sun parts that can be filled (see next explanations). Drawing this sun takes 55 bytes, and can probably be golfed with some tricks.

Filling the sun according to n

To correctly fill the sun, the instructions to follow are :

  • if n = 0, then 0, 1 and 3 (all digits) should be replaced with
  • if n = 1, then 1 should be replaced with , the other digits with *
  • if n = 2, then 0, 1 and 3 (all digits) should be replaced with *
  • if n = 3, then 3 should be replaced with , the other digits with *
  • if n = 4, then 0, 1 and 3 (all digits) should be replaced with (like n = 0)

From that, we can infer that the substitutions required are :

  • replace some digits by (first substitution)
  • replace all other digits by * (second substitution)

Note that "some digits" can mean "no digits" (n = 2 for example). And "all other digits" can also represent "no digits", if all digits have already been replaced by the first substitution (n = 0 for example).

The second substitution can be easily written in 11 bytes :

:%s/\d/*/g<CR>

The first substitution depends on n, so first we have to calculate what digits are going to be replaced. If replaced characters are stored in register a, the substitution command is written in also 11 bytes :

:%s/<C-r>a/ /g<CR>

<C-r>a is replaced by the content of register a when the command is typed.

To calculate the value of a, following the previous instructions, the algorithm is (in pseudo-code) :

n := read()
if (n % 2 != 0)
then
    a := n
else
    if(n % 4 != 0)
    then
        a := "X"
    else
        a := "\d"

"X" string is used because when n = 2, no digits are replaced by spaces. Any string that is not the sun could be used here, as long as the first substitution does nothing.

This could be written in 31 bytes :

D                                   # yank and delete the first character of the file (n) to register "" (yank by default) : n = @"
 :let@a=                            # define register "a content
        @"%2                        # if (n % 2 != 0)
            ?                       # then
             @"                     #   n
               :                    # else
                @"%4                #   if (n % 4 != 0)
                    ?               #   then
                     "X"            #       "X"
                        :           #   else
                         "\\d"      #       "\\d"
                              <CR>  # calculate "a

Solution

Put all these parts in the right order, and you have the solution :

D:let@a=@"%2?@":@"%4?"X":"\\d"<CR>                                              # calculate the digits to replace with spaces
3i <Esc>5a*<Esc>Yphr*$a*<Esc>O**1110333**<Esc>YPi <Esc>3lx3lx"0px4lyl2p$xYp     # draw the sun with spaces, stars, 0, 1 and 3
:%s/<C-r>a/ /g<CR>                                                              # replace the pattern stored in register "a with spaces
:%s/\d/*/g<CR>                                                                  # replace the remaining digits with stars

3

PHP, 114+1 bytes

+1 byte for -R. Thanks @Neil for the shifting hint.

for(;$c="   *****
 **66733**
*666777333*
*666777333*
**6667333**
  *******"[$i++];)echo+$c?" *"[$c*2>>$argn&1]:$c;

uses underscore for *, 0-indexed. Run as pipe with -nR or try it online.

Requires PHP 5.5 or later:
older PHP does not understand literal string indexing (parse error);
PHP 7.1 complains about non-numeric values (replace +$c with $c>0 to fix).


1
I think " _"[$c*2>>$argn&1] avoids negative shift parameters if you need it.
Neil




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