27
\$\begingroup\$

Any regular hexagon can be tiled with diamonds, for instance like so:

   ______
  /_/_/\_\
 /_/\_\/\_\
/\_\/_/\/_/\
\/_/\_\/_/\/
 \_\/_/\_\/
  \_\_\/_/

We'll consider the above a tiling of size 1 (since the diamonds' sides are made of one / or \ each). The same tiling of size 2 would look like:

      ____________ 
     /   /   /\   \  
    /___/___/  \___\ 
   /   /\   \  /\   \  
  /___/  \___\/  \___\ 
 /\   \  /   /\  /   /\  
/  \___\/___/  \/___/  \ 
\  /   /\   \  /   /\  /
 \/___/  \___\/___/  \/ 
  \   \  /   /\   \  /
   \___\/___/  \___\/ 
    \   \   \  /   /
     \___\___\/___/ 

Your task is to receive such an ASCII art tiling (of size 1) as input, along with a positive integer N (in decimal or unary), specifying the size of the desired output. You should then output a scaled-up version of the same tiling.

Note that the hexagon could be any size and as small as 1x1x1 (containing three diamonds).

Neither input nor output must contain any trailing spaces, nor more leading spaces than necessary to align the hexagon shape. Both input and output may optionally contain a single trailing newline (this choice does not have to be the same for input and output).

You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument and outputting the result via STDOUT (or closest alternative), function return value or function (out) parameter.

This is code golf, so the shortest answer (in bytes) in wins.

Example Tilings

Here are a handful of input tilings you can use to test your submission.

 __
/_/\
\_\/

  ____
 /_/\_\
/\_\/_/\
\/_/\_\/
 \_\/_/

   ______
  /_/_/\_\
 /_/\_\/\_\
/\_\/_/\/_/\
\/_/\_\/_/\/
 \_\/_/\_\/
  \_\_\/_/

    ________
   /_/\_\_\_\
  /\_\/\_\_\_\
 /\/_/\/_/_/\_\
/\/\_\/_/_/\/\_\ 
\/\/_/\_\_\/\/_/
 \/\_\/_/\_\/_/
  \/\_\_\/_/_/
   \/_/_/_/_/

The following snippet contains the corresponding outputs for inputs N = 1 through N = 6.

 __
/_/\
\_\/

  ____
 /   /\
/___/  \
\   \  /
 \___\/

   ______
  /     /\
 /     /  \
/_____/    \
\     \    /
 \     \  /
  \_____\/

    ________
   /       /\
  /       /  \
 /       /    \
/_______/      \
\       \      /
 \       \    /
  \       \  /
   \_______\/

     __________
    /         /\
   /         /  \
  /         /    \
 /         /      \
/_________/        \
\         \        /
 \         \      /
  \         \    /
   \         \  /
    \_________\/

      ____________
     /           /\
    /           /  \
   /           /    \
  /           /      \
 /           /        \
/___________/          \
\           \          /
 \           \        /
  \           \      /
   \           \    /
    \           \  /
     \___________\/

  ____
 /_/\_\
/\_\/_/\
\/_/\_\/
 \_\/_/

    ________
   /   /\   \
  /___/  \___\
 /\   \  /   /\
/  \___\/___/  \
\  /   /\   \  /
 \/___/  \___\/
  \   \  /   /
   \___\/___/

      ____________
     /     /\     \
    /     /  \     \
   /_____/    \_____\
  /\     \    /     /\
 /  \     \  /     /  \
/    \_____\/_____/    \
\    /     /\     \    /
 \  /     /  \     \  /
  \/_____/    \_____\/
   \     \    /     /
    \     \  /     /
     \_____\/_____/

        ________________
       /       /\       \
      /       /  \       \
     /       /    \       \
    /_______/      \_______\
   /\       \      /       /\
  /  \       \    /       /  \
 /    \       \  /       /    \
/      \_______\/_______/      \
\      /       /\       \      /
 \    /       /  \       \    /
  \  /       /    \       \  /
   \/_______/      \_______\/
    \       \      /       /
     \       \    /       /
      \       \  /       /
       \_______\/_______/

          ____________________
         /         /\         \
        /         /  \         \
       /         /    \         \
      /         /      \         \
     /_________/        \_________\
    /\         \        /         /\
   /  \         \      /         /  \
  /    \         \    /         /    \
 /      \         \  /         /      \
/        \_________\/_________/        \
\        /         /\         \        /
 \      /         /  \         \      /
  \    /         /    \         \    /
   \  /         /      \         \  /
    \/_________/        \_________\/
     \         \        /         /
      \         \      /         /
       \         \    /         /
        \         \  /         /
         \_________\/_________/

            ________________________
           /           /\           \
          /           /  \           \
         /           /    \           \
        /           /      \           \
       /           /        \           \
      /___________/          \___________\
     /\           \          /           /\
    /  \           \        /           /  \
   /    \           \      /           /    \
  /      \           \    /           /      \
 /        \           \  /           /        \
/          \___________\/___________/          \
\          /           /\           \          /
 \        /           /  \           \        /
  \      /           /    \           \      /
   \    /           /      \           \    /
    \  /           /        \           \  /
     \/___________/          \___________\/
      \           \          /           /
       \           \        /           /
        \           \      /           /
         \           \    /           /
          \           \  /           /
           \___________\/___________/

   ______
  /_/_/\_\
 /_/\_\/\_\
/\_\/_/\/_/\
\/_/\_\/_/\/
 \_\/_/\_\/
  \_\_\/_/

      ____________
     /   /   /\   \
    /___/___/  \___\
   /   /\   \  /\   \
  /___/  \___\/  \___\
 /\   \  /   /\  /   /\
/  \___\/___/  \/___/  \
\  /   /\   \  /   /\  /
 \/___/  \___\/___/  \/
  \   \  /   /\   \  /
   \___\/___/  \___\/
    \   \   \  /   /
     \___\___\/___/

         __________________
        /     /     /\     \
       /     /     /  \     \
      /_____/_____/    \_____\
     /     /\     \    /\     \
    /     /  \     \  /  \     \
   /_____/    \_____\/    \_____\
  /\     \    /     /\    /     /\
 /  \     \  /     /  \  /     /  \
/    \_____\/_____/    \/_____/    \
\    /     /\     \    /     /\    /
 \  /     /  \     \  /     /  \  /
  \/_____/    \_____\/_____/    \/
   \     \    /     /\     \    /
    \     \  /     /  \     \  /
     \_____\/_____/    \_____\/
      \     \     \    /     /
       \     \     \  /     /
        \_____\_____\/_____/

            ________________________
           /       /       /\       \
          /       /       /  \       \
         /       /       /    \       \
        /_______/_______/      \_______\
       /       /\       \      /\       \
      /       /  \       \    /  \       \
     /       /    \       \  /    \       \
    /_______/      \_______\/      \_______\
   /\       \      /       /\      /       /\
  /  \       \    /       /  \    /       /  \
 /    \       \  /       /    \  /       /    \
/      \_______\/_______/      \/_______/      \
\      /       /\       \      /       /\      /
 \    /       /  \       \    /       /  \    /
  \  /       /    \       \  /       /    \  /
   \/_______/      \_______\/_______/      \/
    \       \      /       /\       \      /
     \       \    /       /  \       \    /
      \       \  /       /    \       \  /
       \_______\/_______/      \_______\/
        \       \       \      /       /
         \       \       \    /       /
          \       \       \  /       /
           \_______\_______\/_______/

               ______________________________
              /         /         /\         \
             /         /         /  \         \
            /         /         /    \         \
           /         /         /      \         \
          /_________/_________/        \_________\
         /         /\         \        /\         \
        /         /  \         \      /  \         \
       /         /    \         \    /    \         \
      /         /      \         \  /      \         \
     /_________/        \_________\/        \_________\
    /\         \        /         /\        /         /\
   /  \         \      /         /  \      /         /  \
  /    \         \    /         /    \    /         /    \
 /      \         \  /         /      \  /         /      \
/        \_________\/_________/        \/_________/        \
\        /         /\         \        /         /\        /
 \      /         /  \         \      /         /  \      /
  \    /         /    \         \    /         /    \    /
   \  /         /      \         \  /         /      \  /
    \/_________/        \_________\/_________/        \/
     \         \        /         /\         \        /
      \         \      /         /  \         \      /
       \         \    /         /    \         \    /
        \         \  /         /      \         \  /
         \_________\/_________/        \_________\/
          \         \         \        /         /
           \         \         \      /         /
            \         \         \    /         /
             \         \         \  /         /
              \_________\_________\/_________/

                  ____________________________________
                 /           /           /\           \
                /           /           /  \           \
               /           /           /    \           \
              /           /           /      \           \
             /           /           /        \           \
            /___________/___________/          \___________\
           /           /\           \          /\           \
          /           /  \           \        /  \           \
         /           /    \           \      /    \           \
        /           /      \           \    /      \           \
       /           /        \           \  /        \           \
      /___________/          \___________\/          \___________\
     /\           \          /           /\          /           /\
    /  \           \        /           /  \        /           /  \
   /    \           \      /           /    \      /           /    \
  /      \           \    /           /      \    /           /      \
 /        \           \  /           /        \  /           /        \
/          \___________\/___________/          \/___________/          \
\          /           /\           \          /           /\          /
 \        /           /  \           \        /           /  \        /
  \      /           /    \           \      /           /    \      /
   \    /           /      \           \    /           /      \    /
    \  /           /        \           \  /           /        \  /
     \/___________/          \___________\/___________/          \/
      \           \          /           /\           \          /
       \           \        /           /  \           \        /
        \           \      /           /    \           \      /
         \           \    /           /      \           \    /
          \           \  /           /        \           \  /
           \___________\/___________/          \___________\/
            \           \           \          /           /
             \           \           \        /           /
              \           \           \      /           /
               \           \           \    /           /
                \           \           \  /           /
                 \___________\___________\/___________/

    ________
   /_/\_\_\_\
  /\_\/\_\_\_\
 /\/_/\/_/_/\_\
/\/\_\/_/_/\/\_\
\/\/_/\_\_\/\/_/
 \/\_\/_/\_\/_/
  \/\_\_\/_/_/
   \/_/_/_/_/

        ________________
       /   /\   \   \   \
      /___/  \___\___\___\
     /\   \  /\   \   \   \
    /  \___\/  \___\___\___\
   /\  /   /\  /   /   /\   \
  /  \/___/  \/___/___/  \___\
 /\  /\   \  /   /   /\  /\   \
/  \/  \___\/___/___/  \/  \___\
\  /\  /   /\   \   \  /\  /   /
 \/  \/___/  \___\___\/  \/___/
  \  /\   \  /   /\   \  /   /
   \/  \___\/___/  \___\/___/
    \  /\   \   \  /   /   /
     \/  \___\___\/___/___/
      \  /   /   /   /   /
       \/___/___/___/___/

            ________________________
           /     /\     \     \     \
          /     /  \     \     \     \
         /_____/    \_____\_____\_____\
        /\     \    /\     \     \     \
       /  \     \  /  \     \     \     \
      /    \_____\/    \_____\_____\_____\
     /\    /     /\    /     /     /\     \
    /  \  /     /  \  /     /     /  \     \
   /    \/_____/    \/_____/_____/    \_____\
  /\    /\     \    /     /     /\    /\     \
 /  \  /  \     \  /     /     /  \  /  \     \
/    \/    \_____\/_____/_____/    \/    \_____\
\    /\    /     /\     \     \    /\    /     /
 \  /  \  /     /  \     \     \  /  \  /     /
  \/    \/_____/    \_____\_____\/    \/_____/
   \    /\     \    /     /\     \    /     /
    \  /  \     \  /     /  \     \  /     /
     \/    \_____\/_____/    \_____\/_____/
      \    /\     \     \    /     /     /
       \  /  \     \     \  /     /     /
        \/    \_____\_____\/_____/_____/
         \    /     /     /     /     /
          \  /     /     /     /     /
           \/_____/_____/_____/_____/

                ________________________________
               /       /\       \       \       \
              /       /  \       \       \       \
             /       /    \       \       \       \
            /_______/      \_______\_______\_______\
           /\       \      /\       \       \       \
          /  \       \    /  \       \       \       \
         /    \       \  /    \       \       \       \
        /      \_______\/      \_______\_______\_______\
       /\      /       /\      /       /       /\       \
      /  \    /       /  \    /       /       /  \       \
     /    \  /       /    \  /       /       /    \       \
    /      \/_______/      \/_______/_______/      \_______\
   /\      /\       \      /       /       /\      /\       \
  /  \    /  \       \    /       /       /  \    /  \       \
 /    \  /    \       \  /       /       /    \  /    \       \
/      \/      \_______\/_______/_______/      \/      \_______\
\      /\      /       /\       \       \      /\      /       /
 \    /  \    /       /  \       \       \    /  \    /       /
  \  /    \  /       /    \       \       \  /    \  /       /
   \/      \/_______/      \_______\_______\/      \/_______/
    \      /\       \      /       /\       \      /       /
     \    /  \       \    /       /  \       \    /       /
      \  /    \       \  /       /    \       \  /       /
       \/      \_______\/_______/      \_______\/_______/
        \      /\       \       \      /       /       /
         \    /  \       \       \    /       /       /
          \  /    \       \       \  /       /       /
           \/      \_______\_______\/_______/_______/
            \      /       /       /       /       /
             \    /       /       /       /       /
              \  /       /       /       /       /
               \/_______/_______/_______/_______/

                    ________________________________________
                   /         /\         \         \         \
                  /         /  \         \         \         \
                 /         /    \         \         \         \
                /         /      \         \         \         \
               /_________/        \_________\_________\_________\
              /\         \        /\         \         \         \
             /  \         \      /  \         \         \         \
            /    \         \    /    \         \         \         \
           /      \         \  /      \         \         \         \
          /        \_________\/        \_________\_________\_________\
         /\        /         /\        /         /         /\         \
        /  \      /         /  \      /         /         /  \         \
       /    \    /         /    \    /         /         /    \         \
      /      \  /         /      \  /         /         /      \         \
     /        \/_________/        \/_________/_________/        \_________\
    /\        /\         \        /         /         /\        /\         \
   /  \      /  \         \      /         /         /  \      /  \         \
  /    \    /    \         \    /         /         /    \    /    \         \
 /      \  /      \         \  /         /         /      \  /      \         \
/        \/        \_________\/_________/_________/        \/        \_________\
\        /\        /         /\         \         \        /\        /         /
 \      /  \      /         /  \         \         \      /  \      /         /
  \    /    \    /         /    \         \         \    /    \    /         /
   \  /      \  /         /      \         \         \  /      \  /         /
    \/        \/_________/        \_________\_________\/        \/_________/
     \        /\         \        /         /\         \        /         /
      \      /  \         \      /         /  \         \      /         /
       \    /    \         \    /         /    \         \    /         /
        \  /      \         \  /         /      \         \  /         /
         \/        \_________\/_________/        \_________\/_________/
          \        /\         \         \        /         /         /
           \      /  \         \         \      /         /         /
            \    /    \         \         \    /         /         /
             \  /      \         \         \  /         /         /
              \/        \_________\_________\/_________/_________/
               \        /         /         /         /         /
                \      /         /         /         /         /
                 \    /         /         /         /         /
                  \  /         /         /         /         /
                   \/_________/_________/_________/_________/

                        ________________________________________________
                       /           /\           \           \           \
                      /           /  \           \           \           \
                     /           /    \           \           \           \
                    /           /      \           \           \           \
                   /           /        \           \           \           \
                  /___________/          \___________\___________\___________\
                 /\           \          /\           \           \           \
                /  \           \        /  \           \           \           \
               /    \           \      /    \           \           \           \
              /      \           \    /      \           \           \           \
             /        \           \  /        \           \           \           \
            /          \___________\/          \___________\___________\___________\
           /\          /           /\          /           /           /\           \
          /  \        /           /  \        /           /           /  \           \
         /    \      /           /    \      /           /           /    \           \
        /      \    /           /      \    /           /           /      \           \
       /        \  /           /        \  /           /           /        \           \
      /          \/___________/          \/___________/___________/          \___________\
     /\          /\           \          /           /           /\          /\           \
    /  \        /  \           \        /           /           /  \        /  \           \
   /    \      /    \           \      /           /           /    \      /    \           \
  /      \    /      \           \    /           /           /      \    /      \           \
 /        \  /        \           \  /           /           /        \  /        \           \
/          \/          \___________\/___________/___________/          \/          \___________\
\          /\          /           /\           \           \          /\          /           /
 \        /  \        /           /  \           \           \        /  \        /           /
  \      /    \      /           /    \           \           \      /    \      /           /
   \    /      \    /           /      \           \           \    /      \    /           /
    \  /        \  /           /        \           \           \  /        \  /           /
     \/          \/___________/          \___________\___________\/          \/___________/
      \          /\           \          /           /\           \          /           /
       \        /  \           \        /           /  \           \        /           /
        \      /    \           \      /           /    \           \      /           /
         \    /      \           \    /           /      \           \    /           /
          \  /        \           \  /           /        \           \  /           /
           \/          \___________\/___________/          \___________\/___________/
            \          /\           \           \          /           /           /
             \        /  \           \           \        /           /           /
              \      /    \           \           \      /           /           /
               \    /      \           \           \    /           /           /
                \  /        \           \           \  /           /           /
                 \/          \___________\___________\/___________/___________/
                  \          /           /           /           /           /
                   \        /           /           /           /           /
                    \      /           /           /           /           /
                     \    /           /           /           /           /
                      \  /           /           /           /           /
                       \/___________/___________/___________/___________/

\$\endgroup\$
13
  • 20
    \$\begingroup\$ I see you are interested in diamonds now that you've got one next to your user name. \$\endgroup\$
    – user12205
    Commented May 21, 2015 at 13:34
  • 3
    \$\begingroup\$ @ace: You know what they say: diamonds are a moderator's best friend. \$\endgroup\$
    – Alex A.
    Commented May 21, 2015 at 20:20
  • \$\begingroup\$ I think I know the answer, but I hope I'm wrong: Leading empty lines would count as leading space, which you declared as illegal? My initial solution has N-1 leading empty lines. :( \$\endgroup\$ Commented May 23, 2015 at 23:40
  • \$\begingroup\$ @RetoKoradi Indeed, no leading newlines. Sorry. \$\endgroup\$ Commented May 24, 2015 at 0:00
  • 1
    \$\begingroup\$ I figured. Cost me about 10 bytes. Not as bad as it looked when I first recognized the problem. \$\endgroup\$ Commented May 24, 2015 at 0:46

7 Answers 7

10
+450
\$\begingroup\$

Python 2, 164

def g(s,n,j=1):
 for W in s.split("\n"):exec"O=p='';d=0\nfor c in W:q=' _'[j*'_'in p+c];e=[n-j,j-1][c=='/'];O+=q*(e+d)+[c,q][c>'^'];p=c;d=n+~e\nprint O;j-=1;"*j;j=n

Output on test cases.

So, what's going on here?

The main idea is that each character in the original blows up into an n*n block. For example, for n=4, / might become

   /
  /
 /
/___

The original character appears once in each line, and there's padding to either side. Here, it's on the left, and and _ on the right. Only the bottom row can be padded by '_'; the rest is always ' '.

The main difficulty is that the padding on the right can depend on the upcoming symbol. Specifically, '/ ' has different padding from '/_', so we'd need some look-ahead. Moreover, to avoid trailing spaces, we'd have to note that we're at the last symbol and refrain from padding on the right.

We overcome both these issues by only padding to the left of the current character. When we do so, we also perform the right-padding from the previous character using the current padding symbol. So, we print the right-padding owed from the previous char, the left-padding from the current char, then the current char. We also store the amount of padding "debt" the next char will need to pay.

Now let's go over the code.

def g(s,n,j=1):
    for W in s.split("\n"):
        while j:
            O=p='';d=0
            for c in W:
                q=' _'[j*'_'in p+c]
                e=[n-j,j-1][c=='/']
                O+=q*(e+d)+[c,q][c>'^']
                p=c
                d=n+~e
            print O;j-=1;
        j=n

The input string is s and the scale-factor is n. We go line-by-line, printing n lines for each input line W, with the copies indexed j=n,n-1,...,2,1. The first line is only copied once, which we achieve this by initializing j to 1 but changing it to n each loop.

We iterate through the input line, accumulating the line to print in O. First, we figure out the appropriate padding character q, which is an underscore if we're on the bottom line and either the current or previous character is an underscore, and otherwise a space.

Then, we decide the amount of padding to put on the left (e). For /, it's j-1 (decreasing with decreasing line-copy number), and the complementary n-j for \. We treat other characters the same way. For example, though _ seems like it gives a row of n underscores, it actually gives a single underscore, padded by underscores on the left and right. This seems inefficient, but it lets us works with _ and in the same framework as / and \ The position of the "central" underscore doesn't matter, so we lump it and with \; this choice also makes the top row work out without special casing.

Next, we add to the output string. We've figured out the padding symbol q, and the current padding amount e, but we also need to remember the padding debt d from the previous symbol. So, we add q*(e+d). Then, we add the current symbol c, except we need to avoid underscores in a non-bottom row, which we fix by converting underscores to the padding symbol.

Finally, we record the padding debt amount, which the complement n+~d of the current left-padding amount. We also record the current symbol in p, so that we will later know whether the previous symbol was _.

\$\endgroup\$
8
\$\begingroup\$

CJam, 85 79 76 72 bytes

li:Tlf*NqN/T,f{ff{"_/"2$#_1<@+*~ST*@t}:+z{S+e`);e~{"_ "/"__"*W%}T2**N}/}

The size should be on the first line. And the diamond follows.

Not golfed very well... And half of the characters came from the details.

Explanations (of previous version)

li:T            e# Read the size and save to T.
qN/             e# Read and split to lines.
\,fm*           e# Convert each character X to [X 0] [X 1]... [X T(]
{~              e# For each [X I] in each line:
    ST*         e# T spaces.
    \           e# I.
    "_\\"3$#    e# If X is '_, '\ or '/, return Y = 0, 1, -1 respectively.
    _W>@+       e# If it was '_ or '\, increase I by one.
    *(          e# I * Y - 1.
    @t          e# Set the character at that position to X.
}f%
:z:+            e# Make the returned lists from each iteration across T lines.
{S+e`);e~N+}%   e# Boring details to remove trailing spaces and append a newline.
T(>(\s          e# Boring details to extract the first line and only work on others.
{_{"_ "/"__"*W%}2*_@=!}g
                e# Boring details to make underlines expand left and right.
\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6) 274 281 289 338

// GOLFED
F=(b,n)=>{
b=b[R='replace'](/ |_/g,c=>c[T='repeat'](n))[R](/_(?=[\\\/])/g,'_'[T](n))[R](/\/\\/g,`/${'  '[T](n-1)}\\`)
.split('\n');
for(i=l=b.length*n-n+1;--i;)
b[i]=i%n?b[i+1][R](/_/g,' ')[R](/\/ ?/g,' /')[R](/ \\(.)?/g,'\\$1$1')
:' '[T](i>l/2?n-1:0)+b[i/n];
return b.join('\n')}


// UNGOLFED
U=(b,n)=>{
  b=b
  .replace(/ |_/g,c=>c.repeat(n))
  .replace(/_(?=[\/\\])/g,'_'.repeat(n))
  .replace(/\/\\/g,`/${'  '.repeat(n-1)}\\`)
  .split('\n');
  for(i=l=b.length*n-n+1;--i;)
  {
    if(i%n)
     b[i]=b[i+1]
     .replace(/_/g,' ')
     .replace(/\/ ?/g,' /')
     .replace(/ \\/g,'\\ ').replace(/ +$/,'')
     .replace(/ \\(.)?/g,'\\$1$1')
    else {
      b[i]=b[i/n]
      if(i>l/2)b[i]=' '.repeat(n-1)+b[i];
    }
  }
  return b.join('\n')
}

//TEST

test=[
' __\n/_/\\\n\\_\\/',
'  ____\n /_/\\_\\\n/\\_\\/_/\\\n\\/_/\\_\\/\n \\_\\/_/',
'   ______\n  /_/_/\\_\\\n /_/\\_\\/\\_\\\n/\\_\\/_/\\/_/\\\n\\/_/\\_\\/_/\\/\n \\_\\/_/\\_\\/\n  \\_\\_\\/_/',
'    ________\n   /_/\\_\\_\\_\\\n  /\\_\\/\\_\\_\\_\\\n /\\/_/\\/_/_/\\_\\\n/\\/\\_\\/_/_/\\/\\_\\\n\\/\\/_/\\_\\_\\/\\/_/\n \\/\\_\\/_/\\_\\/_/\n  \\/\\_\\_\\/_/_/\n   \\/_/_/_/_/'
]

test.forEach(t=>{
  var row = '<td>'+t+'<td>'
  for(rr=2;rr<5;rr++)
    row += '<td>'+F(t,rr)+'</td>'
  OUT.innerHTML += '<tr>'+row+'</tr>'
})
td {
  font-family: monospace;
  white-space: pre;
}
(better full page)
<table id=OUT></table>

\$\endgroup\$
3
\$\begingroup\$

Python 2, 217 211 195 194 190

b,f,s='\/ '
R=str.replace
def m(g,n,z=1):
 for d in g.split('\n'):
    for i in range(z):a=z+~i;print[i,a][d[-1]>f]*s+R(R(R(R(d,s,s*n),'_','_ '[i<z-1]*(z+n-1)),f+b,f+s*2*i+b),b+f,b+s*2*a+f);z=n

6 bytes thanks to Sp3000.

Call m with the first argument being the diamond as a string, and the second argument the repetition number.

This is based on a 3-step string replacement sequence:

  • First, replace underscores with 2n-1 spaces or underscores, depending on the line.
  • Second, replace /\ with / \, with the number of intervening spaces going from 2 to 2*(n-1) over the lines.
  • Third, replace \/ with \ /, with the number of intervening spaces going from 2*(n-1) to 2 over the lines.

Then, there's a variety of mucking about to get the leading spaces right, and to get the first line right.

Note that the final line of the program should be a tab, not 4 spaces. Markdown doesn't support tabs.

\$\endgroup\$
1
  • \$\begingroup\$ Two golfs: (i+(n-i+~i)*(d[-1]>f)) --> [i,n+~i][d[-1]>f] and you only use '_' once, so you waste a byte defining it. \$\endgroup\$
    – Sp3000
    Commented May 31, 2015 at 12:24
3
\$\begingroup\$

Python, 272 238 228 243 bytes

Updated version, now takes a single string as input, instead of a string sequence. Also removes trailing whitespace that was present in earlier version. Unfortunately these changes increase the size.

s,u,f,b=' _/\\'
r=str.replace
def d(t,n,j=1):
 for p in t.split('\n'):
  for k in range(n-j,n):m,v=n+~k,'_ '[k<n-1];print r(r(r(r(r(r(r(r(p,f+u,'(_'),u+b,'_)'),s,s*n),u,v*n),f,s*m+f+s*k),'(',v*m+f+v*k),b,s*k+b+s*m),')',v*k+b+v*m).rstrip();j=n

Version with whitespace, and statements broken up into smaller units for readability:

s, u, f, b = ' ', '_', '/', '\\'
def d(t, n):
    j = n - 1
    for p in t:
        for k in range(j, n):
            m, v = n - 1 - k, '_ '[k < n - 1]
            q = p[:-1]
            q = q.replace(f + u, '(_')
            q = q.replace(u + b, '_)')
            q = q.replace(s, s * n)
            q = q.replace(u, v * n)
            q = q.replace(f, s * m + f + s * k)
            q = q.replace('(', v * m + f + v * k)
            q = q.replace(b, s * k + b + s * m)
            q = q.replace(')', v * k + b + v * m)
            print q
            j = 0

The basic approach here is:

  1. Loop over all lines in input.
  2. For each line, loop over the output size N, generating an output line in each loop iteration. There's a special case for the first line, where only the last output line is generated, to avoid generating empty lines at the start of the output.
  3. Replace each character in the line by N characters, where:
    • Each space is replaced by N spaces.
    • Each underscore is replaced by N spaces for the first N -1 loop iterations, and N underscores for the last loop iteration.
    • Slashes and backslashes are padded with N - 1 spaces or underscores.

The most tricky part here is that the padding for slashes/backslashes uses spaces or underscores depending on the next (for slashes) or previous (for backslashes) input character. That did not seem to fit well with the string substitution strategy.

What I did to resolve this is that I first replace certain two character combinations with different characters, so that I can treat them differently during the actual substitution. For example, /_ is replaced by (_. After this, ( is effectively a "slash followed by underscore", which can then be substituted accordingly.

Main program used for testing the function:

import sys
import Golf

n = int(sys.argv[1])
t = ''.join(sys.stdin).rstrip()

Golf.d(t, n)
\$\endgroup\$
7
  • 1
    \$\begingroup\$ n-1-k is n+~k \$\endgroup\$
    – recursive
    Commented May 28, 2015 at 14:57
  • \$\begingroup\$ In the interest of full disclosure: I just found that my solution produces some trailing spaces. Since that's not allowed in the output definition, it does not meet the requirements. In the worst case, I'll have to add an .rstrip() for 9 more characters. I hope I can do better, and also found a way to cut out 5 characters. \$\endgroup\$ Commented May 31, 2015 at 4:43
  • \$\begingroup\$ It looks like your input format is not allowed.sys.stdin is not an allowable input parameter - you need to manipulate the string itself. \$\endgroup\$
    – isaacg
    Commented May 31, 2015 at 8:41
  • \$\begingroup\$ Well you can use sys.stdin and int(sys.argv[1]) but you're not gonna get them for free by expecting them to be passed in as variables (if that was fair game, then you could also expect aliases for range and replace` and whatever else you need to be predefine). \$\endgroup\$ Commented May 31, 2015 at 11:33
  • \$\begingroup\$ @MartinBüttner It says that I can take the input as a function argument. Isn't this what I'm doing here? I was going to change the function argument to a list of strings anyway. Would that be ok? It's not very different, since both stdin and a list of strings are string sequences. \$\endgroup\$ Commented May 31, 2015 at 15:36
1
\$\begingroup\$

Perl, 132

#!perl -p
INIT{$f=pop}s!.!$&x$f!ge;s! $!! while s!\\+\K\\|/(/)! $1!;
for$x(2..m!/!*$f){print y!_! !r;s!\\.?! \\!g;s!./(.)?!/$1$1!g;s!_ !__!g}

Combined STDIN and ARGV input. Example:

$ perl ~/hex.pl <~/hex.txt 3
         __________________
        /     /     /\     \
       /     /     /  \     \
      /_____/_____/    \_____\
     /     /\     \    /\     \
    /     /  \     \  /  \     \
   /_____/    \_____\/    \_____\
  /\     \    /     /\    /     /\
 /  \     \  /     /  \  /     /  \
/    \_____\/_____/    \/_____/    \
\    /     /\     \    /     /\    /
 \  /     /  \     \  /     /  \  /
  \/_____/    \_____\/_____/    \/
   \     \    /     /\     \    /
    \     \  /     /  \     \  /
     \_____\/_____/    \_____\/
      \     \     \    /     /
       \     \     \  /     /
        \_____\_____\/_____/
\$\endgroup\$
1
\$\begingroup\$

Ruby 236 237

->i,z{i.split(?\n).map{|l|z.times.map{|y|l.size.times.map{|i|z.times.map{|x|c=l[i]
z<y+2&&(l[i-1..i]=='_\\'||l[i..i+1]=='/_')&&o=?_
(c<?!||(x==y&&c==?\\)||(z==y+1&&c>?^)||(x+y+1==z&&c==?/))&&o=c
o||' '}.join}.join.rstrip}-['']}.join ?\n}

Online test: http://ideone.com/e6XakQ

This is the code before golfing:

-> diamond, zoom {
  diamond.split(?\n).map do |l|
    zoom.times.map do |y|
      l.size.times.map do |i|
        zoom.times.map do |x|
          out_char = crt_char = l[i]

          out_char = ' '

          # _ has to be continued under / or \
          if zoom == y+1 && l[i-1..i]=='_\\'
            out_char = ?_
          end
          if zoom == y+1 && l[i..i+1]=='/_'
            out_char = ?_
          end

          # logic to "zoom" \, / and _ characters 
          out_char = crt_char if crt_char == ' '
          out_char = crt_char if x==y && crt_char == ?\\  
          out_char = crt_char if zoom==y+1 && crt_char == ?_
          out_char = crt_char if x+y+1==zoom && crt_char == ?/

          out_char
        end.join
      end.join.rstrip
    end - ['']
  end.join ?\n
}
\$\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.