3D ASCII Block Building

83

22

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.

James Holderness

Posted 2018-01-12T20:10:23.100

Reputation: 8 298

9Ohh boy, get ready for 300+ byte solutions. Good challenge. +1 – totallyhuman – 2018-01-12T20:11:13.723

7@totallyhuman Nah, Dennis is going to have a 9-byte solution for this in 20 minutes. – Deacon – 2018-01-12T20:50:46.763

3Does 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. – Level River St – 2018-01-12T21:41:17.137

3I feel inclined to use a real game engine (or part of it) to render a photo and convert it to ASCII – Stan Strum – 2018-01-12T22:16:52.883

@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. – James Holderness – 2018-01-12T22:24:29.360

related – flawr – 2018-01-13T22:17:55.380

This reminds me of Snake Rattle 'n' Roll

– QBrute – 2018-01-14T17:35:32.487

@QBrute That's exactly the kind of thing I had in mind when I came up with this challenge. In my day, though, it was Knight Lore and Alien 8 that popularised the isometric style.

– James Holderness – 2018-01-14T19:17:47.680

Answers

25

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.

Neil

Posted 2018-01-12T20:10:23.100

Reputation: 95 035

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? – Fabian Röling – 2018-01-15T13:09:20.503

@Fabian I'm slightly confused that F¹¹F¹¹F¹¹ wasn't a clue... – Neil – 2018-01-15T16:06:21.017

I don't know this programming language, I just played around a bit with the TIO link. – Fabian Röling – 2018-01-15T20:30:45.410

30

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);
}

Steadybox

Posted 2018-01-12T20:10:23.100

Reputation: 15 798

Can 26*66 not be 1716? – Jonathan Frech – 2018-01-12T23:44:09.540

@JonathanFrech Sure, I forgot about that. – Steadybox – 2018-01-12T23:44:36.167

*s==32 -> *s<33. – Jonathan Frech – 2018-01-12T23:47:11.943

for(e=k=1;e;++k)for(e= -> for(k=1;e;++k)for(e=. – Jonathan Frech – 2018-01-12T23:48:11.990

#define B b(...)&++e -> #define B++e&b(...) (assuming b does not depend on e, which I think it does not). – Jonathan Frech – 2018-01-12T23:52:29.560

@JonathanFrech It doesn't. Thanks! – Steadybox – 2018-01-12T23:54:06.937

#define F for( and using whitespace saves no less bytes than #define F for and using an open-parenthesis after the F, and it somewhat adds to readability... other than that (very minor OCD detail) this is a great answer! +1 – MD XF – 2018-01-13T03:43:46.017

@MDXF Thanks, but doesn't #define F for( save one byte over #define F for, because there are two instances where with #define F for( there is no need for whitespace after F, because the init part of the loop is empty and the F can be directly followed by the semicolon? – Steadybox – 2018-01-13T10:36:54.710

@MDXF There are two occurrences of F; in this code. Redefining F to be for instead of for( saves one byte, replacing both F;s with F(; adds two. In total this would add one byte. – Jonathan Frech – 2018-01-13T10:38:35.030

@both Oh, I didn't notice that, sorry! – MD XF – 2018-01-13T17:32:32.987

9

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`
)

darrylyeo

Posted 2018-01-12T20:10:23.100

Reputation: 6 214

2,$(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. – Neil – 2018-01-13T11:26:18.247

@Neil Brilliant! Drawing all the top lines first saves me the trouble of checking for non-spaces. – darrylyeo – 2018-01-13T16:14:28.373

6

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.

Lynn

Posted 2018-01-12T20:10:23.100

Reputation: 55 648

Nice to see a golfed Python solution for this. My Python proof-of-concept was more than 900 bytes! – James Holderness – 2018-01-13T14:34:40.547

3+1+k -> -~k. – Jonathan Frech – 2018-01-13T16:42:25.080

5

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.

Dúthomhas

Posted 2018-01-12T20:10:23.100

Reputation: 541

The last 0 is not part of the answer! My fault! – sergiol – 2018-05-07T23:08:21.393

Oops! I'll fix it ASAP. – Dúthomhas – 2018-05-08T03:08:43.260

I fixed it myself – sergiol – 2018-11-30T01:56:19.600

5

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!

ngn

Posted 2018-01-12T20:10:23.100

Reputation: 11 449