35
\$\begingroup\$

Introduction

Partly inspired by this StackOverflow question, let's draw an ASCII Lightning Bolt.

Write a program that takes a positive Integer n via STDIN or command line and outputs the ASCII Lightning Bolt below.

Input

Positive Integer n representing the number of lightning zig-zag tiers to draw.

Example Output

n=1

__
\ \
 \ \
  \ \
   \/

n=2

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

n=3

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

n=4

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

. . . etc


Additional notes

  • You may write a function that takes n as the only argument and prints or returns the string.
  • Trailing spaces and new lines are okay.
  • No leading spaces except where appropriate for the designated pattern.
  • Shortest code in bytes wins.
\$\endgroup\$
2
  • 2
    \$\begingroup\$ How many of those challenges have we seen so far? \$\endgroup\$ – flawr Jun 5 '15 at 23:00
  • \$\begingroup\$ @flawr Too many 😯 \$\endgroup\$ – Beta Decay Jun 30 '15 at 16:34

31 Answers 31

1
2
1
\$\begingroup\$

CSASM v2.1.2.3, 144 bytes

func main:
in ""
conv i32
pop $a
push "__\n\\ \\\n \\ \\\n"
push "__\\ \\\n\\  __\\\n \\ \\\n"
dec $a
push $a
mul
add
push "  \\ \\\n   \\/"
add
print
ret
end

Commented:

func main:
    ; Get the input, convert it to an integer then store it in the accumulator
    in ""
    conv i32
    pop $a

    ; Push the first section of the lightning bolt
    push "__\n\\ \\\n \\ \\\n"

    ; Duplicate the middle section of the lightning bolt
    push "__\\ \\\n\\  __\\\n \\ \\\n"
    dec $a
    push $a
    mul

    ; Append the duplicated sections
    add

    ; Append the third section of the lightning bolt
    push "  \\ \\\n   \\/"
    add

    ; Print the result
    print
    ret
end
\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.