Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In this challenge, You have to bring ASCII art (which are usually 2D) to 3D!

How?

like this,

X X DD
 X  D D
X X DD

to...

  X X DD
 X X DD D
X X DDDD
 X XDDD
X X DD

Then How do we do that?

Given the ascii art and N, repeat this N times.

  • for every character (we will call this A):
  • let B be the character which is exactly 1 right and 1 up from A
  • if B is a space or is undefined:
  • set B to A.

Specs

  • The first input can be a string with newline characters or a list of strings representing the 2D ASCII art.
  • You are allowed to use %END% to tag the end of input, but this is not required.
  • The second input will be N. It will be a positive integer.

Examples

Input: ("###\n###",1)

Output:

 ###
####
###

Rules

Basic rules apply.

also, If you have questions, be sure to ask in the comments!

share|improve this question
    
Can it be given as an array of strings? – Leaky Nun 5 hours ago
    
@LeakyNun Yes! inputting with arrays of strings are allowed. – Matthew Roh 5 hours ago
    
You might want to clarify that "blank" refers to space (U+0020) or nothing. – Leaky Nun 5 hours ago
    
@LeakyNun Is it fixed now? – Matthew Roh 4 hours ago
1  
Can I assume that the length of each line will be the same? (Can I pre-pad the input with spaces on the right?) – Leaky Nun 4 hours ago

CJam, 25 24 bytes

{{' 1$f+La@+..{sS@er}}*}

An unnamed block that expects a list of strings and the number of repetitions on the stack and leaves a new list of strings instead.

Test it here. (Includes a test wrapper that reads the string from STDIN for convenience.)

Explanation

{       e# Repeat this block N times...
  '     e#   Push a space character.
  1$    e#   Copy the current grid.
  f+    e#   Prepend the space to each line of the grid.
  La    e#   Push [[]].
  @+    e#   Pull up the other copy of the grid and prepend the [].
        e#   We've now got two copies of the grid, one shifted right by
        e#   a cell and one shifted down by a cell. We now want to replace
        e#   spaces in the latter with the corresponding character in the
        e#   former.
  ..{   e#   For each pair of characters in corresponding positions...
    s   e#     Turn the character in the down-shifted grid into a string.
    S   e#     Push " ".
    @   e#     Pull up the character from the right-shifted grid.
    er  e#     Replace spaces with that character.
  }
}*
share|improve this answer
3  
Ten roflcopters! goo.gl/PEK4iB – Matthew Roh 4 hours ago
    
Why doesn't S work for the initial space? Also, is it allowed to use variables (which might have been overwritten) in a function? – Luis Mendo 56 mins ago
    
@LuisMendo S doesn't work, because then f would be mapping over that string instead. Re functions, I believe so, in "normal" languages, there are also many functions submissions that rely globals which aren't tampered with between invocations. – Martin Ender 54 mins ago
    
Thanks. I forgot that a character in CJam is not the same as a one-character string – Luis Mendo 42 mins ago

Perl, 81 bytes

75 bytes code + 6 for -i -n0.
Note that the \e characters are ASCII \x1b but \e is used for ease of testing.

Please note that this solution uses ANSI escape sequences and requires a compatible terminal, as well as using the -i commandline argument to pass in the number of 'dimensions' you'd like.

$n=s/^//mg-1;s/ /\e[1C/g;print(s/^/\e[${^I}C/grm."\e[${n}A"),--$^I for($_)x$^I

Usage:

In a Linux compatible terminal, run PS1= first to ensure your prompt doesn't overwrite the displayed image.

perl -i10 -n0e '$n=s/^//mg-1;s/ /\e[1C/g;print(s/^/\e[${^I}C/grm."\e[${n}A"),--$^I for($_)x$^I' <<< ' 
ROFL:ROFL:ROFL:ROFL
         _^___
 L    __/   [] \    
LOL===__        \ 
 L      \________]
         I   I
        --------/
'

          ROFL:ROFL:ROFL:ROFL
         ROFL:ROFL:ROFL:ROFL
        ROFL:ROFL:ROFL:ROFL
       ROFL:ROFL:ROFL:ROFL\
      ROFL:ROFL:ROFL:ROFL\_]
     ROFL:ROFL:ROFL:ROFL\_]
    ROFL:ROFL:ROFL:ROFL\_]/
   ROFL:ROFL:ROFL:ROFL\_]/
  ROFL:ROFL:ROFL:ROFL\_]/
 ROFL:ROFL:ROFL:ROFL\_]/
   LOL==___^___]_\_\_]/
  LOL==__/ \_[]_\_\_]/
 LOL===__ \______\_]/
  L      \________]/
          I---I---/
         --------/

perl -i3 -n0e '$n=s/^//mg-1;s/ /\e[1C/g;print(s/^/\e[${^I}C/grm."\e[${n}A"),--$^I for($_)x$^I' <<< 'X X DD
 X  D D
X X DD
'
   X X DD
  X X DD D
 X X DDDD
  X XDDD
 X X DD
share|improve this answer

APL, 49 bytes

{⎕UCS 32⌈{s+(s=0)×1⊖¯1⌽s←0⍪⍵,0}⍣⍺⊣(32∘≠×⊣)⎕UCS↑⍵}

Input: vector of character vectors. Example:

      2 {⎕UCS 32⌈{s+(s=0)×1⊖¯1⌽s←0⍪⍵,0}⍣⍺⊣(32∘≠×⊣)⎕UCS↑⍵} 'X X DD' ' X  D D' 'X X DD'
  X X DD 
 X X DD D
X X DDDD 
 X XDDD  
X X DD   

How it works:

  • ↑⍵ turns the argument into a matrix of chars
  • ⎕UCS from char to integer
  • (32∘≠×⊣) substitute the spaces (32) with zeroes
  • ...⍣⍺⊣ apply ⍺ (the left argument) times the function on the left
  • s←0⍪⍵,0 border with zeroes on top and on the right the argument
  • 1⊖¯1⌽ rotate 1 up and 1 right
  • s+(s=0)× sum to the original the shifted version but only on top of the zeroes of the original
  • 32⌈ turns back the zeroes into 32s
  • ⎕UCS from integer to char
share|improve this answer

Pyth, 54 33 bytes

ju+++dhG.bsmh|-d;;.t,Y+dNdtGGeG.*

Test suite.

share|improve this answer
    
why do you need two ;? – ven 3 hours ago
    
@ven ; is not the usual ; in programming languages. – Leaky Nun 3 hours ago
    
; is a variable. – Leaky Nun 3 hours ago
    
ah pyth overloads ; in lambdas... – ven 3 hours ago
    
@ven When you get along with Pyth, you would use I, .?, V, F, ;, (explicit statements) very less, and they will be replaced by ?, m, u, F, M, L, R, #, ... – Leaky Nun 3 hours ago

MATL, 24 bytes

:"ct32>*TTYatFTEqYSy~*+c

Input format is

2
{'X X DD', ' X  D D', 'X X DD'}

So the other example is

1
{'###', '###'}

The output contains extra whitespace, which is allowed by the challenge.

Try it online!

share|improve this answer

Ruby, 97 bytes

f=->a,n{n.downto(0){|i|f="<Esc>[1C"
$><<a.gsub(/^/,f*i).gsub(" ",f)+(i>0?"<Esc>[#{a.lines.size-1}A":"")}}

Each <Esc> is a literal ESC character (0x1b).

Usage

Assign the lambda to a variable e.g. func.

art = <<END
X X DD
 X  D D
X X DD
END

func[art, 2]
# Prints:
#   X X DD
#  X X DD D
# X X DDDD
#  X XDDD
# X X DD

Ungolfed

->(art, num) {
  num.downto(0) do |i|
    forward = "\e[1C"
    $> << art.gsub(/^/, forward * i).gsub(" ", forward) +
            (i > 0 ? "\e[#{art.lines.size - 1}A" : "")
  end
}

The forward escape sequence, \e[1C, moves the cursor forward (right) 1 space and \e[<n>A moves it up n lines. Basically what this code does is print the "layers" back to front, replacing spaces with the forward sequence to avoid overwriting the other layers with a space.

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.