Iron particles around a magnet
up vote
7
down vote
favorite
There are some iron particles on a board. We pass a current carrying wire through that board and the particles form circles. Show these circles if input given is the position of the wire.
Consider the board to be a grid of size 7x7 (fixed)
No extra white spaces are allowed.
Input can be 0-indexed or 1-indexed. (0-indexed in examples)
Examples
input:3,3 # 'X' is wire here but can be shown by anything other than '/|-'
output:
/-----
|/---|
||/-||
|||X|||
||-/||
|---/|
-----/
input:1,2
output:
|/-|||
||X||||
|-/|||
---/||
-----/|
------/
-------
input:0,0
output:
X||||||
-/|||||
--/||||
---/|||
----/||
-----/|
------/
input:0,3
output:
|||X|||
||-/||
|---/|
-----/
-------
-------
-------
input:3,0
output:
---|||
--||||
-|||||
X||||||
-/|||||
--/||||
---/|||
This is code-golf so shortest code wins.
code-golf ascii-art
add a comment |
up vote
7
down vote
favorite
There are some iron particles on a board. We pass a current carrying wire through that board and the particles form circles. Show these circles if input given is the position of the wire.
Consider the board to be a grid of size 7x7 (fixed)
No extra white spaces are allowed.
Input can be 0-indexed or 1-indexed. (0-indexed in examples)
Examples
input:3,3 # 'X' is wire here but can be shown by anything other than '/|-'
output:
/-----
|/---|
||/-||
|||X|||
||-/||
|---/|
-----/
input:1,2
output:
|/-|||
||X||||
|-/|||
---/||
-----/|
------/
-------
input:0,0
output:
X||||||
-/|||||
--/||||
---/|||
----/||
-----/|
------/
input:0,3
output:
|||X|||
||-/||
|---/|
-----/
-------
-------
-------
input:3,0
output:
---|||
--||||
-|||||
X||||||
-/|||||
--/||||
---/|||
This is code-golf so shortest code wins.
code-golf ascii-art
Can the function output a 7x7 matrix with the characters or we need to print it on the console ?
– digEmAll
12 hours ago
add a comment |
up vote
7
down vote
favorite
up vote
7
down vote
favorite
There are some iron particles on a board. We pass a current carrying wire through that board and the particles form circles. Show these circles if input given is the position of the wire.
Consider the board to be a grid of size 7x7 (fixed)
No extra white spaces are allowed.
Input can be 0-indexed or 1-indexed. (0-indexed in examples)
Examples
input:3,3 # 'X' is wire here but can be shown by anything other than '/|-'
output:
/-----
|/---|
||/-||
|||X|||
||-/||
|---/|
-----/
input:1,2
output:
|/-|||
||X||||
|-/|||
---/||
-----/|
------/
-------
input:0,0
output:
X||||||
-/|||||
--/||||
---/|||
----/||
-----/|
------/
input:0,3
output:
|||X|||
||-/||
|---/|
-----/
-------
-------
-------
input:3,0
output:
---|||
--||||
-|||||
X||||||
-/|||||
--/||||
---/|||
This is code-golf so shortest code wins.
code-golf ascii-art
There are some iron particles on a board. We pass a current carrying wire through that board and the particles form circles. Show these circles if input given is the position of the wire.
Consider the board to be a grid of size 7x7 (fixed)
No extra white spaces are allowed.
Input can be 0-indexed or 1-indexed. (0-indexed in examples)
Examples
input:3,3 # 'X' is wire here but can be shown by anything other than '/|-'
output:
/-----
|/---|
||/-||
|||X|||
||-/||
|---/|
-----/
input:1,2
output:
|/-|||
||X||||
|-/|||
---/||
-----/|
------/
-------
input:0,0
output:
X||||||
-/|||||
--/||||
---/|||
----/||
-----/|
------/
input:0,3
output:
|||X|||
||-/||
|---/|
-----/
-------
-------
-------
input:3,0
output:
---|||
--||||
-|||||
X||||||
-/|||||
--/||||
---/|||
This is code-golf so shortest code wins.
code-golf ascii-art
code-golf ascii-art
asked 15 hours ago
Vedant Kandoi
1,028224
1,028224
Can the function output a 7x7 matrix with the characters or we need to print it on the console ?
– digEmAll
12 hours ago
add a comment |
Can the function output a 7x7 matrix with the characters or we need to print it on the console ?
– digEmAll
12 hours ago
Can the function output a 7x7 matrix with the characters or we need to print it on the console ?
– digEmAll
12 hours ago
Can the function output a 7x7 matrix with the characters or we need to print it on the console ?
– digEmAll
12 hours ago
add a comment |
7 Answers
7
active
oldest
votes
up vote
6
down vote
J, 40 bytes
7 7{.6 6&-|.0":<^:6@8[9!:7@'/_____/|-'
Try it online!
Neat use of box drawing once again! Can you provide an axplanation of the entire solution? Thanks!
– Galen Ivanov
13 hours ago
add a comment |
up vote
5
down vote
R, 136 bytes
function(j,i,m=diag(7),R=row(m),C=col(m),a=i+j-C,b=i-j+C){m='|'
m[R==a]='\'
m[R==b]='/'
m[R<a&R>b|R>a&R<b]='-'
m[i,j]=0
write(m,1,7)}
Try it online!
Function that takes (row,col) coordinates of the wire (1-indexed).
add a comment |
up vote
3
down vote
Canvas, 27 23 22 21 bytes
-7×[+]↶ω⟳n↔┼⁷⁸╵77@↕↔
Try it here!
add a comment |
up vote
1
down vote
JavaScript (ES7), 105 103 bytes
Takes input as (y)(x)
, 0-indexed.
Y=>X=>(g=x=>y<7?`/\-|X
`[h=(X-x)**2,v=(Y-y)**2,x<7?h<v?2:h>v?3:h?x<X^y<Y:4:5]+g(x<7?x+1:!++y):'')(y=0)
Try it online!
add a comment |
up vote
1
down vote
Perl 6, 90 bytes
{<| v - X />[1+([-]($_>>.abs).sign||3+[*]($_).sign)for ^7-$^a X ^7-$^b].rotor(7)>>.join}
Try it online!
Anonymous code block that takes two numbers and returns a list of lines.
add a comment |
up vote
1
down vote
Python 2, 105 bytes
lambda x,y:[''.join(['/-|'[cmp(abs(i-x),abs(j-y))],'X'[i==x]][i-x==y-j]for j in R)for i in R]
R=range(7)
Try it online!
add a comment |
up vote
0
down vote
Charcoal, 21 bytes
GL⁷-↙⁶X‖B↘¬‖J±N±NT⁷¦⁷
Try it online! Takes 0-indexed column and row as input. Explanation:
GL⁷-
Draw a triangle of -
s.
↙⁶
Draw a line along the hypotenuse. (This is the wrong way around but we'll fix that later.)
X
Draw the X
that shows the position of the wire. (Any other ASCII character could be used here of course.) This completes one eighth of the drawing.
‖B↘¬
Reflect three times (¬
= left + down) while transforming characters to almost complete the diagram.
‖
Reflect without transforming to switch the /
s with the s so they now point around the
X
instead of towards it.
J±N±N
Jump to the top left of the desired output.
T⁷¦⁷
Trim the result to the desired size.
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%2f177429%2firon-particles-around-a-magnet%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
J, 40 bytes
7 7{.6 6&-|.0":<^:6@8[9!:7@'/_____/|-'
Try it online!
Neat use of box drawing once again! Can you provide an axplanation of the entire solution? Thanks!
– Galen Ivanov
13 hours ago
add a comment |
up vote
6
down vote
J, 40 bytes
7 7{.6 6&-|.0":<^:6@8[9!:7@'/_____/|-'
Try it online!
Neat use of box drawing once again! Can you provide an axplanation of the entire solution? Thanks!
– Galen Ivanov
13 hours ago
add a comment |
up vote
6
down vote
up vote
6
down vote
J, 40 bytes
7 7{.6 6&-|.0":<^:6@8[9!:7@'/_____/|-'
Try it online!
J, 40 bytes
7 7{.6 6&-|.0":<^:6@8[9!:7@'/_____/|-'
Try it online!
answered 15 hours ago
FrownyFrog
2,4271518
2,4271518
Neat use of box drawing once again! Can you provide an axplanation of the entire solution? Thanks!
– Galen Ivanov
13 hours ago
add a comment |
Neat use of box drawing once again! Can you provide an axplanation of the entire solution? Thanks!
– Galen Ivanov
13 hours ago
Neat use of box drawing once again! Can you provide an axplanation of the entire solution? Thanks!
– Galen Ivanov
13 hours ago
Neat use of box drawing once again! Can you provide an axplanation of the entire solution? Thanks!
– Galen Ivanov
13 hours ago
add a comment |
up vote
5
down vote
R, 136 bytes
function(j,i,m=diag(7),R=row(m),C=col(m),a=i+j-C,b=i-j+C){m='|'
m[R==a]='\'
m[R==b]='/'
m[R<a&R>b|R>a&R<b]='-'
m[i,j]=0
write(m,1,7)}
Try it online!
Function that takes (row,col) coordinates of the wire (1-indexed).
add a comment |
up vote
5
down vote
R, 136 bytes
function(j,i,m=diag(7),R=row(m),C=col(m),a=i+j-C,b=i-j+C){m='|'
m[R==a]='\'
m[R==b]='/'
m[R<a&R>b|R>a&R<b]='-'
m[i,j]=0
write(m,1,7)}
Try it online!
Function that takes (row,col) coordinates of the wire (1-indexed).
add a comment |
up vote
5
down vote
up vote
5
down vote
R, 136 bytes
function(j,i,m=diag(7),R=row(m),C=col(m),a=i+j-C,b=i-j+C){m='|'
m[R==a]='\'
m[R==b]='/'
m[R<a&R>b|R>a&R<b]='-'
m[i,j]=0
write(m,1,7)}
Try it online!
Function that takes (row,col) coordinates of the wire (1-indexed).
R, 136 bytes
function(j,i,m=diag(7),R=row(m),C=col(m),a=i+j-C,b=i-j+C){m='|'
m[R==a]='\'
m[R==b]='/'
m[R<a&R>b|R>a&R<b]='-'
m[i,j]=0
write(m,1,7)}
Try it online!
Function that takes (row,col) coordinates of the wire (1-indexed).
edited 6 hours ago
answered 13 hours ago
digEmAll
2,36148
2,36148
add a comment |
add a comment |
up vote
3
down vote
Canvas, 27 23 22 21 bytes
-7×[+]↶ω⟳n↔┼⁷⁸╵77@↕↔
Try it here!
add a comment |
up vote
3
down vote
Canvas, 27 23 22 21 bytes
-7×[+]↶ω⟳n↔┼⁷⁸╵77@↕↔
Try it here!
add a comment |
up vote
3
down vote
up vote
3
down vote
Canvas, 27 23 22 21 bytes
-7×[+]↶ω⟳n↔┼⁷⁸╵77@↕↔
Try it here!
Canvas, 27 23 22 21 bytes
-7×[+]↶ω⟳n↔┼⁷⁸╵77@↕↔
Try it here!
edited 13 hours ago
answered 13 hours ago
dzaima
14.3k21754
14.3k21754
add a comment |
add a comment |
up vote
1
down vote
JavaScript (ES7), 105 103 bytes
Takes input as (y)(x)
, 0-indexed.
Y=>X=>(g=x=>y<7?`/\-|X
`[h=(X-x)**2,v=(Y-y)**2,x<7?h<v?2:h>v?3:h?x<X^y<Y:4:5]+g(x<7?x+1:!++y):'')(y=0)
Try it online!
add a comment |
up vote
1
down vote
JavaScript (ES7), 105 103 bytes
Takes input as (y)(x)
, 0-indexed.
Y=>X=>(g=x=>y<7?`/\-|X
`[h=(X-x)**2,v=(Y-y)**2,x<7?h<v?2:h>v?3:h?x<X^y<Y:4:5]+g(x<7?x+1:!++y):'')(y=0)
Try it online!
add a comment |
up vote
1
down vote
up vote
1
down vote
JavaScript (ES7), 105 103 bytes
Takes input as (y)(x)
, 0-indexed.
Y=>X=>(g=x=>y<7?`/\-|X
`[h=(X-x)**2,v=(Y-y)**2,x<7?h<v?2:h>v?3:h?x<X^y<Y:4:5]+g(x<7?x+1:!++y):'')(y=0)
Try it online!
JavaScript (ES7), 105 103 bytes
Takes input as (y)(x)
, 0-indexed.
Y=>X=>(g=x=>y<7?`/\-|X
`[h=(X-x)**2,v=(Y-y)**2,x<7?h<v?2:h>v?3:h?x<X^y<Y:4:5]+g(x<7?x+1:!++y):'')(y=0)
Try it online!
edited 14 hours ago
answered 15 hours ago
Arnauld
71.3k688298
71.3k688298
add a comment |
add a comment |
up vote
1
down vote
Perl 6, 90 bytes
{<| v - X />[1+([-]($_>>.abs).sign||3+[*]($_).sign)for ^7-$^a X ^7-$^b].rotor(7)>>.join}
Try it online!
Anonymous code block that takes two numbers and returns a list of lines.
add a comment |
up vote
1
down vote
Perl 6, 90 bytes
{<| v - X />[1+([-]($_>>.abs).sign||3+[*]($_).sign)for ^7-$^a X ^7-$^b].rotor(7)>>.join}
Try it online!
Anonymous code block that takes two numbers and returns a list of lines.
add a comment |
up vote
1
down vote
up vote
1
down vote
Perl 6, 90 bytes
{<| v - X />[1+([-]($_>>.abs).sign||3+[*]($_).sign)for ^7-$^a X ^7-$^b].rotor(7)>>.join}
Try it online!
Anonymous code block that takes two numbers and returns a list of lines.
Perl 6, 90 bytes
{<| v - X />[1+([-]($_>>.abs).sign||3+[*]($_).sign)for ^7-$^a X ^7-$^b].rotor(7)>>.join}
Try it online!
Anonymous code block that takes two numbers and returns a list of lines.
edited 14 hours ago
answered 14 hours ago
Jo King
20.3k245107
20.3k245107
add a comment |
add a comment |
up vote
1
down vote
Python 2, 105 bytes
lambda x,y:[''.join(['/-|'[cmp(abs(i-x),abs(j-y))],'X'[i==x]][i-x==y-j]for j in R)for i in R]
R=range(7)
Try it online!
add a comment |
up vote
1
down vote
Python 2, 105 bytes
lambda x,y:[''.join(['/-|'[cmp(abs(i-x),abs(j-y))],'X'[i==x]][i-x==y-j]for j in R)for i in R]
R=range(7)
Try it online!
add a comment |
up vote
1
down vote
up vote
1
down vote
Python 2, 105 bytes
lambda x,y:[''.join(['/-|'[cmp(abs(i-x),abs(j-y))],'X'[i==x]][i-x==y-j]for j in R)for i in R]
R=range(7)
Try it online!
Python 2, 105 bytes
lambda x,y:[''.join(['/-|'[cmp(abs(i-x),abs(j-y))],'X'[i==x]][i-x==y-j]for j in R)for i in R]
R=range(7)
Try it online!
answered 14 hours ago
TFeld
14k21240
14k21240
add a comment |
add a comment |
up vote
0
down vote
Charcoal, 21 bytes
GL⁷-↙⁶X‖B↘¬‖J±N±NT⁷¦⁷
Try it online! Takes 0-indexed column and row as input. Explanation:
GL⁷-
Draw a triangle of -
s.
↙⁶
Draw a line along the hypotenuse. (This is the wrong way around but we'll fix that later.)
X
Draw the X
that shows the position of the wire. (Any other ASCII character could be used here of course.) This completes one eighth of the drawing.
‖B↘¬
Reflect three times (¬
= left + down) while transforming characters to almost complete the diagram.
‖
Reflect without transforming to switch the /
s with the s so they now point around the
X
instead of towards it.
J±N±N
Jump to the top left of the desired output.
T⁷¦⁷
Trim the result to the desired size.
add a comment |
up vote
0
down vote
Charcoal, 21 bytes
GL⁷-↙⁶X‖B↘¬‖J±N±NT⁷¦⁷
Try it online! Takes 0-indexed column and row as input. Explanation:
GL⁷-
Draw a triangle of -
s.
↙⁶
Draw a line along the hypotenuse. (This is the wrong way around but we'll fix that later.)
X
Draw the X
that shows the position of the wire. (Any other ASCII character could be used here of course.) This completes one eighth of the drawing.
‖B↘¬
Reflect three times (¬
= left + down) while transforming characters to almost complete the diagram.
‖
Reflect without transforming to switch the /
s with the s so they now point around the
X
instead of towards it.
J±N±N
Jump to the top left of the desired output.
T⁷¦⁷
Trim the result to the desired size.
add a comment |
up vote
0
down vote
up vote
0
down vote
Charcoal, 21 bytes
GL⁷-↙⁶X‖B↘¬‖J±N±NT⁷¦⁷
Try it online! Takes 0-indexed column and row as input. Explanation:
GL⁷-
Draw a triangle of -
s.
↙⁶
Draw a line along the hypotenuse. (This is the wrong way around but we'll fix that later.)
X
Draw the X
that shows the position of the wire. (Any other ASCII character could be used here of course.) This completes one eighth of the drawing.
‖B↘¬
Reflect three times (¬
= left + down) while transforming characters to almost complete the diagram.
‖
Reflect without transforming to switch the /
s with the s so they now point around the
X
instead of towards it.
J±N±N
Jump to the top left of the desired output.
T⁷¦⁷
Trim the result to the desired size.
Charcoal, 21 bytes
GL⁷-↙⁶X‖B↘¬‖J±N±NT⁷¦⁷
Try it online! Takes 0-indexed column and row as input. Explanation:
GL⁷-
Draw a triangle of -
s.
↙⁶
Draw a line along the hypotenuse. (This is the wrong way around but we'll fix that later.)
X
Draw the X
that shows the position of the wire. (Any other ASCII character could be used here of course.) This completes one eighth of the drawing.
‖B↘¬
Reflect three times (¬
= left + down) while transforming characters to almost complete the diagram.
‖
Reflect without transforming to switch the /
s with the s so they now point around the
X
instead of towards it.
J±N±N
Jump to the top left of the desired output.
T⁷¦⁷
Trim the result to the desired size.
answered 6 hours ago
Neil
78.8k744175
78.8k744175
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%2f177429%2firon-particles-around-a-magnet%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
Can the function output a 7x7 matrix with the characters or we need to print it on the console ?
– digEmAll
12 hours ago