Challenge

Given a size s, print a cube net of that size made of hash symbols (#) and spaces ().

Examples:

1:
  #
# # #    
  #
  #

2:
    # #
    # #
# # # # # #
# # # # # #
    # #
    # #
    # #
    # #

3:
      # # #
      # # #
      # # #
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
      # # #
      # # #
      # # #
      # # #
      # # #
      # # #

The net can actually be any valid cube net that can fold into a cube, for example these:

    # #
    # #
# # # # # # # #
# # # # # # # #
    # #
    # #

    # # # #
    # # # #
    # #
    # #
    # #
    # #
# # # #
# # # #

Rules

  • The resulting net must be geometrically valid (foldable into a cube)
  • Standard loopholes forbidden
  • Read the rules carefully
  • This is , shortest answer wins, but will not be selected
share|improve this question
    
Can there be leading/trailing spaces/newlines? – Kritixi Lithos 18 hours ago
    
@KritixiLithos Yes – wat 18 hours ago
7  
For reference, all 11 cube nets. – xnor 18 hours ago
    
Is it okay if we output it without the spaces? – Kritixi Lithos 18 hours ago
    
@KritixiLithos no – wat 16 hours ago

15 Answers 15

Python 2, 47 bytes

n=input()
for c in 1,4,1:exec"print'# '*c*n;"*n

Try it online!

Prints this net, chosen for being left-justified:

# # 
# # 
# # # # # # # # 
# # # # # # # # 
# # 
# # 

The lines have n or 4*n copies of '# '. For each of 1,4,1, we print n times that many copies, done n times for n lines. Having an exec loop inside a for loop seems wasteful, but I didn't see better.

Alternatives I tested:

lambda n:('# '*n*3+'\n')*n+('  '*n+'# '*n+'\n')*3*n

lambda n:('# '*n*3+'\n')*n+(' '*4*n+'# '*n*3+'\n')*n

def f(n):
 for c in[3]*n+[1]*3*n:print('# '*c*n).center(6*n)

def f(n):
 for c in[4]*n+[0]*n:print' '*c*n+'# '*n*3

def f(n):
 for c in[1]*n+[4]*n+[1]*n:print'# '*c*n

def f(n):
 c=1;exec("print'# '*c*n;"*n+"c^=5;"*n)*3

def f(n):
 for i in range(3*n):print'# '*[1,4,1][i/n]*n

def f(n):
 for c in 1,4,1:print('# '*c*n+'\n')*n,

def f(n):
 for c in 1,4,1:exec"print'# '*c*n;"*n

(The def functions can all be one shorter as a program.)

share|improve this answer

Octave, 58 44 42 32 bytes

@(n)[z=repmat('# ',n);z,z,z,z;z]

partly inspired by @xnor 's python answer.

z=repmat('# ',n);

creates a squre pattern of '# ' for input 2 results the following pattern:

# #             
# # 

y=[z,z,z,z];

four z s are concatenated horizontally:

# # # # # # # # 
# # # # # # # # 

[z;y;z]

z and y and z are concatenated vertically

Try It Online!

# #             
# #             
# # # # # # # # 
# # # # # # # # 
# #             
# #             

Previous answer:

@(n){h(1:n,1:2:n*6)=1;h(1:n*4,n*2+1:2:4*n)=1;' #'(h+1)}{3}

Try It Online!

Generates a T shaped one

# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
      # # #      
      # # #      
      # # #      
      # # #      
      # # #      
      # # #      
      # # #      
      # # #      
      # # #      
share|improve this answer

Mathematica, 77 60 52 bytes

Thanks to Martin Ender for golfing 8 bytes away!

{±s_:=s~Table~#;b=±{a=±"# ","
"},±{a,a,a,a,"
"}}<>b&

Unnamed function taking a positive integer argument # and returning a string with newlines (including a trailing newline); each line has a trailing space as well. First we define ± to be a function that repeats its input # times; then a is defined as ±"# " (this # is a character, not the input!), and from that b is defined to be the set of # short lines, while ±{a,a,a,a}<>n is the set of # long lines. (In both cases, there is a literal linefeed between matching quotes.) The final <>b concatenates the resulting list of strings with second copy of the set of short lines. Example output when #=2 (xnor's answer taught me that this orientation is golfier):

# #     
# #     
# # # # # # # # 
# # # # # # # # 
# #     
# #     

Previous version of this implementation:

""<>(±s_:=s&~Array~#;{b=±{a=±"# ",n="\n"},±{a,a,a,a}<>n,b})&

Original submission:

""<>If[(m=n~Mod~t)==0,"\n",If[n<t#||#<m<=2#,"# ","  "]]~Table~{n,4(t=3#+1)#}&

Builds a string out of 4*(3#+1) pieces, each of which is either "# ", " ", or "\n"; simply calculates which pieces to use based on the index n. Example output when #=2:

# # # # # # 
# # # # # # 
    # #     
    # #     
    # #     
    # #     
    # #     
    # #     
share|improve this answer
    
You can use a literal linefeed instead of \n. However, since this is a space/linefeed-separated output, it may be shortest to construct a nested list of #s and then rely on the default behaviour of StringRiffle, but I'd have to try to be sure. – Martin Ender 16 hours ago
1  
Hm, not 100% sure how to make that work but {±s_:=s~Table~#;b=±{a=±"# ","¶"},±{a,a,a,a,"¶"}}<>b& saves a bunch of bytes (where the pilcrows should be linefeeds). – Martin Ender 14 hours ago
    
Btw, I'm counting 60 bytes for your current solution. – Martin Ender 14 hours ago

Jelly, 20 bytes

x@”#ẋ³Wẋ³K€Y
141DÇ€Y

Try it online!

Is this golfable??? 20 bytes just seems like too much, seeing Link 1.

Explanation:

x@”#ẋ³Wẋ³K€Y Helper link. Arguments: z
             z (implicit).
  ”#         Character #.
x@           Repeat each element of y x times.
     ³       1st command-line argument.
    ẋ        Repeat x y times.
      W      Wrap z.
        ³    1st command-line argument.
       ẋ     Repeat x y times.
         K   Join z on spaces.
          €  Map this link on x.
           Y Join z on newlines.

141DÇ€Y Main link. Arguments: 0
141     Integer 141.
   D    Convert z to base 10.
    Ç   The above link as a monad.
     €  Map this link on x.
      Y Join z on newlines.
share|improve this answer
    
It's V versus Jelly now :) – Kritixi Lithos 17 hours ago
    
@KritixiLithos Nah, your solution was first. – Erik the Outgolfer 17 hours ago
    
V is at 18 bytes now :) – Kritixi Lithos 15 hours ago
    
You can drop a byte by not using @ but swapping the operands to x yourself: ”#xẋ³Wẋ³K€Y. – steenbergh 5 hours ago

JavaScript (ES6), 71

n=>((z='# '[R='repeat'](n))[R](3)+`
`)[R](n)+('  '[R](n)+z+`
`)[R](n*3)

Test

f=
n=>((z='# '[R='repeat'](n))[R](3)+`
`)[R](n)+('  '[R](n)+z+`
`)[R](n*3)

function update() {
  O.textContent=f(I.value)
}

update()
<input id=I type=number value=3 oninput='update()'><pre id=O></pre>

share|improve this answer
    
I'm not entirely sure, but I think you need to output the spaces along with the hashes. (I also made that mistake of not including the spaces in the first edit of my answer) – Kritixi Lithos 17 hours ago
    
@KritixiLithos uh, got it. Thanks – edc65 16 hours ago

Java 8, 99 bytes

l->{for(int i=-1,j;++i<3*l;)for(j=-1,k=(i/l==2)?4*l:l;++j<k;)System.out.print("# "+j>k-2?"\n":"");}
share|improve this answer

Scala, 56 bytes

(n:Int)=>Seq(1,4,1)map("# "*_*n+"\n")map(_*n)mkString ""
share|improve this answer

Ruby, 36 bytes

f=->n{puts (t=[s="# "*n]*n)+[s*4]*n+t}

Usage:

f=->n{puts (t=[s="# "*n]*n)+[s*4]*n+t}
f[3]
# # #
# # #
# # #
# # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # # # # # #
# # #
# # #
# # #

Ruby, 38 bytes

This shape is longer in Ruby but I expect there are some languages where it is shorter.

->n{puts [s="# "*n*3]*n+[" "*n*4+s]*n}

Usage:

g=->n{puts [s="# "*n*3]*n+[" "*n*4+s]*n}
g[3]
# # # # # # # # #
# # # # # # # # #
# # # # # # # # #
            # # # # # # # # #
            # # # # # # # # #
            # # # # # # # # #

Both answers can be shorter if it is permitted to return (preferably) an array of strings or (less preferably) a single string instead of printing.

share|improve this answer

V, 24 23 20 18 20 bytes

Ài# ddÀpLyGïp3PGïp

With all the hidden characters shown

Ài# ^[ddÀp^VLyGïp3PGoïp

^[ is 0x1b (escape character literal) and ^V is 0x16 (C-v)

Try it online!

I had to increase bytecount because the Ä command was being buggy in this new V pull

Outputs in this format:

# 
# # # # 
# 

with a leading newline

Hexdump:

00000000: c069 2320 1b64 64c0 7016 4c79 47ef 7033  .i# .dd.p.LyG.p3
00000010: 5047 ef70                                PG.p

Explanation

Ài# ^[              " Argument times insert "# "
ddÀp                " Argument times duplicate line

Now that one face of the net has been completed, we have to create the net

^VLy                " Copy the face
Gïp                 " Paste it at the end of buffer
3P                  " Paste 3 times (these form the line)
Gïp                 " Paste at end of buffer again

Alternate solution if we don't output the spaces:

21 20 18 16 18 bytes

Àé#ddÀpLyGïp3pGïp

(for the same reason as the top solution, this TIO link is modified)

Try it online!

share|improve this answer

V, 14 bytes (non-competing)

Ài# 5Ù4JjòÀÄk

Try it online!

00000000: c069 2320 1b35 d934 4a6a f2c0 c46b       .i# .5.4Jj...k

For whatever reason, this challenge uncovered numerous bugs. Now that they're all fixed, this version is unfortunately non-competing, but it's nice to see what a V answer to this challenge should look like when it doesn't have to add tons of bytes to keep up with my sloppy coding.

Explanation:

À                   " Arg 1 times:
 i# <esc>           "   Insert the string "# "
         5Ù         " Make 5 copies of this line, and put the cursor on the second copy
           4J       " Join four of these lines together
             j      " Move down to the last line
              ò     " Recursively:
               ÀÄ   "   Make Arg 1 copies of this line
                 k  "   And move up a line
share|improve this answer
    
To be fair, the J issue wasn't sloppy coding AFAIK, I think it was just nvim default? – nmjcman101 3 hours ago
    
Yeah, that's true. But the duplicate operator definitely was sloppy. Thankfully this new version is much simpler. – DJMcMayhem 3 hours ago

JavaScript (ES6), 59 bytes

f=
n=>`141`.replace(/./g,m=>`${`# `.repeat(n*m)}\n`.repeat(n))
<input type=number oninput=o.textContent=f(this.value)><pre id=o>

Output includes a trailing space at the end of each line and a trailing newline.

share|improve this answer

Python 2, 68 71 bytes

Edit Updated to include the spaces which I had stupidly missed out. Thanks to @Mego and @wat for pointing out.

def f(i,c=1):
 j=i*2;print(' '*j*2,'')[c>j/2]+'# '*i*3
 if j>c:f(i,c+1)

Try it online!

In the absence of finding a way to beat @xnor I'll post my recursive function simply as an alternative approach. For f(5) prints

                    # # # # # # # # # # # # # # # 
                    # # # # # # # # # # # # # # # 
                    # # # # # # # # # # # # # # # 
                    # # # # # # # # # # # # # # # 
                    # # # # # # # # # # # # # # # 
# # # # # # # # # # # # # # # 
# # # # # # # # # # # # # # # 
# # # # # # # # # # # # # # # 
# # # # # # # # # # # # # # # 
# # # # # # # # # # # # # # # 

This pattern was chosen simply because it can be split into two parts unlike all of the others.

share|improve this answer
2  
Where are the spaces? – wat 8 hours ago
1  
Without the spaces in the output, this isn't valid. – Mego 8 hours ago
    
My mistake +3 to add the spaces. Updated. – ElPedro 8 hours ago

Batch, 111 bytes

@set s=
@set i=@for /l %%i in (1,1,%1)do @
%i%call set s=%%s%% #
%i%echo%s%
%i%echo%s%%s%%s%%s%
%i%echo%s%
share|improve this answer

Bash / Unix utilities, 72 69 68 66 bytes

b()(yes `dc<<<2o4d$n^$1^3/p`|tr 01 ' #'|head -$n);n=$1;b 1;b 4;b 1

Try it online!

This works by using the fact that [4^k / 3], when written in base 2, is 10101010...01, with k 1's. (The square brackets here denote the floor function.)

share|improve this answer

QBIC, 54 bytes

:[a|H=H+@#`][a|Z=Z+B+H}[a|Z=Z+B[4|Z=Z+H}[a|Z=Z+@┘`+G+H

Prints in this pattern:

#
####
#

Explanation:

:[a|H=H+@#`]    Gets N, defines the literal '#' and creates a string of N '#'s -> named H$

                Top square:        #
[a|Z=Z+B+H}     Add H$ to Z$ for N times, prefixed with a newline (B$, not yet defined)

                Long centerpiece:  ####
[a|Z=Z+B        For N times (the number of lines) add a newline to Z$, and also
  [4|Z=Z+H}     add H$ four consecutive times

                Bottom square:     #
[a|Z=Z+@┘`+G+H  Same as To pSquare, except B$ gets defined here as being a new-line
                --> ┘ signifies a new-line in QBIC
                --> Declaration of B$ (and all string literals) is moved to the 
                top of the script, so it can be used before declaration.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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