Disegna un fulmine ASCII


35

introduzione

In parte ispirato a questa domanda StackOverflow , disegniamo un Fulmine ASCII.

Scrivi un programma che prende un intero positivo ntramite STDIN o riga di comando e genera il fulmine ASCII in basso.

Ingresso

nNumero intero positivo che rappresenta il numero di livelli lampo a zig-zag da disegnare.

Esempio di output

n = 1

__
\ \
 \ \
  \ \
   \/

n = 2

__
\ \
 \ \
__\ \
\  __\
 \ \
  \ \
   \/

n = 3

__
\ \
 \ \
__\ \
\  __\
 \ \
__\ \
\  __\
 \ \
  \ \
   \/

n = 4

__
\ \
 \ \
__\ \
\  __\
 \ \
__\ \
\  __\
 \ \
__\ \
\  __\
 \ \
  \ \
   \/

. . . eccetera


Note aggiuntive

  • È possibile scrivere una funzione che accetta n come unico argomento e stampa o restituisce la stringa.
  • Gli spazi finali e le nuove linee vanno bene.
  • Nessuno spazio iniziale eccetto dove appropriato per il modello designato.
  • Vince il codice più breve in byte.

2
Quante di queste sfide abbiamo visto finora?
flawr

@flawr Too many 😯
Beta Decay

Risposte:


19

Java, 201 196 189 186 182 byte

Ovviamente non è il migliore, ma è in Java.

class I{public static void main(String[]a){System.out.print(("__\na a"+new String(new byte[new Byte(a[0])-1]).replace("\0","__a\\  __\\\n a")+"  a   \\/").replace("a","\\ \\\n"));}}

23
Ah, Java, la lingua in cui hai già perso prima di iniziare a digitare il corpo del tuo mainmetodo.
David Richerby,

2
A volte penso che mainnon dovrebbe essere conteggiato ai fini del conteggio dei byte, e alcune lingue che hanno parole chiave verbose dovrebbero forse essere autorizzate macro. Quindi sarebbe interessante
Alec Teal,

@AlecTeal Totalmente d'accordo, alcune domande in realtà specificano che richiedono solo frammenti funzionali, ma la maggior parte no.
David Mulder,

6
You may write a function that takes n as the only argument and prints or returns the string. So, umm, you should probably do that ;)
Geobits

It may be 6 months late, but codegolf.stackexchange.com/a/64713/42736 or using a function make it shorter.
J Atkin

10

CJam, 41 bytes

":¡ö cQïO[nu÷&,"255b6b"
 _\/X"f='X/~ri(*\

I can probably squeeze a few more bytes out, but here's some compression. I picked a base which would lead to no unprintables.

Try it online.

The lightning bolt is split into top + middle * (input-1) + bottom, where top, middle, bottom (compressed using base conversion) are

__
\. 

  \
.\.\
__\.\
\..__ 

    \
.\.\
..\.\
...\/

(Spaces are marked with .s)

40 bytes

Thanks to Optimizer

"¹Ñ³Û-+ÎDx^áÐ"254b6b"
_\ 0/"f=)/~ri(*\


thanks for the explanation with top + middle * (input-1) + bottom, it reminded me that powershell could multiply strings :P
Nacht - Reinstate Monica

8

JavaScript (ES6) 76

Using template string, the 3 newlines are significant and counted.

Test running the snippet (Firefox only)

f=n=>`__
1 1${`__1\\  __\\
 1`.repeat(n-1)}  1   \\/`.replace(/1/g,`\\ \\
`)

// TEST

go=_=>O.innerHTML=f(I.value)

go()
N: <input id=I value=3><button onclick='go()'>Test</button>
<pre id=O></pre>


7

PowerShell, 72 63 bytes

Stupid Windows and your \r\n... This could have been 67 59 bytes!

%{$a="\ \
";"__
$a"+" $a`__$a\  __\
"*($_-1)+" $a  $a   \/"}

For code golf, I think \r\n still counts as \n, since it does the same thing
MilkyWay90

6

PHP - 84 79 78 bytes

<?php
define('N',3); // <- didnt count these bytes as TS said I could take var N as input
?>
<?="__
\ \
 \ \ ".str_repeat("
__\ \
\  __\
 \ \ ",N-1)."
  \ \ 
   \/"

View the result source or wrap in <pre /> to check results. The newlines are required in the code.
The -1 could be moved to the define, but I considered that a cheat.

1st improvement: replace \n with actual newlines
2nd: Since I can define a var, I used a CONTANT, safes the $. +an unneeded space in str_repeat
3rd: Accidentally removed the -1, but saved a byte by using <?= instead of echo.


6

Pyth, 60 54 bytes (Thanks @isaacg)

My first attempt at Pyth, probably very bad.

"__
\ \ "VtQ" \ \ 
__\ \ 
\  __\ ")" \ \ 
  \ \ 
   \/

Verify it here.


Welcome to Pyth! By the way, Pyth allows literal newlines in its strings, which would be 1 character shorter than \n. Also, the first \\ in the program could be just a \ .
isaacg

@isaacg Thanks.
JNV


4

Brainfuck, 164 bytes

,<++++++++++++++++[>--->+>++++++>++>++++++>+++<<<<<<-]>->--->---->>->-<..<<<.>.>
.<.<.>>.<.>.<.<.<[>>>>..<<.>.<.<.>.>..>..<<.<.>>.<.>.<.<.<-]>>>..<.>.<.<.>>...<.
>>>.

With comments:

Initialise n and character set with i as counter
Memory = in♪\ _/
,<++++++++++++++++[>--->+>++++++>++>++++++>+++<<<<<<-]>->--->---->>->-

Draw top of lightning bolt
<..<<<.>.>.<.<.>>.<.>.<.<.<

Draw lightning bolt zigzags
[>>>>..<<.>.<.<.>.>..>..<<.<.>>.<.>.<.<.<-]

Draw lightning bolt tip
>>>..<.>.<.<.>>...<.>>>.

Okay, how this Brainfuck answer beating Java and C#?


Woah, nice golfing, especially in BF!
MilkyWay90

4

><> (Fish), 409 bytes

Run by fish.py bolt.fish --value n where bolt.fish is the program name and n is your positive integer input.

\
\         "__"       a
\         "\ \"      a
\         " \ \"     a
\r1-:?!vr "__\ \"    a
\     !0  "\  __\"   a
\     !6  " \ \"  a04.
>r   9a.  "__\ \"    \
      /   "\  __\"  \ 
       /  " \ \"   \  
        / "  \ \" \   
         /"   \/"\    
                 aaaaa
|o|!~r           /    
         \        /   
        \          /  
       \            / 
      \              /

It's not short, but it looks cool. My attempt was to try to make it look like a lightning strike. Also, it always errors on completion.


3

Perl, 69+1

69 characters, plus 1 for the -n command line switch to fetch input from stdin.

$s="\\ \\$/";print"__$/$s $s".("__$s\\  __\\$/ $s"x--$_)."  $s   \\/"

Usage example:

perl -ne '$s="\\ \\$/";print"__$/$s $s".("__$s\\  __\\$/ $s"x--$_)."  $s   \\/"' <<<"2"
__
\ \
 \ \
__\ \
\  __\
 \ \
  \ \
   \/

1
Some improvements possible: perl -pe'$_="__\ns s"."__s\\ __\\\n s"x--$_." s \\/";s!s!\\ \\\n!g'. Also \n can be replaced with a literal line break for another 3 character saved.
nutki

3

Javascript (ES6), 86

Not gonna win, but I love 1-line solution and I hate slashes.

f=n=>atob("X18KXCBc"+"CiBcIFwKX19cIFwKXCAgX19c".repeat(n-1)+"CiBcIFwKICBcIFwKICAgXC8")

Cool! What it is doing?
rpax

1
@rpax I used base64 encoding to avoid \\ and \n . Have a look at atob documentation
Michael M.

3

C, 101 bytes

My not so original implementation in c

f(n){puts("__\n\\ \\");for(;--n;puts(" \\ \\\n__\\ \\\n\\  __\\"));puts(" \\ \\\n  \\ \\\n   \\/");}

3

C#, 221 Bytes

class C{static void Main(string[]n){int e=System.Int32.Parse(n[0]);var o=@"__{0}\ \{0} \ \{0}";while(e>1){o+=@"__\ \{0}\  __\{0} \ \{0}";e--;}System.Console.WriteLine(o + @"  \ \{0}   \/{0}",System.Environment.NewLine);}}

This isn't the best, or the smallest answer, but I figured I'd give it a try. Fsacer's answer is much shorter and I think you should check it out. I just decided to do this just as an alternative method really.


1
Hey, here I easily dropped it to 182B. Code: class C{static void Main(string[]n){var e=int.Parse(n[0]);var o=@"__{0}\ \{0} \ \{0}";while(e-->1){o+=@"__\ \{0}\ __\{0} \ \{0}";}System.Console.Write(o+@" \ \{0} \/{0}","\n");}}
fsacer

Nice, and just curious, but why did you change the type of e from int to var? It's still 3 characters :P

No reason, maybe because I love var keyword :)
fsacer

3

C#, 166 bytes

class I{static void Main(string[]a){System.Console.Write(("__\na a"+"".PadLeft(int.Parse(a[0])-1).Replace(" ",@"__a\  __\
 a")+@"  a   \/").Replace("a",@"\ \
"));}}

EDIT 1: improved the result from 186B to 173B
EDIT 2: saved 1B by using PadLeft instead of PadRight
EDIT 3: saved 8B by dropping PadLeft's second parameter and using verbatim string literals


Nice... You can do away with the public at the start, and instead of using a char array, you can use String.PadRight(int, char), which is shorter and will let you use an ascii char which doesn't need escaping.
VisualMelon

Some tweaks: * drop the second parameter to PadLeft(); it's implied (-4) * change 2 of the strings to verbatim (the OP says newlines are fine, and we only need \n, not full \r\n, so -4) End result: class I{static void Main(string[]a){System.Console.Write(("__\na a"+"".PadLeft(int.Parse(a[0])-1).Replace(" ",@"__a\ __\ a")+" a \\/").Replace("a",@"\ \ "));}} [I can't display newlines in comments, but you can edit the original answer!]
sellotape

Nice tips, it was hard to verbatim formatting in order :)
fsacer

3

Awk, 101+8 bytes

101 characters, plus 8 for -v n=$1 to get integer from shell.

'{l="\\ \\";print"__\n"l"\n "l;for(i=1;i<n;++i)print"__"l"\n\\  __\\\n "l}END{print"  "l"\n   \\/"}'

New to this SE site, unclear if those parameters should count.

Ungolfed

awk 
-v n=$1
'{
  l="\\ \\";
  print "__\n"l"\n "l;
  for(i=1; i<n; ++i)
    print "__"l"\n\\  __\\\n "l
}
END
{
  print "  "l"\n   \\/"
}'

Usage example:

lightning() { echo | awk -v n=$1 '{l="\\ \\";print"__\n"l"\n "l;for(i=1;i<n;++i)print"__"l"\n\\  __\\\n "l}END{print"  "l"\n   \\/"}'; }
lightning 3
__
\ \
 \ \
__\ \
\  __\
 \ \
__\ \
\  __\
 \ \
  \ \
   \/


Let's formulate it as “calculated that right”, because definitely not done right. If the variable assignment is put after the code, there is no need for the -v part. Not mentioning that the whole variable idea is unnecessary, just like the separate END block. 89 characters: {l="\\ \\";print"__\n"l"\n "l;for(;--$1;)print"__"l"\n\\ __\\\n "l;print" "l"\n \\/"} pastebin.com/NCznF9kH
manatwork

3

Python 97 82 78char:

print(("__\nl l"+"__l\  __\\\n l"*~-input()+"  l   \/").replace('l','\ \\\n'))

This is my first code golf

@(^_^)@

Test here


This is python 3? Will that input() work for integers?
Renae Lider

2

C, 119 108 bytes

p(t){for(;t;t/=4)putchar(" \\_\n"[t%4]);}main(c){for(p(13434);p(836),--c;p(57154842));p(265488);puts("/");}

First attempt, 150 bytes

v(a){putchar(a);}s(b){b--?v(32),s(b):v(92);}l(b){s(b);s(1);v(10);}main(c){for(puts("__\n\\ \\");l(1),--c;puts("__\\ \\\n\\  __\\"));l(2);s(3);v(47);}

main is accepting an int argument, so run like this: ./lightning . . . to pass 4 as argument.


2

Python 3, 126 118 117 bytes

Just something to get us started with.

n=int(input())
p=print
p('__\n\\ \\')
for i in[0]*~-n:p(r''' \ \
__\ \
\  __\
''',end='')
p(r''' \ \
  \ \
   \/
''')

If you don't need the loop variable, you can iterate trough [0]*(n-1) to get n-1 iterations. You can also write (n-1) as ~-n.
randomra

You can save one byte by changing i in [0] to i in[0].
mbomb007

Welcome! A single line print in the loop is also shorter: for i in[0]*~-n:p(' \\ \\\n__\\ \\\n\\ __\\').
randomra

2

Python 2, 76 bytes

print'__\n\ \\\n \ \\\n'+r'''__\ \
\  __\
 \ \
'''*~-input()+'  \ \\\n   \/'

Just print the first three lines, then print the next three lines n-1 times, and then print the final 2 lines. All in one go.

And here is a nice try at an alternative that (unfortunately) uses exactly the same number of bytes:

print('__\n| |'+'__|\  __\\\n |'*~-input()+'  |   \/').replace('|','\ \\\n')

Have you tried using %s formatting for the middle part?
Sp3000

@Sp3000 I need braces around it to make it work with the *, that gives me 77 bytes - or not inlining it, but that costs 79 bytes. :(
Matty

2

F#, 98 characters, 105 bytes

let l n=(@"__♪◙\z"+String.replicate(n-1)@" \z__\z\  __\♪◙"+ @" \z  \z   \/").Replace("z"," \\\n")

2

CJam 54 Chars

not the shortest, but since i started CJam today, im happy with it.

rd(:T;{'__}:W~N{'\:XSXN}:V~SV{WVXSSWXNSV;N}T*SSVSSSX'/

Try it


2

Pascal: 149 142 141 137 characters

var n:Word;begin
Read(n);Writeln('__'#10'\ \'#10' \ \');for n:=2to n do Writeln('__\ \'#10'\  __\'#10' \ \');Write('  \ \'#10'   \/')end.

After all, Pascal's only golfing strength is that backslashes need no escaping…


1
for n:=2 to n do also works.
randomra

Thank you, @randomra. I think was ~18 years ago when I was aware of this possibility… Nice to remember it.
manatwork

2

Google Sheets, 60 Bytes

Anonymous worksheet function that takes input from range [A1] and outputs to the calling cell.

="__
\ \
 \ \
"&REPT("__\ \
\  __\
 \ \
",A1-1)&"  \ \
   \/


1

SpecBAS - 135 104 bytes

The apostrophe in PRINT statements moves cursor to a new line.

SpecBAS lets you incorporate ASCII characters in a string via way of #n, so have built in some carriage returns (ASCII 13).

Built a string up using carriage returns and other characters, then used REP$ to repeat it the required number of times.

1 LET b$="\ \": INPUT n: PRINT "__"'b$+REP$(#13" "+b$+#13"__"+b$+#13"\  __\",n-1)'" ";b$'"  ";b$'"   \/"

1

PHP 155

$l=PHP_EOL;echo$l;echo "__$l";for($i=0;$i<$argv[1];$i++){if($i>=1)echo "__\\ \\$l\\  __\\$l";else echo "\\ \\$l";echo " \\ \\$l";}echo "  \\ \\$l   \\/$l";

Ungolfed Version

$n = $argv[1];

echo PHP_EOL;
echo '__'.PHP_EOL;
for($i=0;$i<$n;$i++)
{
    if($i>=1) {
        echo '__\\ \\'.PHP_EOL.'\\  __\\'.PHP_EOL;

    }
    else
    {
        echo '\\ \\'.PHP_EOL;
    }   
    echo ' \\ \\'.PHP_EOL; 


}    
echo '  \\ \\'.PHP_EOL;
echo '   \\/';
echo PHP_EOL;

Your code can be further improved, got it down to 121 bytes. echo$l=PHP_EOL,"__$l";$g="\\ \\$l";for($i=0;$i<$argv[1];$i++){echo($i?"__$g\\ __\\$l":$g)," $g";}echo" $g",' \\/',$l;
Octfx

In addition: remove the $i=0; remove the brackets for the for loop; increment the counter inside the loop (echo($i++?"…).
Blackhole

1

Java, 183 180 bytes

class L{public static void main(String[]a){String b="__\n\\ \\\n \\ \\\n";for(int i=1;i<new Long(a[0]);++i)b+="__\\ \\\n\\  __\\\n \\ \\\n";System.out.print(b+"  \\ \\\n   \\/");}}

Lua, 110 bytes

function l(n)print("__\n\\ \\\n \\ \\\n"..string.rep("__\\ \\\n\\  __\\\n \\ \\\n",n-1).."  \\ \\\n   \\/")end

1

Retina, 46 bytes

.+
__#r r$0x  r   \/
1x

1
__r\  __\# r
r
\ \#   

Takes input as unary.

Each line should go to its own file and # should be changed to newline in the files. This is impractical but you can run the code as is, as one file, with the -s flag, keeping the # markers. You can change the #'s to newlines in the output for readability if you wish. E.g.:

> echo -n 11|retina -s lightning|tr # '\n'
__
\ \
 \ \
__\ \
\  __\
 \ \
  \ \
   \/

The algorithm is very simple. The pairs of lines (regex - substitute pairs) do the following substitution steps:

  • Surround input with the top and bottom of the lightning.
  • Subtract 1 from the unary input.
  • Change every unary digit into the middle part of the lightning.
  • Decompress the compressed \ \ parts of the lightning to get the desired output.

Try it online! (includes decimal conversion) but I was able to do it in 45 bytes: Try it online! (includes decimal conversion).
Neil

1

Powershell, 59 bytes

'__
\ \'
,' \ \
__\ \
\  __\'*--$args[0]
' \ \
  \ \
   \/'

Test script:

$f = {

'__
\ \'
,' \ \
__\ \
\  __\'*--$args[0]
' \ \
  \ \
   \/'

}

&$f 1
&$f 2
&$f 3
&$f 4

Output:

__
\ \
 \ \
  \ \
   \/
__
\ \
 \ \
__\ \
\  __\
 \ \
  \ \
   \/
__
\ \
 \ \
__\ \
\  __\
 \ \
__\ \
\  __\
 \ \
  \ \
   \/
__
\ \
 \ \
__\ \
\  __\
 \ \
__\ \
\  __\
 \ \
__\ \
\  __\
 \ \
  \ \
   \/

Explanation:

This script is traditional top+middle+bottom. There is only one smart thing: the comma before the middle string force to repeat an array element rather than a string. Therefore, each middle is displayed on a new line.

Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.