15
\$\begingroup\$

Draw a tiled cube of any size, in ASCII art.

You will be given three numbers a, b and c, and the program should output an a*b*c sized cube.

Examples

3 3 3 ->

    ____ ____ ____
  /____/____/____/|
 /____/____/____/||
/____/____/____/|||
|____|____|____|||/
|____|____|____||/
|____|____|____|/

5 2 3 ->

    ____ ____ ____ ____ ____
  /____/____/____/____/____/|
 /____/____/____/____/____/||
/____/____/____/____/____/||/
|____|____|____|____|____||/
|____|____|____|____|____|/

4 6 5 ->

      ____ ____ ____ ____ 
    /____/____/____/____/|
   /____/____/____/____/||
  /____/____/____/____/|||
 /____/____/____/____/||||
/____/____/____/____/|||||
|____|____|____|____||||||
|____|____|____|____|||||/
|____|____|____|____||||/
|____|____|____|____|||/
|____|____|____|____||/
|____|____|____|____|/

Shortest code wins.

\$\endgroup\$
10
  • \$\begingroup\$ I think it would look slightly better if the bottom-right piece was / instead of | so it matches the top, and the number of pipes in each vertical line be identical. \$\endgroup\$
    – mellamokb
    Aug 14, 2012 at 18:13
  • \$\begingroup\$ Also, what are the valid ranges for a, b, and c? For example, are any of the dimensions allowed to be 0? \$\endgroup\$
    – mellamokb
    Aug 14, 2012 at 18:15
  • \$\begingroup\$ @stevether: I didn't think about "tiled cube", that's a better name. \$\endgroup\$
    – beary605
    Aug 14, 2012 at 18:33
  • 4
    \$\begingroup\$ A cube has equal edges. Better to call it a tiled parallelepiped or brick. \$\endgroup\$
    – DavidC
    Aug 15, 2012 at 16:27
  • 5
    \$\begingroup\$ @beary605 I'm not a mathematician, but I think you may be referring to a rectangular cuboid. (en.wikipedia.org/wiki/Cuboid) A cube, a particular type of rectangular cuboid, has square faces. (en.wikipedia.org/wiki/Cube) As far as I know, the name of the shape of the object doesn't change when the object is tiled. \$\endgroup\$
    – DavidC
    Aug 15, 2012 at 18:03

4 Answers 4

12
\$\begingroup\$

Python, 145 chars

a,b,c=map(int,raw_input().split())
for i in range(b+c+1):print(' '*(c-i)+((' /|'[(i>c)+(i>0)]+'_'*4)*(a+1))[:-4]+('|'*(b+c-i))[:b]+'/')[:5*a+c+1]

$ echo "5 6 3" | ./cube.py
    ____ ____ ____ ____ ____
  /____/____/____/____/____/|
 /____/____/____/____/____/||
/____/____/____/____/____/|||
|____|____|____|____|____||||
|____|____|____|____|____||||
|____|____|____|____|____||||
|____|____|____|____|____|||/
|____|____|____|____|____||/
|____|____|____|____|____|/
\$\endgroup\$
2
  • \$\begingroup\$ Could we see a picture of the printout? \$\endgroup\$
    – DavidC
    Aug 15, 2012 at 17:23
  • \$\begingroup\$ @DavidCarraher: done. \$\endgroup\$ Aug 15, 2012 at 18:53
4
\$\begingroup\$

Mathematica 148 143 139 chars

I decided to use transparent glass tiles.

The following code prints ASCII characters "-" in the form of a cuboid with edges, a, b, and c. The only thing you see in the figure below are hyphens.

w_~e~_ := {Arrowheads@Table[{1, p/9, Graphics@Style[Text["-"], Red]}, {p, 9}], White,Arrow@w};
GridGraph[{a, b, c} + 1, VertexSize -> 0, EdgeShapeFunction -> e]

enter image description here

\$\endgroup\$
2
  • 1
    \$\begingroup\$ That's not ascii... cool idea, but no. \$\endgroup\$
    – boothby
    Aug 16, 2012 at 8:42
  • 1
    \$\begingroup\$ It was admittedly cheeky on my part. Thanks for the feedback. \$\endgroup\$
    – DavidC
    Aug 16, 2012 at 11:09
2
\$\begingroup\$

C, 226, 212

s(x,y){x<1?:putchar(" |/_\n"[y],s(x-1,y));}i,j,x,y,z;main(){for(scanf("%d%d%d",&x,&y,&z);j<=y+z;s(i,1),s(j++>y,2),s(1,4)){s(z-j,0);for(i=x;s(1,j?j>z?1:2:0),i;i--)s(4,3);i=y+z-j;if(i>j)i=j;if(i>z)i=z;if(i>y)i=y;}}

Once again a poor score compared to other langs - so any C golf gurus able to improve?

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

Python3 (188)

a,b,c=map(int,input().split())
x=a+1
k="_"*4
p=print
l=5*a+c+1
p(" "*c+k.join(" "*x))
r=" "*c+k.join("/"*x)+"|"*b+"/"
exec("r=r[1:];p(r[:l]);"*c+'b-=1;p((k.join("|"*x)+"|"*b+"/")[:l]);'*b)

It is beaten already, but idea could help someone-else.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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