# Print a cube net of the specified size

## 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
• This is , shortest answer wins, but will not be selected
• Can there be leading/trailing spaces/newlines? Commented Jan 14, 2017 at 7:51
• @KritixiLithos Yes Commented Jan 14, 2017 at 7:54
• For reference, all 11 cube nets.
– xnor
Commented Jan 14, 2017 at 8:04
• What if I don't read the rules carefully? Commented Jan 18, 2017 at 12:44
• @steenbergh Then your solution is invalid Commented Nov 14, 2017 at 23:05

## 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.)

# Octave, 584442 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!

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


@(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

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


# Mathematica, 7760 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:

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


## JavaScript (ES6), 59 bytes

f=
",$a,"# ");  Prints a net like this: # # # # # # # # # # # # # # # # # # # # # # # #  (with a leading newline) • while($a="282"[$i++/$s=$argv[1]]*$s)echo str_pad("\n",$a,"# "); saves 2 bytes. Commented Feb 2, 2017 at 10:30 ## 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%  # Pushy, 3229 28 bytes Prints the cube net left-justified. This was hard to golf... &V:# ;{:";N^:4:";T'.;TD^:"  Try it online! # Retina, 39 37 bytes This is my first time using Retina, I'm still trying to understand how to do things. .+$*#
#
#
\#
$_¶ G. )*%^$_$_$_



(with two trailing spaces after the 4th and 5th lines)

Thanks to Martin Ender for golfing 2 bytes!

Try it online!

• @MartinEnder Thank you, I had not noticed that requirement, now it should be correct. Do you have any suggestion to how I should try to golf this?
– Leo
Commented Jan 16, 2017 at 10:08
• Not having a lot of brilliant ideas but tio.run/nexus/… saves two bytes. You can avoid the trailing linefeeds by wrapping everything in a group which has its output flag (and since the group is the last thing the program, the output flag defaults to being non-silent). The other byte avoids the fourth $_ at the bottom by switching some things around after removing the empty line. tio.run/nexus/… is the same byte count but a bit uglier. Commented Jan 16, 2017 at 10:17 • @MartinEnder Thank you for the tips, and thank you for this language too, it's really nice! – Leo Commented Jan 16, 2017 at 13:04 • Thank you for the kind words. :) There's a chatroom for it if you have any questions or want to discuss things. It's currently rather quiet, but I try to keep it unfrozen in case people have questions (and you should be able to ping me in there any time). Commented Jan 16, 2017 at 13:25 ## QBIC, 5267 40 bytes Complete re-write: :[a*3|G=G+@#][a*2|G=G+@ ][a|?G][a|?_fG  This now uses this pattern: ###-- --###  Where the -are filled with spaces. : Read 'a' from the command line > Assume 3 [a*3| Create 3 segments times 'a' filling > 3*3 = 9 G=G+@#] Define A$ to be '#' and add this to G > G = "#########"
[a*2|    Create 2 segments times 'a' spacer
G=G+@ ] Define B$to be ' ' and add this to G > G = "######### " [a| FOR d == 1; d <= a; d++ ?G] Display G$:
"#########       "
"#########       "
"#########       "
[a|      FOR e == 1; e <= a; e++
?_fG     Display G$in reverse (_f...|): " #########" " #########" " #########" (For loop and function call to Flip impicitly closed by QBIC)  • This answer is invalid, per this comment by OP. Commented Jan 16, 2017 at 7:47 • @EriktheOutgolfer Updated. Commented Jan 16, 2017 at 8:48 • Appropriate language name for the challenge! Commented Jan 25, 2017 at 21:40 # Pip, 2817 16 bytes 15 bytes of code, +1 for -n flag. "# "Xa*_RLaM141  Takes the size as a command-line argument. Try it online! ### Explanation  a is 1st cmdline arg _ Build a lambda function a* (a) times argument "# "X Repeat string "# " that many times RLa Wrap in a list and repeat list (a) times M141 Map that function to the characters of 141 Autoprint (-n flag separating list items with newlines)  The following isn't exactly how the data gets modified, but it gives the basic idea (for a=2): 141 [1;4;1] [2;8;2] ["# # ";"# # # # # # # # ";"# # "] [["# # ";"# # "];["# # # # # # # # ";"# # # # # # # # "];["# # ";"# # "]] # # # # # # # # # # # # # # # # # # # # # # # #  # 05AB1E, 13 bytes D141S×S*„# ×»  Try it online! Explanation Example input n=2 D # duplicate input # STACK: 2, 2 141S # push the list [1,4,1] # STACK: 2, 2, [1,4,1] × # repeat each item in the list input_no times # STACK: 2, [11, 44, 11] S # split into list of digits # STACK: 2, [1, 1, 4, 4, 1, 1] * # multiply each digit with input # STACK: [2, 2, 8, 8, 2, 2] „# × # repeat the string "# " for each item in the list # STACK: ['# # ','# # ','# # # # # # # # ','# # # # # # # # ','# # ','# # '] » # join by newlines # OUTPUT: # # # # # # # # # # # # # # # # # # # # # # # #  ## C#, 152 bytes n=>{var m="";foreach(int c in new[]{1,4,1})for(int i=0,j;i++<n;){for(j=0;j++<c;)m+=new System.Text.StringBuilder().Insert(0,"# ",n);m+="\n";}return m;};  # SmileBASIC, 57 50 bytes INPUT S E$=("#"*S*3+CHR$(10))*S?E$SCROLL-S*2,-1?E$ Explained: INPUT SIZE PART$=("#"*SIZE*3+CHR$(10))*S 'generate half of the pattern PRINT PART$ 'print half the pattern
SCROLL SIZE*-2,-1 'scroll the text 2 squares right (and 1 character down)
PRINT PART\$ 'print half the pattern again


After the first PRINT (size=2, @ is the cursor position):

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

@


After the SCROLL:

    ######
######
@


After the second PRINT:

    ######
######
######
######
@

• In this case you can skip the spaces Commented Feb 2, 2017 at 20:17

# Common Lisp, 8381 79 bytes

(lambda(x)(format t"~v@{~v@{# ~}~:*~%~}~v@{~vt~0@*~v{# ~}~%~}"x(* x 3)(* x 2)))


Usage:

(funcall #'(lambda(x)(format t"~v@{~v@{# ~}~:*~%~}~v@{~vt~0@*~v{# ~}~%~}"x(* x 3)(* x 2)))2)

Output:

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


### How does it work?

in format there is control string (inside "") and arguments after it
certain commands in string can use arguments (for example ~a displays argument)
~v{~} - takes argument and does commends inside {~} number of times
(number is given by argument)
we can write text in loop body - it'll get displayed. Even if
in loop body there are none commends to use arguments loop itself
will consume one argument (it doesn't matter what it'll be -
it doesn't seem to affect output)
~% - newline
~n* - jump n arguments forward (relative)
~n:* - jump n arguments backwards (relative)

~v@{~v{# ~}~%~1@*~} <--- we make x rowns, 3x columns of string "# ".
~1@* is used during looping, it also means we end on argument 1.
(second argument: (* x 3))
~v@{~vt~0@*~v{# ~}~%~} <--- we now make 3x rows, x columns of "(spaces)# "
~vt - is used to move to accurate column.
Here it takes argument (* x 2) - this is
minimal number of columns to display. It uses
space as default character for missing characters
(when string inside <~> is shorter than min
number of columns). I use it to make (* x 2) spaces,
by giving it no text.
~0@* <--- after making spaces we have no new arguments, we go back to
argument number zero - it is used to determine number of columns in our block
`

Ideas for improvement are welcomed.