Dessine le «Cool S»


38

introduction

Nous connaissons tous le cool S (également connu sous les noms de Superman S, Stüssy S, Super S, Skater S, Pointy S, Graffiti S, etc.): des milliards d'écoliers du monde entier ont dessiné ce S et se sont immédiatement sentis fiers d'eux-mêmes. Au cas où vous avez oublié ou eu un enfance complètement pas cool , voici une image de ce cool S:

Soit un facteur d'échelle nen entrée (où 1n20 ), émettez le Cool S en ASCII art.

Comment le dessiner

De la page Wikipedia sur le Cool S:

Sortie

Le Cool S quand n= 1 est:

   ^
  / \
 /   \
/     \
|  |  |
|  |  |
\  \  /
 \  \/
 /\  \
/  \  \
|  |  |
|  |  |
\     /
 \   /
  \ /
   v

Et pour différentes valeurs de n, vous augmentez simplement les ntemps de sortie . Par exemple, n= 2:

     ^  
    / \
   /   \
  /     \
 /       \
/         \
|    |    |
|    |    |
|    |    |
|    |    |
\    \    /
 \    \  /
  \    \/
  /\    \
 /  \    \
/    \    \
|    |    |
|    |    |
|    |    |
|    |    |
\         /
 \       /
  \     /
   \   /
    \ /
     v

Notez que les sections verticales sont deux fois plus longues et l’espacement entre les lignes verticales est deux fois plus large.

Et quand n= 3:

       ^
      / \
     /   \
    /     \
   /       \
  /         \
 /           \
/             \
|      |      |
|      |      |
|      |      |
|      |      |
|      |      |
|      |      |
\      \      /
 \      \    /
  \      \  /
   \      \/
   /\      \
  /  \      \
 /    \      \
/      \      \
|      |      |
|      |      |
|      |      |
|      |      |
|      |      |
|      |      |
\             /
 \           /
  \         /
   \       /
    \     /
     \   /
      \ /
       v

Remarque: Bien que non requis, votre code peut également prendre en chargen= 0:

 ^
/ \
\\/
/\\
\ /
 v

Gagnant

Le programme le plus court en octets gagne.



L’enfant des années 90 de la construction ASCII en moi veut suggérer d’utiliser / \ au lieu de ^ pour le conseil.
Ça a l'air

Le seul problème avec @Flater est que / \ utilise deux caractères. La ligne verticale centrale devrait donc être décalée, ce qui la rend très désordonnée.
Beta Decay

@BetaDecay: Cela semble bien sur N = 2 et N = 3 (car il conserve la symétrie ponctuelle), mais je suis d'accord pour N = 1. Il y a aussi l'option de la tête en bas V:Λ
Flater

2
@JacobGarby: Mon argument était stylistique, pas golfy :)
Flater

Réponses:


14

Charbon de bois , 58 53 47 43 41 octets

Nθ≔⊕⊗θδ↗θ/⊗θ↘δ^‖B↓‖M← vMδ⁰⊗θ↗⊕θM⁰δ↗θ/⊗θ⟲T

Essayez-le en ligne!

Je voulais juste essayer une autre approche, cela dessine l’extérieur par des réflexions (merci à Neil d’avoir développé l’idée) et dessine ensuite la partie intérieure. Comme Charcoal a la :Leftdirection par défaut pour tracer des lignes, je l’utilise autant que possible pour sauvegarder des octets en traçant le S horizontalement, comme ceci:

     /----\    /----\     
    /      \  /      \    
   /        \/        \   
  /         /          \  
 /         /            \ 
v     ----/    /----     ^
 \            /         / 
  \          /         /  
   \        /\        /   
    \      /  \      /    
     \----/    \----/     

Et puis je dois juste faire pivoter la toile de 90 degrés dans le sens anti-horaire.


Vous pouvez être sur quelque chose là-bas ... 22 octets vous donne tout de l'extérieur ...
Neil

@Neil ce n'était pas exactement comme ça, votre idée nécessitait une solution mineure, mais en réalité, cela a été une grande amélioration!
Charlie

Oui, j'ai commis une erreur similaire sur mon message d'origine car je ne vérifiais pas correctement l'effet de redimensionnement.
Neil

Quelqu'un a dit Rotate? Cela me donne une idée ...
Neil

@ Neil hey, tu as tout à fait une amélioration! :-)
Charlie

13

Python 3 , 255 249 248 209 octets

-6 bytes thanks to Kevin Cruijssen

-1 byte thanks to Kevin Cruijssen

-39 bytes thanks to Rod and Jo King

n=int(input())
m=2*n
a,b,q,c,l='\ \n/|'
f=m*b
s=q+q.join([f[d:]+c+b*2*d+b+a+f[d:]for d in range(m+1)]+[l+f+l+f+l]*m+[d*b+a+f+a+f[d*2:]+c+d*b for d in range(n)]+[n*b+a+f+a+c+n*b])
print(f,'^'+s+q+s[::-1]+f,'v')

Try it online!

It now handles n=0.


Both o+~d can be m-d and range(o) can be range(m+1), and then you can remove o=m+1\n to save 6 bytes. Nice answer though, +1 from me.
Kevin Cruijssen

1
Oh, and one more byte by changing p(s)\np(s[::-1]) to p(s+q+s[::-1]): 248 bytes
Kevin Cruijssen

You can save 6 bytes if you use a single print, and more 4 by removing [] from join([...]), totalizing 238 bytes
Rod

You can also store q.join in a variable to save a byte
Rod

217. Joined all the q.joins, and a couple of other things
Jo King

13

Charcoal, 47 42 41 bytes

Fv^«↓⊗θ↘⊗⊕θ←↓⊗θ↙⊕⊗θ↖ι↖⊕⊗θ→↑⊗θ↗⊕θMθ⁺⊗θ⊕θ⟲⁴

Try it online! Link is to verbose version of code. Explanation: Draws the following lines in order:

   ^
  / \
 /   \
/     \
|  1  |
|  1  |
\  2  /
 \  2/
 8\  2
8  \  2
7  |  3
7  9  3
6     4
 6   4
  6 4
   5

Where 5 is the current character of the string v^. At the end of the first loop the cursor is then positioned at point 9. The entire canvas is then rotated so that the other half of the Cool S can be drawn. (The canvas actually gets rotated twice, but this is just an implementation detail.)

Charcoal doesn't support RotateCopy(:Up, 4) but if it did then this would work for 33 bytes:

↖^↖⊕⊗θ→↑⊗θ↗⊕θ‖BM↓↙⊗θ→↓⊗θ⟲C↑⁴J⁰¦⁰v

@BetaDecay Sorry about that. I also had the wrong byte count anyway...
Neil

Nice, it gets n=0 right too
Beta Decay

6

Canvas, 36 32 29 bytes

«|*‼l├/L1^╋;╶╵\∔∔│α╶«├:╵╋:↔↕∔

Try it here!

A whole lot of stack manipulation. (outdated) explanation:

«|*                                an array of input*2 "|"s
   ‼                               cast to a 2D object (needed because bug)
    :                              duplicate that (saved for the center line)
     l├                            height+2
       /                           create a diagonal that long
        L1^╋                       and in it, at (width; 1) insert "^"
            ;∔                     append the vertical bars
                               ^
                              /
          so far done:       / 
                            /  
                            |  
                            |  
              ⁸╵                   input+1
                \                  antidiagonal with that size
                 ∔                 appended to the above
                  │                mirror horizontally
                              ^
                             / \
                            /   \
                           /     \
                current:   |     |
                           |     |
                           \     /
                            \   /                                                       |
                   α               get the 2nd to last popped thing - the antidiagonal  |
                    └∔             append it to the vertical line copied way before:    \
                      ⁸«├          input/2 + 2                                            \
                         :╵        duplicate + 1
                           ╋       at (input/2 + 2; input/2 + 3) in the big part insert  ^
                            :↔↕∔   mirror a copy vertically & horizontally and append that to the original

3

Python 2, 227 208 207 202 196 181 bytes

I=n=2*input()
R,L,S,P='/\ |'
k=n*[2*(P+S*n)+P]
exec"k=[R+S+2*S*I+L]+k+-~I%2*[L+S*n+L+S*I+R];I-=1;"*-~n
print'\n'.join(t.center(2*n+3)for t in['^']+k+[a[::-1]for a in k[::-1]]+['v'])

Try it online!

Thks to Jo King for 1 byte; and then another 5 bytes total (via n => 2*n).

Works for n=0 as well.


3

C (gcc), 379 353 344 334 bytes

I used a couple of #defines for subexpression elimination and several globals to communicate between the internal functions. The main loop goes {0,1,2,3,3,2,1,0} to construct the S.

Thanks to Jonathan Frech for the suggestions.

#define z(a,b...)printf("%*c%*c%*c\n"+a,b);}
#define y(a){for(i=~-a*t;v*i<v*a*!t+t;i+=v)
i,n,p,r,t,u,v;a(){z(6,r+2,94+t*24)b()y(-~r)z(3,-i-~r,47+u,i*2+2,92-u)c()y(r)z(0,~r,124,~r,124,~r,124)d()y(-~n)z(0,i+1,92-u,2*(n-t*i)+1,92,2*(n-!t*i)+1,47+u)(*x[])()={a,b,c,d};f(s){r=2*s;for(p=0;p<8;x[7*t-p++*(2*t-1)](n=s))t=p>3,v=2*!t-1,u=t*45;}

Try it online!


w -r-1 could possibly be golfed to w~r.
Jonathan Frech

Though then inlining is one byte shorter.
Jonathan Frech



3

C (gcc), 260 254 bytes

-6 bytes thanks to ceilingcat.

f(n){int s=2*n++,t=s+1,I[]={1,t,s,n,n,s,t,1},A[]={s,1,1,1,2*t,1,t,t,1,t,1,n,t,t,1,t,t,1,1,1,t,s,1,1},x;for(s=8;s--;)for(n=0;n<I[s];n++,puts(""))for(t=3;t--;)x=s*3+t,printf("%*c",n*("AAAA?BAAAAC@?ABAAACA@AAA"[x]-65)+A[x],"w!!!0]}}}]]00]]}}}]!0_!!"[x]-1);}

Try it online!

Rundown

We can divide the shape into parts:

 ^           Top cap
/ \          Top slope
|||          Sides
\\/          Twist, part 1
/\\          Twist, part 2
|||          Sides
\ /          Bottom slope
 v           Bottom cap

Each part could be described by a number of lines, three chars, and three relationships to certain values that decides the field-width at each line.

A first iteration came to be:

#define g(x,s,A,B,C)for(i=0;i<x;i++)printf("%*c%*c%*c\n",A,*s,B,s[1],C,s[2]);
f(n)
{
    int s=2*n++,t=s+1,i;

    g(1,  "  ^",  1,      1,  t-1)
    g(t, "/ \\",t-i,      1,2*i+1)
    g(s,  "|||",  1,      t,    t)
    g(n,"\\\\/",i+1,      t,t-2*i)
    g(n,"/\\\\",n-i,  2*i+1,    t)
    g(s,  "|||",  1,      t,    t)
    g(t, "\\/ ",i+1,2*t-2*i,    1)
    g(1,  "  v",  1,      1,  t-1)
}

The calls to the g() macro looks very much like a table could be constructed and looped over. Field-widths are sometimes related to the index counter, and sometimes not. We can generalise the field-width to be F * i + A, where F is some factor to multiply i with, and A is some value to add to the width. So the last width of the fourth call above would be -2 * i + t, for example.

Thus we get:

f(n){int s=2*n++,t=s+1,         s = size of "side" parts, t = size of top and bottom slopes
I[]={1,t,s,n,n,s,t,1},          The number of lines per part.
A[]={...},x;                    A[] holds the values to add to each field-width.
for(s=8;s--;)                   Loop through the parts.
for(n=0;n<I[s];n++,puts(""))    I[s] decides how many lines to the part. Ends with newline.
for(t=3;t--;)                   Go through the three chars of each line.
x=s*3+t,                        Calculate offset.
printf("%*c",                   Print the char.
n*("..."[x]-65)+A[x],           Build field-width. The string holds the index factor, A[]
                                holds the offset part.
"..."[x]-1);}                   The char itself is grabbed from the string.
                                Shifted by 1 to eliminated double backspaces.

In the end it was not much shorter than a tightened version of the g() calling one, but shorter is shorter.


@ceilingcat Cheers.
gastropner

@ceilingcat The undefined evaluation order of function arguments give me pause.
gastropner

2

Java, 435 bytes

The function itself takes 435 bytes. There is certainly room for improvement, "high level" by analyzing the rules about where to place which character (in the end the S is point-symmetric), and "low-level", by classical golfing (maybe pulling out another variable or combining two of the for-loops). But it's a first shot with this rather ungolfy language:

import static java.util.Arrays.*;
import static java.lang.System.*;

public class CoolS
{
    public static void main(String[] args)
    {
        print(1);
        print(2);
        print(3);
    }
    static void print(int n){int i,r,d=3+6*n,w=3+n*4,h=6+n*10,m=n+n,v=w/2,k=h-1,j=w-1;char t[],S='/',B='\\',P='|',s[][]=new char[h][w];for(char x[]:s)fill(x,' ');s[0][v]='^';s[k][v]='v';for(i=0;i<1+m;i++){r=i+1;t=s[r];t[v-r]=S;t[v+r]=B;t=s[k-r];t[v-r]=B;t[v+r]=S;}for(i=0;i<m;i++){r=2+m+i;t=s[r];t[0]=t[v]=t[j]=P;t=s[k-r];t[0]=t[v]=t[j]=P;}for(i=0;i<1+n;i++){r=2+m+m+i;t=s[r];t[i]=t[i+1+m]=B;t[j-i]=S;t=s[d-i];t[i]=S;t[v-i]=t[j-i]=B;}for(char x[]:s)out.println(x);}
}

Hi there. Imports are part of the byte-count I'm afraid, so your current answer is actually 478 bytes. You can however golf it down to (coincidentally enough) your current 435 bytes with some basic things to golf.
Kevin Cruijssen

Been able to golf a bit more to 405 bytes by removing some variables and using t=... a bit less where it would save bytes. If you have any questions about any of the changes I made, let me know. :)
Kevin Cruijssen

Thanks @KevinCruijssen , unfortunately I currently cannot invest more time here - this was just a recreational thing, and considering the "verbosity" of Java, not a serious competitor anyhow ;-) Consider adding your solution as an own answer, though - then we at least have some intra-language competition :-)
Marco13

2

PHP, 378 374 378 377 376 335 331 328 bytes

-3 bytes, thanks to manatwork

-4 bytes, used str_pad instead of str_repeat

-41 bytes, thanks to manatworks' suggestions

-1 byte, merged two increments into a +=2

-1 byte, removed superfluous \

-4 bytes by echoing once. Forgot I needed to pass the string into the function so this is more bytes

Works for n = 0 as well.

function s($b){return str_pad($w,$b);}echo s($i=1+$a=2*$argv[1]).'^
';for(;$i;$j++,$y=$z.$y)echo$z=s(--$i).'/'.s(++$j).'\
';for(;$k<$a;$k++)$x.='|'.s($a).'|'.s($a).'|
';echo$x;for(;$l<=$a/2;)echo s($m++).$c='\\',s($a).$c.s($a-$l++*2).'/
';for(;$m;$n+=2)echo s(--$m).'/'.s($n).$c.s($a).'\
';echo$x.strtr($y,'/\\','\/').s($a+1).v;

Try it online!


1
As function declaration is quite expensive and you use t() only twice, would be shorter without it. If beside the 9 notices you take 1 warning too, you can remove the quotes around 'v' in the final echo.
manatwork

1
You could use single loop for the top and bottom oblique parts. The initialization of $a and $i could be compacted by moving them at their first usage.
manatwork

1
Oh, and $i>0 and $m>0 can be written simply as $i and $m.
manatwork

1
With trailing spaces, as in some other solutions.
manatwork

1
You can also move the declaration of $c to its first usage. Just change the . concatenation after it to ,. Try it online!
manatwork

1

Python 3, 321 307 bytes

Thanks to @EsolangingFruit for saving 14 bytes

n=int(input())
b,f='\/'
c,l=4*n+3,10*n+6
r=range
h=c//2
L=[c*[' ']for _ in r(l)]
L[0][h],L[-1][h]='^v'
for i in r(h):a=L[h-i];a[i],a[c+~i]=f,b
for i in r(2*n):L[h-~i][0::h]='|'*3
for i in r(n+1):a=L[h+h+i];a[c+~i],a[i:c-1:h]=f,b*2
for i in r(1,l//2):L[l+~i]=L[i][::-1]
print('\n'.join(''.join(i)for i in L))

Try it online!

Python 2, 303 bytes

n=int(input())
b,f='\/'
c,l,r=4*n+3,10*n+6,range
h=c/2
L=[c*[' ']for _ in r(l)]
L[0][h],L[-1][h]='^v'
for i in r(h):a=L[h-i];a[i],a[c+~i]=f,b
for i in r(2*n):L[h-~i][0::h]='|'*3
for i in r(n+1):a=L[h+h+i];a[c+~i],a[i:c-1:h]=f,b*2
for i in r(1,l/2):L[l+~1]=L[i][::-1]
print'\n'.join(''.join(i)for i in L)

Try it online!


You can replace '\\','/' on the second line with *'\/' to save three bytes.
Esolanging Fruit


Thanks! @EsolangingFruit! I was not aware of bit operations in Python. Also, it would save a few bytes to use Python2 because of the division and parentheses in print
Pétur

In Python 2, input() automatically eval()s the string, so you can skip the int() call as well.
Esolanging Fruit

For Python 3, you can change the last line to for l in L:print(*l,sep="") (I don't think there is an equivalent in Python 2).
Esolanging Fruit
En utilisant notre site, vous reconnaissez avoir lu et compris notre politique liée aux cookies et notre politique de confidentialité.
Licensed under cc by-sa 3.0 with attribution required.