Draw this diamond pattern
up vote
20
down vote
favorite
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 code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf ascii-art
add a comment |
up vote
20
down vote
favorite
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 code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf ascii-art
Loosely related
– Luis Mendo
Nov 9 at 22:57
add a comment |
up vote
20
down vote
favorite
up vote
20
down vote
favorite
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 code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf ascii-art
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 code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
code-golf ascii-art
code-golf ascii-art
asked Nov 9 at 20:03
AdmBorkBork
26k364226
26k364226
Loosely related
– Luis Mendo
Nov 9 at 22:57
add a comment |
Loosely related
– Luis Mendo
Nov 9 at 22:57
Loosely related
– Luis Mendo
Nov 9 at 22:57
Loosely related
– Luis Mendo
Nov 9 at 22:57
add a comment |
8 Answers
8
active
oldest
votes
up vote
8
down vote
Canvas, 26 25 24 21 18 bytes
4/╬/╬⁰r:⤢n↷⁸{A×├m↷
Try it here!
-3 bytes by fixing m 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
wow O_o canvas is too short
– ASCII-only
Nov 11 at 22:26
add a comment |
up vote
6
down vote
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.
add a comment |
up vote
3
down vote
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!
add a comment |
up vote
3
down vote
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.
add a comment |
up vote
3
down vote
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:
$$((ybmod 10)>3text{ and }d=5)text{ or }((ybmod 10)<6text{ 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:
$$((ybmod 10)>3text{ and }a=8)text{ or }((ybmod 10)<6text{ 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.
This is really cool.
– AdmBorkBork
Nov 12 at 13:27
add a comment |
up vote
2
down vote
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!
add a comment |
up vote
0
down vote
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/33 /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:
- The pattern array mapped to string with length 100 bytes;
- The string reduced by simple
replace.
add a comment |
up vote
0
down vote
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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175619%2fdraw-this-diamond-pattern%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
8
down vote
Canvas, 26 25 24 21 18 bytes
4/╬/╬⁰r:⤢n↷⁸{A×├m↷
Try it here!
-3 bytes by fixing m 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
wow O_o canvas is too short
– ASCII-only
Nov 11 at 22:26
add a comment |
up vote
8
down vote
Canvas, 26 25 24 21 18 bytes
4/╬/╬⁰r:⤢n↷⁸{A×├m↷
Try it here!
-3 bytes by fixing m 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
wow O_o canvas is too short
– ASCII-only
Nov 11 at 22:26
add a comment |
up vote
8
down vote
up vote
8
down vote
Canvas, 26 25 24 21 18 bytes
4/╬/╬⁰r:⤢n↷⁸{A×├m↷
Try it here!
-3 bytes by fixing m 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
Canvas, 26 25 24 21 18 bytes
4/╬/╬⁰r:⤢n↷⁸{A×├m↷
Try it here!
-3 bytes by fixing m 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
edited Nov 11 at 11:33
answered Nov 9 at 20:25
dzaima
14.4k21754
14.4k21754
wow O_o canvas is too short
– ASCII-only
Nov 11 at 22:26
add a comment |
wow O_o canvas is too short
– ASCII-only
Nov 11 at 22:26
wow O_o canvas is too short
– ASCII-only
Nov 11 at 22:26
wow O_o canvas is too short
– ASCII-only
Nov 11 at 22:26
add a comment |
up vote
6
down vote
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.
add a comment |
up vote
6
down vote
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.
add a comment |
up vote
6
down vote
up vote
6
down vote
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.
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.
edited Nov 11 at 10:50
answered Nov 9 at 20:36
Arnauld
71.6k688299
71.6k688299
add a comment |
add a comment |
up vote
3
down vote
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!
add a comment |
up vote
3
down vote
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!
add a comment |
up vote
3
down vote
up vote
3
down vote
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!
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!
edited Nov 10 at 12:54
answered Nov 9 at 21:58
ovs
18.7k21059
18.7k21059
add a comment |
add a comment |
up vote
3
down vote
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.
add a comment |
up vote
3
down vote
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.
add a comment |
up vote
3
down vote
up vote
3
down vote
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.
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.
edited Nov 11 at 10:11
answered Nov 9 at 20:46
Neil
78.9k744175
78.9k744175
add a comment |
add a comment |
up vote
3
down vote
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:
$$((ybmod 10)>3text{ and }d=5)text{ or }((ybmod 10)<6text{ 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:
$$((ybmod 10)>3text{ and }a=8)text{ or }((ybmod 10)<6text{ 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.
This is really cool.
– AdmBorkBork
Nov 12 at 13:27
add a comment |
up vote
3
down vote
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:
$$((ybmod 10)>3text{ and }d=5)text{ or }((ybmod 10)<6text{ 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:
$$((ybmod 10)>3text{ and }a=8)text{ or }((ybmod 10)<6text{ 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.
This is really cool.
– AdmBorkBork
Nov 12 at 13:27
add a comment |
up vote
3
down vote
up vote
3
down vote
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:
$$((ybmod 10)>3text{ and }d=5)text{ or }((ybmod 10)<6text{ 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:
$$((ybmod 10)>3text{ and }a=8)text{ or }((ybmod 10)<6text{ 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.
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:
$$((ybmod 10)>3text{ and }d=5)text{ or }((ybmod 10)<6text{ 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:
$$((ybmod 10)>3text{ and }a=8)text{ or }((ybmod 10)<6text{ 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.
edited Nov 11 at 12:14
answered Nov 10 at 23:10
Arnauld
71.6k688299
71.6k688299
This is really cool.
– AdmBorkBork
Nov 12 at 13:27
add a comment |
This is really cool.
– AdmBorkBork
Nov 12 at 13:27
This is really cool.
– AdmBorkBork
Nov 12 at 13:27
This is really cool.
– AdmBorkBork
Nov 12 at 13:27
add a comment |
up vote
2
down vote
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!
add a comment |
up vote
2
down vote
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!
add a comment |
up vote
2
down vote
up vote
2
down vote
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!
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!
answered Nov 9 at 23:28
Xcali
5,137520
5,137520
add a comment |
add a comment |
up vote
0
down vote
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/33 /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:
- The pattern array mapped to string with length 100 bytes;
- The string reduced by simple
replace.
add a comment |
up vote
0
down vote
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/33 /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:
- The pattern array mapped to string with length 100 bytes;
- The string reduced by simple
replace.
add a comment |
up vote
0
down vote
up vote
0
down vote
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/33 /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:
- The pattern array mapped to string with length 100 bytes;
- The string reduced by simple
replace.
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/33 /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:
- The pattern array mapped to string with length 100 bytes;
- The string reduced by simple
replace.
edited Nov 11 at 22:28
answered Nov 11 at 22:23
mazzy
2,0151314
2,0151314
add a comment |
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Nov 11 at 23:34
Titus
12.9k11237
12.9k11237
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f175619%2fdraw-this-diamond-pattern%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Loosely related
– Luis Mendo
Nov 9 at 22:57