90
\$\begingroup\$

Challenge

Write a program that takes an 11x11 array of integers, and constructs a 3D ASCII block building, where each value in the array represents the height of a column of blocks at the coordinates matching the array position. A negative height is a "floating" column - only the top block is visible.

Example

                                                        __________________
                                        ___            /\__\__\__\__\__\__\
 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,      /\__\          /\/\__\__\__\__\__\__\
 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,     /\/__/         /\/\/__/__/__/__/__/__/
 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,    /\/\__\        /\/\/\__\      /\/\/__/
 1, 0, 0, 7,-7,-7,-7,-7, 7, 0, 0,    \/\/\__\      /\/\/\/__/     /\/\/__/
 0, 0, 0, 7,-7,-7,-7,-7, 7, 0, 0,     \/\/__/     /\/\/\/\__\    /\/\/__/
 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0,      \/\__\    /\/\/\/\/__/   /\/\/__/
 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0,       \/__/    \/\/\/\/\__\_  \/\/__/
 1, 0, 0, 4, 3, 2, 1, 0, 0, 0, 1,                 \/\/\/\/__/_\_ \/__/
 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,            ___   \/\/\/__/__/_\_         ___
 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,           /\__\   \/\/__/__/__/_\       /\__\
 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1,           \/\__\   \/__/__/__/__/       \/\__\
                                             \/\__\_________         ______\/\__\
                                              \/\__\__\__\__\       /\__\__\__\__\
                                               \/__/__/__/__/       \/__/__/__/__/

Input

The input will be a list of 121 integers, either read from stdin (choice of separator is up to you), or passed in as an array (can be 1D or 2D).

Heights will be in the range -11 to 11.

Output

The generated building can be written to stdout, displayed directly on the screen, or returned as a newline-separated string.

Leading and trailing whitespace is allowed.

Building Rules

The shape of an individual 3D block looks like this:

 ___
/\__\
\/__/

And a 2x2x2 cube of blocks looks like this:

  ______
 /\__\__\
/\/\__\__\
\/\/__/__/
 \/__/__/

When blocks overlap, a higher block takes precedence over a lower one, blocks in front take precedence over those further back, and blocks to the left takes precedence over those to the right. The only special case is that the top line of a block should never overwrite any non-space character behind it.

The interpretation of column heights can best be explained by looking at a 2D representation from the side.

HEIGHT:  1    2    3   -3   -2   -1
                  __   __
             __  |__| |__|  __
        __  |__| |__|      |__|  __
       |__| |__| |__|           |__|

Test Cases

If you want to try out your solution on a few more inputs, I've put together a couple of test cases here.

Winning

This is , so the shortest submission (in bytes) wins.

\$\endgroup\$
8
  • 9
    \$\begingroup\$ Ohh boy, get ready for 300+ byte solutions. Good challenge. +1 \$\endgroup\$ Jan 12, 2018 at 20:11
  • 7
    \$\begingroup\$ @totallyhuman Nah, Dennis is going to have a 9-byte solution for this in 20 minutes. \$\endgroup\$
    – Deacon
    Jan 12, 2018 at 20:50
  • 3
    \$\begingroup\$ Does the perspective have to be as shown with the bottom left of the input data in the foreground? The fact that this is not the first or last element of data makes it harder. Is it acceptable to either 1. keep the mapping as is and draw output with the bottom right column in the foreground or 2. draw a mirror image or 90 deg rotation of the data? Either of these would make the last data element correspond with the column in the foreground, which would be easier. \$\endgroup\$ Jan 12, 2018 at 21:41
  • 3
    \$\begingroup\$ I feel inclined to use a real game engine (or part of it) to render a photo and convert it to ASCII \$\endgroup\$
    – Stan Strum
    Jan 12, 2018 at 22:16
  • \$\begingroup\$ @LevelRiverSt That seems like a reasonable request - you can choose the order of the 121 input elements to be whatever makes the most sense for your solution, as long as your ordering is consistent. It must be possible to produce every kind of layout that can be produced with the default order. \$\endgroup\$ Jan 12, 2018 at 22:24

6 Answers 6

34
\$\begingroup\$

C,  376   350   313   309  285 bytes

Thanks to @Jonathan Frech for saving four bytes!

#define F for(
char*t,G[26][67],*s;i,j,e,k,v,x,y;b(){F s="\\/__//\\__\\ ___ ";*s;--y,s+=5)F e=5;e--;*t=*s<33&*t>32?*t:s[e])t=G[y]+x+e;}f(int*M){F;e<1716;++e)G[e/66][e%66]=32;F k=0;++k<12;)F i=0;i<11;++i)F j=11;j--;v+k||b())x=i+j*3+k,y=14+i-k,(v=M[i*11+j])>=k&&b();F;++e<26;)puts(G+e);}

Try it online!

Unrolled:

#define F for(

char *t, G[26][67], *s;
i, j, e, k, v, x, y;

b()
{
    F s="\\/__//\\__\\ ___ "; *s; --y, s+=5)
        F e=5; e--; *t=*s<33&*t>32?*t:s[e])
            t = G[y]+x+e;
}

f(int*M)
{
    F; e<1716; ++e)
        G[e/66][e%66] = 32;

    F k=0; ++k<12;)
        F i=0; i<11; ++i)
            F j=11; j--; v+k||b())
                x = i+j*3+k,
                y = 14+i-k,
                (v=M[i*11+j])>=k && b();

    F; ++e<26;)
        puts(G+e);
}
\$\endgroup\$
11
  • \$\begingroup\$ Can 26*66 not be 1716? \$\endgroup\$ Jan 12, 2018 at 23:44
  • \$\begingroup\$ @JonathanFrech Sure, I forgot about that. \$\endgroup\$
    – Steadybox
    Jan 12, 2018 at 23:44
  • \$\begingroup\$ *s==32 -> *s<33. \$\endgroup\$ Jan 12, 2018 at 23:47
  • \$\begingroup\$ for(e=k=1;e;++k)for(e= -> for(k=1;e;++k)for(e=. \$\endgroup\$ Jan 12, 2018 at 23:48
  • 1
    \$\begingroup\$ 262 bytes, I couldn't fit all test cases on the link due to the character limit, just copy them from the answer \$\endgroup\$
    – c--
    May 29, 2023 at 18:32
25
\$\begingroup\$

Charcoal, 70 69 68 bytes

≔E¹¹⮌I⪪S,θF²F¹¹F¹¹F¹¹«J⁻⁻⁺λκ×μ³ι⁻λκ≔§§θλμη¿∨⁼±η⊕κ‹κη¿ι“↗⊟&⁹κUhnI”___

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

≔E¹¹⮌I⪪S,θ

Read the array, split each line on commas and cast to integer, but also reverse each line, since we want to draw right-to-left so that left columns overwrite right columns. (Other dimensions already have desired overwriting behaviour.)

F²F¹¹F¹¹F¹¹«

Loop through i) top lines and bodies k) height l) rows m) columns. (Looping through first top lines and then bodies avoids overwriting bodies with top lines.)

J⁻⁻⁺λκ×μ³ι⁻λκ

Jump to the position of the cube.

≔§§θλμη

Fetch the height at the current row and column.

¿∨⁼±η⊕κ‹κη

Test whether a cube should be drawn at this height for this row and column.

¿ι“↗⊟&⁹κUhnI”___

Draw the body or top of the cube.

\$\endgroup\$
3
  • \$\begingroup\$ When I change the first 3 to a 33, I only get 11 blocks in the tower. In general towers seem to be capped at 11. How does that happen? \$\endgroup\$ Jan 15, 2018 at 13:09
  • \$\begingroup\$ @Fabian I'm slightly confused that F¹¹F¹¹F¹¹ wasn't a clue... \$\endgroup\$
    – Neil
    Jan 15, 2018 at 16:06
  • \$\begingroup\$ I don't know this programming language, I just played around a bit with the TIO link. \$\endgroup\$ Jan 15, 2018 at 20:30
9
\$\begingroup\$

JavaScript (ES6), 277 251 bytes

a=>(n=55,$=f=>[...Array(n)].map((_,i)=>f(i)),S=$(_=>$(_=>' ')),n=11,$(l=>$(z=>$(y=>$(x=>(x=10-x,X=x*3+y+z,Y=y-z+n,Z=a[y][x])<=z&&Z+z+1?0:l?['/\\__\\','\\/__/'].map(s=>S[++Y].splice(X,5,...s)):S[Y].splice(X+1,3,...'___'))))),S.map(r=>r.join``).join`
`)

f=

a=>(n=55,$=f=>[...Array(n)].map((_,i)=>f(i)),S=$(_=>$(_=>' ')),n=11,$(l=>$(z=>$(y=>$(x=>(x=10-x,X=x*3+y+z,Y=y-z+n,Z=a[y][x])<=z&&Z+z+1?0:l?['/\\__\\','\\/__/'].map(s=>S[++Y].splice(X,5,...s)):S[Y].splice(X+1,3,...'___'))))),S.map(r=>r.join``).join`
`)

console.log(f([
	[3, 0, 0, 0, 0, 0, 0, 0, 0, 0, -11],
	[2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
	[1, 0, 0, 7,-7,-7,-7,-7, 7, 0, 0],
	[0, 0, 0, 7,-7,-7,-7,-7, 7, 0, 0],
	[0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0],
	[0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0],
	[1, 0, 0, 4, 3, 2, 1, 0, 0, 0, 1],
	[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
	[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
	[1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 11]
]))

Saved 26 bytes from @Neil's suggestion.

Ungolfed

a=>(
    n=55,
    $=f=>[...Array(n)].map((_,i)=>f(i)),
    S=$(_=>$(_=>' ')),
    n=11,
    $(l=>
        $(z=>$(y=>$(x=>(
            x=10-x,
            X=x*3+y+z,
            Y=y-z+n,
            Z=a[y][x],
            Z<=z && Z+z+1 || (
                l
                ? ['/\\__\\','\\/__/'].map(s=>S[++Y].splice(X,5,...s))
                : S[Y].splice(X+1,3,...'___')
            )
        ))))
    ),
    S.map(r=>r.join``).join`\n`
)
\$\endgroup\$
2
  • 2
    \$\begingroup\$ ,$(w=>$(z=>$(y=>$(x=>(Z=a[y][x=10-x,X=x*3+y+z,Y=y-z+n,x])<=z&&Z+z+1?0:w?['/\\__\\','\\/__/'].map(s=>S[++Y].splice(X,5,...s)):S[Y].splice(X+1,3,...'___'))))), seems to save 26 bytes. \$\endgroup\$
    – Neil
    Jan 13, 2018 at 11:26
  • \$\begingroup\$ @Neil Brilliant! Drawing all the top lines first saves me the trouble of checking for non-spaces. \$\endgroup\$
    – darrylyeo
    Jan 13, 2018 at 16:14
6
\$\begingroup\$

Python 2, 243 bytes

a=input()
s=eval(`[[' ']*55]*23`)
for h in range(7986):
 k=h%3;x=h/3%11;y=h/33%11;z=h/363%11;i=h/3993;u=y+z-x*3+30;v=y-z+10
 if~-(z>=a[y][10-x]!=~z):
	if i*k:s[v+k][u:u+5]='\//\____/\\'[k%2::2]
	if~-i:s[v][u+1+k]='_'
for l in s:print''.join(l)

Try it online!

A Python translation of Neil’s Charcoal approach.

\$\endgroup\$
2
  • \$\begingroup\$ Nice to see a golfed Python solution for this. My Python proof-of-concept was more than 900 bytes! \$\endgroup\$ Jan 13, 2018 at 14:34
  • 3
    \$\begingroup\$ +1+k -> -~k. \$\endgroup\$ Jan 13, 2018 at 16:42
5
\$\begingroup\$

APL (Dyalog Unicode), 117 116 112 bytesSBCS

a←23 55⍴0⋄{i←11 0+⍵+.×3 2⍴3-7897⊤⍨6⍴5⋄(,2 5↑i↓a)←745366⊤⍨10⍴4⋄(3⍴i↓1⌽¯1⊖a)⌈←1}¨j[⍋-/↑j←⍸↑⎕↑¨⊂⌽1,11/⍳2]⋄' _/\'[a]

Try it online!

\$\endgroup\$
5
\$\begingroup\$

Tcl, 380 409 bytes

User sergiol has been busy crunching this down very nicely:

set X [read stdin]
proc L {a b c d e s} {time {incr z
set y -1
time {incr y
set x -1
time {if {abs([set Z [lindex $::X [expr ($y+1)*11-[incr x]-1]]])==$z|$z<$Z} {set s [string repl [string repl $s [set i [expr -3*$x+57*$y-55*abs($z)+701]] $i+$b $a] [incr i $c] $i+$e $d]}} 11} 11} 12
set s}
puts [L /\\__\\ 4 56 \\/__/ 4 [L "" -1 -55 ___ 2 [string repe [string repe \  55]\n 23]]]

Try it online!

Original Content

set xs [read stdin]
proc L {a b c d e s} {set z 0
while {[incr z]<12} {set y -1
while {[incr y]<11} {set x -1
while {[incr x]<11} {set Z [lindex $::xs [expr ($y+1)*11-$x-1]]
if {abs($Z)==$z||$z<$Z} {set i [expr -3*$x+57*$y-55*abs($z)+701]
set s [string repl [string repl $s $i $i+$b $a] [incr i $c] $i+$e $d]}}}}
set s}
puts [L /\\__\\ 4 56 \\/__/ 4 [L "" -1 -55 ___ 2 [string repe [string repe \  55]\n 23]]]

Try it online!

Alas, it is what it is. It’s only a tad easier on the eyes when “ungolfed”

set s [string repeat [string repeat " " 55]\n 23]

proc loops {s0 i0 io s1 i1} {
  set z  0; while {[incr z] < 12} {
  set y -1; while {[incr y] < 11} {
  set x -1; while {[incr x] < 11} {
    set Z [lindex $::xs [expr {($y+1) * 11 - $x - 1}]]
    if {abs($Z) == $z || $z < $Z} {
        set i [expr {-3*$x + 57*$y - 55*abs($z) + 701}]
        set ::s [string replace $::s $i $i+$i0 $s0]
        incr i $io
        set ::s [string replace $::s $i $i+$i1 $s1]
    }
  } } }
}

loops ""      -1 -55 \
       ___     2
loops /\\__\\  4  56 \
      \\/__/   4

puts $s

Builds a string, as per requirements. Takes the array from stdin. Goes from bottom to top, front to back, right to left over the string data. Does it in two passes, once for the top edge and again for the rest of the body of each cube.

I tried to make it smaller using some sweet functional lambda mojo, but alas, that made it larger.

\$\endgroup\$
9

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.