6
\$\begingroup\$

Your task is to output a spaceship of size n with shooters/guns and n bullets of character - (with spaces in between each bullet) for each gun

Rules

  • If n is odd, you must output the guns every even numbered row

  • If n is even, you must output the guns every odd numbered row

  • All rows with guns must have n+1 # characters

  • The spaceship is 3 characters tall for all inputs

  • n must be greater than 0

Testcases

1
->
#
## -
#

2
->
### - - 
##
### - -

3
->
###
#### - - -
###

Trailing spaces are allowed

This is , so shortest code wins!

\$\endgroup\$

15 Answers 15

4
\$\begingroup\$

J, 40 38 36 35 bytes

' -#'{~3{.2&||.4$$&2,:2,$&2,0 1$~+:

Try it online!

Consider input 2:

  • 0 1$~+: Extends 0 1 to twice the input size:

    0 1 0 1
    
  • $&2, Extend 2 to the input size, and prepend it:

    2 2 0 1 0 1
    
  • 2, Prepend one more two:

    2 2 2 0 1 0 1
    
  • $&2,: Extend 2 to the input size, and prepend it as a new table item, with zero fill:

    2 2 0 0 0 0 0
    2 2 2 0 1 0 1
    
  • 4$ Extend this new list to 4 elements:

    2 2 0 0 0 0 0
    2 2 2 0 1 0 1
    2 2 0 0 0 0 0
    2 2 2 0 1 0 1
    
  • 2&||. Rotate it left 0 times for even numbers, 1 time for odd:

    2 2 0 0 0 0 0
    2 2 2 0 1 0 1
    2 2 0 0 0 0 0
    2 2 2 0 1 0 1
    
  • 3{. Take first 3:

    2 2 0 0 0 0 0
    2 2 2 0 1 0 1
    2 2 0 0 0 0 0
    
  • ' -#'{~ Convert to ascii:

    ##
    ### - -
    ##
    
\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 75 bytes

n=>(r=x=>x-3?"#".repeat(n)+(x%2-n%2?"":"#"+" -".repeat(n))+`
`+r(++x):"")``

Try it online!

\$\endgroup\$
3
  • 1
    \$\begingroup\$ x%2-n%2 => x-n&1 \$\endgroup\$
    – l4m2
    Mar 23 at 0:19
  • 1
    \$\begingroup\$ 70 bytes \$\endgroup\$
    – tsh
    Mar 23 at 1:54
  • \$\begingroup\$ @tsh 69 \$\endgroup\$
    – l4m2
    Mar 23 at 12:16
2
\$\begingroup\$

Charcoal, 21 bytes

NθE³⁺×θ#⎇﹪⁺θι²ω⁺#× -θ

Try it online! Link is to verbose version of code. Explanation:

Nθ                      First input as an integer
  E³                    Map over 3 rows
       #                Literal string `#`
     ×                  Repeated by
      θ                 Input integer
    ⁺                   Concatenated with
           θ            Input integer
          ⁺             Plus
            ι           Current index
         ﹪              Modulo
             ²          Literal integer `2`
        ⎇               If nonzero then
              ω         Empty string else
                #       Literal string `#`
               ⁺        Concatenated with
                   -    Literal string ` -`
                 ×      Repeated by
                    θ   Input integer
                        Implicitly print
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 18 bytes

'#×DĆ„ -I׫IFs}DŠ»

Try it online or verify some more test cases.

Explanation:

'#×        '# Repeat "#" the (implicit) input amount of times as string
   D        # Duplicate this string
    Ć       # Enclose; append its own character to increase its size by 1
     „ -I×  # Repeat " -" the input amount of times as string
          « # Append it to the earlier string
IF }        # Loop the input amount of times:
  s         #  Swap the two values on the stack each iteration
            # (basically swap for odd values; and not for even)
    D       # Duplicate the top string
     Š      # Triple-swap the three values on the stack from a,b,c to c,a,b
      »     # Join the three strings on the stack by newlines
            # (after which it is output implicitly as result)
\$\endgroup\$
1
\$\begingroup\$

Python 3.8 (pre-release), 66 bytes

lambda n:[s:="#"*(n+1-(k:=n%2))+" -"*(1-k)*n,"#"*(n+k)+" -"*k*n,s]

Outputs a list of lines, which is allowed by default I/O method rules.

Could 100% be shorter in some way.

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 45 bytes \$\endgroup\$
    – friddo
    Mar 23 at 10:04
1
\$\begingroup\$

APL+WIN, 42 bytes

Prompts for n

⊃¯3↑(2|n)⌽4⍴(n⍴'#')(((n+1)⍴'#'),(n←⎕)⍴'-')

Try it online! Thanks to Dyalog Classic

\$\endgroup\$
1
\$\begingroup\$

x86-64 machine code, 34 bytes

89 F0 D1 E8 99 B0 23 89 F1 F3 AA 72 0A AA 89 F1 66 B8 20 2D 66 F3 AB F5 B0 0A AA FF C2 7B E6 88 37 C3

Try it online!

Following the standard calling convention for Unix-like systems (from the System V AMD64 ABI), this takes in RDI an address at which to place the result, as a null-terminated byte string, and takes the number n in ESI.

In assembly:

f:  mov eax, esi    # Set EAX to n.
    shr eax, 1      # Shift it right by 1. The low bit goes into CF.
    cdq             # Set EDX to 0 (as the high bit of EAX is 0).
r:  mov al, 0x23    # Set AL to the ASCII code of #.
    mov ecx, esi    # Set RCX to n (automatic zero extension).
    rep stosb       # Write AL to the string RCX times, advancing the pointer.
    jc s            # Jump if CF=1, to skip the guns.
    stosb           # Write AL to the string one more time, advancing the pointer.
    mov ecx, esi    # Set RCX to n (automatic zero extension).
    mov ax, 0x2D20  # Set AL and AH to the ASCII codes of the space and the hyphen.
    rep stosw       # Write those to the string RCX times, advancing the pointer.
s:  cmc             # Invert CF.
    mov al, 0x0A    # Set AL to the ASCII code of the line feed.
    stosb           # Write AL to the string, advancing the pointer.
    inc edx         # Add 1 to EDX, setting the flags except CF.
    jpo r           # Jump to repeat if the sum of the low 8 bits is odd.
                    # This is true for 1 (1₂) and 2 (10₂) but not for 3 (11₂).
    mov [rdi], dh   # Add the second-lowest byte of EDX, which is 0, to the string.
    ret             # Return.
\$\endgroup\$
1
  • \$\begingroup\$ Seems you're not using features of x64 and using x32 is one byte less from inc edx \$\endgroup\$
    – l4m2
    Mar 23 at 14:23
1
\$\begingroup\$

Lua, 69 bytes

n=...for i=1,3 do b=n+i print(("#"):rep(b%2+n)..(' -'):rep(b%2*n))end

Attempt This Online!

enter image description here

Alternate Approaches

-- 73 bytes
a,n="#",...r=a.rep for i=1,3 do b=n+i print(r(a,n+b%2)..r(' -',b%2*n))end
-- 73 bytes as well
a,n="#",...for i=1,3 do b=n+i print(a:rep(n+b%2)..(' -'):rep(n*(b%2)))end
-- 73 bytes also as well
a,n="#",...for i=1,3 do print(a:rep(n+(n+i)%2)..(' -'):rep(n*(n+i%2)))end
-- 71 bytes with rearranging
a,n="#",...for i=1,3 do b=n+i print(a:rep(b%2+n)..(' -'):rep(b%2*n))end
\$\endgroup\$
0
\$\begingroup\$

MathGolf, 19 bytes

)'#*û -k*+_k<kÄ\┼ßn

Try it online.

Explanation:

)          # Increase the (implicit) input-integer by 1
 '#*      '# Pop and push a string of that many "#"
    û -    # Push string " -"
       k*  # Repeat it the input amount of times as string
         + # Append the two strings together
_          # Duplicate the string
 k         # Push the input-integer
  <        # Only leave that many leading characters from the duplicated string
k          # Push the input-integer again
 Ä         # Loop that many times, with a single char as inner code-block:
  \        #  Swap the top two values
           # (basically swap for even values; and not for odd)
   ┼       # Push a copy of the second top item of the stack to the top
    ß      # Wrap all three values onto a list
     n     # Join by newlines
           # (after which the entire stack is output implicitly)
\$\endgroup\$
0
\$\begingroup\$

Ruby, 36 bytes

->n{([?#*-~n+" -"*n,?#*n]*2)[n%2,3]}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

C#, 118 bytes

n=>string.Join('\n',new int[3].Select((_,i)=>new string('#',n)+(n%2==i%2?"#"+new string('x',n).Replace("x"," -"):"")))

-5 bytes thanks to Stone_Red from The Programmers Hangout discord

Curse you C# and your verbose string repeating shakes fist

Try it on .NETFiddle

Explanation

n is the size of the spaceship, i is the 0-indexed current line being generated.

Select is C#'s version of a map function. When the provided predicate takes 2 arguments, the first will be the original value in the collection being enumerated, and the second will be the index of that value in the collection.

In our case we don't care about the original value, we're just using it as a shorter for(int i = 0; i < 3; i++). Likewise we don't care about the original type, because Select will return a collection of whatever type the provided predicate function returns. In this case the predicate function returns a string, so Select will return an IEnumerable<string> regardless of the fact that it's enumerating over an int[]. int was chosen as it's the shortest standard type name in .NET.

Every line of output begins with new string('#',n), which is C#'s way to repeat a char n times. Then the following ternary statement handles generating guns:

(n%2==i%2?"#"+new string('x',n).Replace("x"," -"):"")

The condition here is n%2==i%2, which is true if the parity of the size and line number match. If n's parity matches i's parity, it's a gun line (even values for n will have a gun on line 0 and 2, odd values will have a gun on line 1).

Guns have one extra #, so we start with "#"+.
new string('x',n).Replace("x"," -") generates a string of n x characters, then replaces each x with -, this is the golfiest way I know of to repeat a string n times, as the new string('#',n) method only works for repeating a single char.

Finally the IEnumerable<string> returned from Select is passed to string.Join('\n',...), which simply concatenates the collection into a single string, with newlines between each line.

\$\endgroup\$
0
\$\begingroup\$

Pip, 26 bytes

'#Xa.('#.'-XaJs)X%aBX^UhJn

Attempt This Online!

Explanation

'#Xa.('#.'-XaJs)X%aBX^UhJn
                       h    Builtin variable for 100
                      U     Increment to 101
                     ^      Split into [1; 0; 1]
                   BX       Bitwise XOR each with
                 %a         Input mod 2
     (         )X           Repeat the following string that many (0 or 1) times:
      '#.                     Pound sign concatenated to
         '-Xa                 Hyphen repeated (input) times
             Js               Join on spaces
    .                       Concatenate the following to the beginning:
'#Xa                          Pound sign repeated (input) times
                        Jn  Join the final list on newlines and autoprint
\$\endgroup\$
0
\$\begingroup\$

Retina 0.8.2, 47 bytes

.+
$*##$&$*-$&$*__
$
¶$`_¶$`
#-+(__)+\b|_

-
 -

Try it online! Link includes test cases. Explanation:

.+
$*##$&$*-$&$*__

Assume each row has a gun, so output the right number of #s and -s appropriately. Also append the same number of _s as #s for calculation purposes.

$
¶$`_¶$`

Triplicate the row but append an extra _ to the middle row.

#-+(__)+\b|_

Delete the gun if it's on the wrong row(s).

-
 -

Space out the bullets.

\$\endgroup\$
0
\$\begingroup\$

C (gcc), 98 bytes

r,d,k;f(n){for(r=4;--r;puts("")){for(k=n+r&1,d=n+k;d--;printf("#"));for(;k&&++d<n;printf(" -"));}}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

JavaScript (V8), 68 bytes

n=>[1,2,1].map(i=>'#'.repeat(n+(m=n+i&1))+' -'.repeat(m&&n)).join`
`

Try it online!

\$\endgroup\$

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.