Iron particles around a magnet
Clash Royale CLAN TAG#URR8PPP
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 |
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
Dec 12 at 15:00
add a comment |
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 Dec 12 at 12:26
Vedant Kandoi
1,073226
1,073226
Can the function output a 7x7 matrix with the characters or we need to print it on the console ?
– digEmAll
Dec 12 at 15:00
add a comment |
Can the function output a 7x7 matrix with the characters or we need to print it on the console ?
– digEmAll
Dec 12 at 15:00
Can the function output a 7x7 matrix with the characters or we need to print it on the console ?
– digEmAll
Dec 12 at 15:00
Can the function output a 7x7 matrix with the characters or we need to print it on the console ?
– digEmAll
Dec 12 at 15:00
add a comment |
7 Answers
7
active
oldest
votes
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
Dec 12 at 14:09
add a comment |
R, 136 119 116 bytes
function(i,j,m=diag(7),R=row(m)-i,a=j-col(m))R>a&-R>a]='
Try it online!
- -3 bytes saved thanks to @Giuseppe
Function that takes (row,col) coordinates of the wire (1-indexed) and returns a 7x7 matrix with the characters.
add a comment |
Canvas, 27 23 22 21 bytes
-7×[+]↶ω⟳n↔┼⁷⁸╵77@↕↔
Try it here!
add a comment |
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 |
Perl 6, 90 bytes
Try it online!
Anonymous code block that takes two numbers and returns a list of lines.
add a comment |
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 |
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',
autoActivateHeartbeat: false,
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
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
Dec 12 at 14:09
add a comment |
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
Dec 12 at 14:09
add a comment |
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 Dec 12 at 12:51
FrownyFrog
2,4471518
2,4471518
Neat use of box drawing once again! Can you provide an axplanation of the entire solution? Thanks!
– Galen Ivanov
Dec 12 at 14:09
add a comment |
Neat use of box drawing once again! Can you provide an axplanation of the entire solution? Thanks!
– Galen Ivanov
Dec 12 at 14:09
Neat use of box drawing once again! Can you provide an axplanation of the entire solution? Thanks!
– Galen Ivanov
Dec 12 at 14:09
Neat use of box drawing once again! Can you provide an axplanation of the entire solution? Thanks!
– Galen Ivanov
Dec 12 at 14:09
add a comment |
R, 136 119 116 bytes
function(i,j,m=diag(7),R=row(m)-i,a=j-col(m))R>a&-R>a]='
Try it online!
- -3 bytes saved thanks to @Giuseppe
Function that takes (row,col) coordinates of the wire (1-indexed) and returns a 7x7 matrix with the characters.
add a comment |
R, 136 119 116 bytes
function(i,j,m=diag(7),R=row(m)-i,a=j-col(m))R>a&-R>a]='
Try it online!
- -3 bytes saved thanks to @Giuseppe
Function that takes (row,col) coordinates of the wire (1-indexed) and returns a 7x7 matrix with the characters.
add a comment |
R, 136 119 116 bytes
function(i,j,m=diag(7),R=row(m)-i,a=j-col(m))R>a&-R>a]='
Try it online!
- -3 bytes saved thanks to @Giuseppe
Function that takes (row,col) coordinates of the wire (1-indexed) and returns a 7x7 matrix with the characters.
R, 136 119 116 bytes
function(i,j,m=diag(7),R=row(m)-i,a=j-col(m))R>a&-R>a]='
Try it online!
- -3 bytes saved thanks to @Giuseppe
Function that takes (row,col) coordinates of the wire (1-indexed) and returns a 7x7 matrix with the characters.
edited Dec 15 at 15:54
answered Dec 12 at 14:18
digEmAll
2,43148
2,43148
add a comment |
add a comment |
Canvas, 27 23 22 21 bytes
-7×[+]↶ω⟳n↔┼⁷⁸╵77@↕↔
Try it here!
add a comment |
Canvas, 27 23 22 21 bytes
-7×[+]↶ω⟳n↔┼⁷⁸╵77@↕↔
Try it here!
add a comment |
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 Dec 12 at 14:27
answered Dec 12 at 14:00
dzaima
14.4k21754
14.4k21754
add a comment |
add a comment |
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 |
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 |
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 Dec 12 at 13:03
answered Dec 12 at 12:51
Arnauld
72.3k689303
72.3k689303
add a comment |
add a comment |
Perl 6, 90 bytes
Try it online!
Anonymous code block that takes two numbers and returns a list of lines.
add a comment |
Perl 6, 90 bytes
Try it online!
Anonymous code block that takes two numbers and returns a list of lines.
add a comment |
Perl 6, 90 bytes
Try it online!
Anonymous code block that takes two numbers and returns a list of lines.
Perl 6, 90 bytes
Try it online!
Anonymous code block that takes two numbers and returns a list of lines.
edited Dec 12 at 13:11
answered Dec 12 at 13:04
Jo King
20.7k246109
20.7k246109
add a comment |
add a comment |
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 |
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 |
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 Dec 12 at 13:44
TFeld
14.1k21240
14.1k21240
add a comment |
add a comment |
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 |
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 |
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 Dec 12 at 21:36
Neil
79.2k744177
79.2k744177
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
Dec 12 at 15:00