24
\$\begingroup\$

The below pattern will form the basis of this challenge.

     /\
     \/
     /\
    /  \
   /    \
/\/      \/\
\/\      /\/
   \    /
    \  /
     \/
     /\
     \/

Given an input width and height, each >=1, output the above ASCII art pattern repeated that many times, joining (and overlapping) at the small diamonds.

For example, here is an input with width = 2 and height = 1:

     /\        /\
     \/        \/
     /\        /\
    /  \      /  \
   /    \    /    \
/\/      \/\/      \/\
\/\      /\/\      /\/
   \    /    \    /
    \  /      \  /
     \/        \/
     /\        /\
     \/        \/

Here is an input width = 3 and height = 2:

     /\        /\        /\
     \/        \/        \/
     /\        /\        /\
    /  \      /  \      /  \
   /    \    /    \    /    \
/\/      \/\/      \/\/      \/\
\/\      /\/\      /\/\      /\/
   \    /    \    /    \    /
    \  /      \  /      \  /
     \/        \/        \/
     /\        /\        /\
     \/        \/        \/
     /\        /\        /\
    /  \      /  \      /  \
   /    \    /    \    /    \
/\/      \/\/      \/\/      \/\
\/\      /\/\      /\/\      /\/
   \    /    \    /    \    /
    \  /      \  /      \  /
     \/        \/        \/
     /\        /\        /\
     \/        \/        \/

Rules and I/O

  • Input and output can be given by any convenient method.
  • You can print it to STDOUT or return it as a function result.
  • Either a full program or a function are acceptable.
  • Any amount of extraneous whitespace is acceptable, so long as the characters line up appropriately.
  • Standard loopholes are forbidden.
  • This is so all usual golfing rules apply, and the shortest code (in bytes) wins.
\$\endgroup\$

11 Answers 11

10
\$\begingroup\$

Canvas, 26 25 24 21 18 bytes

4/╬/╬⁰r:⤢n↷⁸{A×├m↷

Try it here!

-3 bytes by fixing not repeating canvas

Explanation:

4/╬                 quad-palindromize a 4-long diagonal - big inner diamond
   /╬               quad-palindromize "/" - small diamond
     ⁰r             join the two vertically, centered
       :⤢n          overlap with transpose
           ↷        and rotate the thing clockwise
            ⁸{      for each input
              A×      times 10
                ├     plus 2
                 m    mold the canvas to that width
                  ↷   and rotate clockwise, setting up for the next iteration
\$\endgroup\$
  • \$\begingroup\$ wow O_o canvas is too short \$\endgroup\$ – ASCII-only Nov 11 '18 at 22:26
6
\$\begingroup\$

JavaScript (ES8), 167 161 159 bytes

NB: This is encoding the pattern. See my other answer for a shorter mathematical approach.

Takes input as (width)(height).

w=>h=>(g=h=>h?g(--h)+`
`+([4106,4016,31305,21504,17010]['0102344320'[h%=10]]+'').replace(/./g,c=>'\\/'[c^h>5]||''.padEnd(c-1)).repeat(w+1).slice(8):'')(h*10+2)

Try it online!

How?

We encode the upper half of the pattern with digits:

  • \$0\$ means \
  • \$1\$ means /
  • \$n=2\$ to \$7\$ means \$n-1\$ spaces

This gives:

0  ···/\·····  -->  [3 spaces] [/] [\] [5 spaces]             -->  4106
1  ···\/·····  -->  [3 spaces] [\] [/] [5 spaces]             -->  4016
0  ···/\·····  -->  [3 spaces] [/] [\] [5 spaces]             -->  4106
2  ··/··\····  -->  [2 spaces] [/] [2 spaces] [\] [4 spaces]  -->  31305
3  ·/····\···  -->  [1 space] [/] [4 spaces] [\] [3 spaces]   -->  21504
4  /······\/\  -->  [/] [6 spaces] [\] [/] [\]                -->  17010

For the lower half, we use the rows \$4,3,2,0\$ with / and \ inverted.

\$\endgroup\$
6
\$\begingroup\$

JavaScript (ES6), 139 bytes

This is using quite a different approach from my initial answer, so I'm posting this separately.

Takes input as (width)(height).

w=>h=>(g=x=>y>8?` /\\
`[a=(x+y*9)%10,d=(x+y)%10,x?(y%10>3&&2*(a==8)|d==5)|(y%10<6&&2*(a==6)|d==7):3]+g(x--?x:--y&&w):'')(w=w*10+2,y=-~h*10)

Try it online!

How?

Given the width \$w\$ and the height \$h\$, we draw the output character by character over a grid which is:

  • \$10w+3\$ characters wide
  • \$10h+2\$ characters high

with \$x\$ going from \$10w+2\$ to \$0\$ (left to right) and \$y\$ going from \$10h+10\$ to \$9\$ (top to bottom).

Example for \$w=3\$ and \$h=2\$:

$$\begin{matrix}(32,30)&(31,30)&\dots&(0,30)\\ (32,29)&(31,29)&&(0,29)\\ \vdots&&\ddots&\vdots\\ (32,9)&(31,9)&\dots&(0,9)\end{matrix}$$

The rightmost cells (at \$x=0\$) are simply filled with linefeeds.

For all other cells, we compute:

  • \$a=(x-y)\bmod 10\$ (constant over anti-diagonals)
  • \$d=(x+y)\bmod 10\$ (constant over diagonals)

We draw a "/" if:

$$((y\bmod 10)>3\text{ and }d=5)\text{ or }((y\bmod 10)<6\text{ and }d=7)$$

 y  | y % 10 | output (w = 3, h = 1)
----+--------+----------------------------------
 20 |    0   | ...../........./........./......
 19 |    9   | ....../........./........./.....
 18 |    8   | ...../........./........./......
 17 |    7   | ..../........./........./.......
 16 |    6   | .../........./........./........
 15 |    5   | /./......././......././......./.
 14 |    4   | ./......././......././......././
 13 |    3   | ......../........./........./...
 12 |    2   | ......./........./........./....
 11 |    1   | ....../........./........./.....
 10 |    0   | ...../........./........./......
  9 |    9   | ....../........./........./.....

We draw a "\" if:

$$((y\bmod 10)>3\text{ and }a=8)\text{ or }((y\bmod 10)<6\text{ and }a=6)$$

 y  | y % 10 | output (w = 3, h = 1)
----+--------+----------------------------------
 20 |    0   | ......\.........\.........\.....
 19 |    9   | .....\.........\.........\......
 18 |    8   | ......\.........\.........\.....
 17 |    7   | .......\.........\.........\....
 16 |    6   | ........\.........\.........\...
 15 |    5   | .\.......\.\.......\.\.......\.\
 14 |    4   | \.\.......\.\.......\.\.......\.
 13 |    3   | ...\.........\.........\........
 12 |    2   | ....\.........\.........\.......
 11 |    1   | .....\.........\.........\......
 10 |    0   | ......\.........\.........\.....
  9 |    9   | .....\.........\.........\......

Or we draw a space if none of these conditions is fulfilled.

\$\endgroup\$
  • \$\begingroup\$ This is really cool. \$\endgroup\$ – AdmBorkBork Nov 12 '18 at 13:27
  • \$\begingroup\$ 111 using the C++ answer? \$\endgroup\$ – ASCII-only Jan 6 at 2:47
  • \$\begingroup\$ @Arnauld I knew this was your answer by just looking at the code :D \$\endgroup\$ – flawr Jan 6 at 13:14
6
\$\begingroup\$

C++ (gcc), 137 bytes

#include<cstdio>
auto p(int x,int y){int n=10,t=x=++x*n;for(++y*=n;y>8;)t>7?putchar(t<9?y--,n:t%n-y%n+4&7?t%n+y%n-5&7?32:47:92),t--:t=x;}

Try it online!

Explanation

_______________________________
   098765432109876.... 
   9    \/    .     factor =y%10 - x10
   8    /\    .     if factor = -4 || 4. Print --> '\'  47
   7   /  \   . 
   6  /    \  .     factor =x%10+y%10;  
   5\/      \/*-.   if factor = 5 || 13 --> /  92
   4/\      /\   `.
   3  \    /       `->  * is 9,5 => \
   2   \  /      
   1    \/   
   0    /\       
   9
\$\endgroup\$
  • 1
    \$\begingroup\$ Invalid, not a full program nor a function \$\endgroup\$ – ASCII-only Jan 6 at 1:46
  • 1
    \$\begingroup\$ But, 10/10 a very nice method \$\endgroup\$ – ASCII-only Jan 6 at 2:10
  • 1
    \$\begingroup\$ Which answers exactly aren't full programs or functions? (just asking, may have missed it) Note that some languages (e.g. scripting languages) don't need boilerplate for full programs \$\endgroup\$ – ASCII-only Jan 6 at 6:06
  • 1
    \$\begingroup\$ I think you need to include the #include \$\endgroup\$ – ASCII-only Jan 6 at 6:09
  • 1
    \$\begingroup\$ 159, but not sure if outputting from a function is valid (it probably is) \$\endgroup\$ – ASCII-only Jan 6 at 7:24
4
\$\begingroup\$

Haskell, 179 bytes

k '\\'='/'
k '/'='\\'
k x=x
f x=take(10*x+2)
w#h=map(f w.cycle).f h.drop 9.cycle$(++).reverse=<<map(map k)$["\\/\\      /","   \\    / ","    \\  /  ","     \\/   ","     /\\   "]

Try it online!


Haskell, 181 bytes

k '\\'='/'
k '/'='\\'
k x=x
f x=take(10*x+2)
w#h=map(f w.cycle).f h.drop 9.cycle$(++).reverse=<<map(map k)$map t[49200,36058,31630,30010,29038]
t 0=""
t n="\\ /"!!mod n 3:t(div n 3)

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ Using reverse.map(map k)<>id over (++).reverse=<<map(map k) saves you 3 bytes in both solutions. \$\endgroup\$ – ბიმო Dec 31 '18 at 15:22
  • 1
    \$\begingroup\$ Ah, and in the 2nd one map t can become t<$> and take$10*x+2 saves another byte too and finally you can use cycle"\\ /"!!n over "\\ /"!!mod n 3 - now the second one is shorter :) Try it online! \$\endgroup\$ – ბიმო Dec 31 '18 at 16:00
3
\$\begingroup\$

Charcoal, 24 22 20 bytes

\/↘²‖M↘LF⊖NCχ⁰F⊖NC⁰χ

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

´\/↘²

Draw one eighth of the original pattern.

‖M↘L

Duplicate it three times to complete the original pattern.

F⊖NCχ⁰

Copy the required number of times horizontally.

F⊖NC⁰χ

Copy the required number of times vertically.

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

Powershell, 146 bytes

param($w,$h)0..9*$h+0,1|%{$y=$_
-join(0..9*$w+0,1|%{('3  /\33  \/33  /\33 /  \3  /3 \ /\/33\\/\33/3\3 /3  \  /33 \/3'-replace3,'   ')[$y*10+$_]})}

Explanation

The pattern is 10x10 chars array:

     /\   
     \/   
     /\   
    /  \  
   /    \ 
/\/      \
\/\      /
   \    / 
    \  /  
     \/   

The script:

  • repeats the pattern;
  • appends columns [0,1] to the end of each line;
  • appends lines [0,1] to the end of the output.

Two things for golf:

  1. The pattern array mapped to string with length 100 bytes;
  2. The string reduced by simple replace.
\$\endgroup\$
2
\$\begingroup\$

Perl 5 -p, 148 bytes

$_='3A3'x$_.$/.'3B3
3A3
2/2\\2
1/4\\1
A6A
B6B
1\\4/1
2\\2/2
3B3
3A3
'=~s/^.*$/$&x$_/mger x<>.'3B3'x$_;s|A+|/\\|g;s|B+|\\/|g;s/\d/$"x$&/ge;s|^ |  |gm

Try it online!

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

PHP, 159 bytes

pattern taken from mazzy; translated to 1-2-3, converted to base26 -> decoded by the program

while($y<$argv[2]*10+2)echo str_pad("",$argv[1]*10+2,strtr(base_convert([jng1,jnnb,jng1,jofn,k333,"1h4p5",23814,k94d,k02h,jnnb][$y++%10],26,4),312,"\ /")),"
";

requires PHP 5.5 or later. Run with -nr or try it online.

calculating may be shorter (as it was for Arnauld). I may look into that.

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

Kotlin, 196 135 bytes

Saved 61 bytes thanks to ASCII-only's suggestion to use AZTECCO's C++ algorithm.

{h,w->var r=""
for(l in 9..h*10+10){for(c in 9..w*10+10){r+=when{(l%10+c%10)%8==5->'/'
(l%10-c%10+8)%8==4->'\\'
else->' '}}
r+='\n'}
r}

Try it online!

\$\endgroup\$
  • 1
    \$\begingroup\$ 136, stealing from the new C++ answer \$\endgroup\$ – ASCII-only Jan 6 at 2:25
1
\$\begingroup\$

Python 3, 194 192 187 127 bytes

@ASCII-only's solution:

lambda w,h,n=10:"\n".join("".join([" /","\\"][(j%n-i%n)%8==4][(j%n+i%n)%8==5]for i in range(-1,w*n+1))for j in range(-1,h*n+1))

Try it online!


Original Solution

n="\n"
def f(w,h):a=[r"     /\   "*w,r"     \/   "*w,r"    \  /  "*w,r"   \    / "*w,r"\/\      /"*w+r"\/"];return a[0]+n+n.join(([i.translate({47:92,92:47})for i in a]+a[::-1])*h)+n+a[1]

Try it online!

-2 bytes thanks to @Black Owl Kai showing that the tops and bottoms can be accessed from the generated array instead of in separate variables.

-5 more bytes thanks to @Black Owl Kai using a more creative way to store the diamonds

Generates this portion of each diamond:

     /\
     \/
    \  /
   \    /
\/\      /\/

A /\ added at the end of each row to complete it. Then, the /s and \s are swapped to form the top of each diamond, and the order of the lines is reversed to form the bottom half. Finally, it adds in the very top row of /\s and the very bottom row of \/s to complete the image.

\$\endgroup\$
  • \$\begingroup\$ 192 bytes by not using the variables b and c \$\endgroup\$ – Black Owl Kai Jan 6 at 0:13
  • \$\begingroup\$ 187 bytes by removing two +=/*= assignments and flipping the whole diamond upside-down, making the last string easier to store \$\endgroup\$ – Black Owl Kai Jan 6 at 0:58
  • \$\begingroup\$ 181 \$\endgroup\$ – ASCII-only Jan 6 at 1:23
  • \$\begingroup\$ 147, uses c++ solution \$\endgroup\$ – ASCII-only Jan 6 at 1:55
  • \$\begingroup\$ 127 \$\endgroup\$ – ASCII-only Jan 6 at 1:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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