Track an object in 2d space
Clash Royale CLAN TAG#URR8PPP
up vote
6
down vote
favorite
Description
The task of this challenge is to devise a program or function that tracks a given object in an $nÃÂn$ space.
I/O
Your program will be given 3 inputs, which may be taken in any sensible way:
n
will be the size of the plane's side. (so, for $n=5$, your plane will be $5ÃÂ5$). You may assume n
will always be an odd integer.
s
will be the starting position of the object, given as a pair of $(x, y)$ coordinates.
D
will be a vector of ordered pairs. D
will follow the format $D = [(d_0,t_0),(d_1,t_1),...,(d_n,t_n)]$, where $d_k$ will always be one of 'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'
, for the cardinal and primary intercardinal directions, and $t_k$ will be an integer for the number of 'ticks'.
Given these inputs, your program must output a tracking of the object in the plane.
Rules
The output must contain the plane's boundaries. E.g.:
- 21012 +
+âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
2â âÂÂ
1â âÂÂ
0â âÂÂ
1â âÂÂ
2â âÂÂ
-âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
would be an example of an empty $5ÃÂ5$ plane. The numbers above and to the side are for reference only and don't need to be printed.
You may use whatever character(s) for the boundaries, as long as it's not whitespace (or renders as whitespace). The characters you choose must delineate the full plane, meaning that there can be no gaps between them.
Some acceptable planes include:
âÂÂâÂÂâÂÂâ .... ---- +--+
â â . . | | | |
â â . . | | | |
âÂÂâÂÂâÂÂâÂÂ; ....; ----; +--+
Nonacceptable planes include:
.... .... ++++ . .
. . + + . .
. + + . .
; ....; ....; + +; . .
The object to be tracked may be whatever character you choose, as long as it only occupies 1 space on the plane and is different from the boundary characters.
The trace of the tracked object may also be whatever characters you choose, as long as they only occupy 1 space on the plane and are different from the object.
For each element $(d_k,t_k)$ in $D$, the object must move $t$ spaces towards $d$, and leave a trace behind.
If the object would hit a boundary, it'll be reflected. If the object still has any moves left, it'll keep moving in the direction it was reflected to.
For reference, these directions reflect to each other:
$Nrightleftharpoons S$ â when the top or bottom boundary is met;
$Erightleftharpoons W$ â when a lateral boundary is met;
The final output will contain the newest possible traces, that is, if the object would leave a trace in a space where there's already a trace, the newer trace character will overwrite the older.
As usual, standard loopholes are forbidden by default.
Scoring:
This is a code-golf challenge.
Examples:
Input: $n=5$, $s=(0,0)$, $D=[('NW',2),('S',2),('E',1)]$
Working it out:
$t=0$
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â âÂÂ
â âÂÂ
0â â âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
$t=2$
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂâ âÂÂ
â âÂÂ
0â âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
$t=4$
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂ⧠âÂÂ
âÂÂ| âÂÂ
0âÂÂâ âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
$t=5$, which will be the output.
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂ⧠âÂÂ
âÂÂ| âÂÂ
0âÂÂâÂÂâ âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
(The 0s are just for reference, and they don't need to be in the final output.)
Input: $n=9$, $s=(3,-1)$, $D=[('N',2),('SW',8),('SE',3),('NE',8)]$
Notice that, when $t=10$:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â âÂÂ
â âÂÂ
â âÂÂ
â â§ âÂÂ
0â /| âÂÂ
â â / | âÂÂ
âÂÂ⨠/ âÂÂ
â / âÂÂ
â â¨ âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
The object has been reflected twice: once when reaching the bottom of the plane while going towards the $SW$, where it reflects to the $NW$; then once again when reaching the left side of the plane, where $NW$ reflects to $NE$.
The final output comes at $t=21$:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â â âÂÂ
â âÂÂ
â âÂÂ
â âÂÂ
0â /|â©âÂÂ
â â§ / / âÂÂ
âÂÂ⨠/ / âÂÂ
â / âÂÂ
â â¨ ⨠âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
Test cases:
Input: $n=5$, $s=(0,0)$, $D=[('NW',2),('S',2),('E',1)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂ⧠âÂÂ
âÂÂ| âÂÂ
0âÂÂâÂÂâ âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
Input: $n=9$, $s=(3,-1)$, $D=[('N',2),('SW',8),('SE',3),('NE',8)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â â âÂÂ
â âÂÂ
â âÂÂ
â âÂÂ
0â /|â©âÂÂ
â â§ / / âÂÂ
âÂÂ⨠/ / âÂÂ
â / âÂÂ
â â¨ ⨠âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
Input: $n=3$, $s=(1,1)$, $D=[('N',5),('W',5)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂ
â |âÂÂ
0âÂÂ-âÂÂâÂÂâÂÂ
â |âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂ
Input: $n=11$, $s=(3,-5)$, $D=[('NW',8),('E',5),('SE',3),('SW',5),('N',6),('NE',10)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â â§ âÂÂ
â / âÂÂ
âÂÂâÂÂ---/ âÂÂ
â / âÂÂ
â / â©âÂÂ
0â | / / âÂÂ
â | / â âÂÂ
â | / âÂÂ
â | / âÂÂ
â |/ âÂÂ
â â¨ âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
code-golf ascii-art
 |Â
show 6 more comments
up vote
6
down vote
favorite
Description
The task of this challenge is to devise a program or function that tracks a given object in an $nÃÂn$ space.
I/O
Your program will be given 3 inputs, which may be taken in any sensible way:
n
will be the size of the plane's side. (so, for $n=5$, your plane will be $5ÃÂ5$). You may assume n
will always be an odd integer.
s
will be the starting position of the object, given as a pair of $(x, y)$ coordinates.
D
will be a vector of ordered pairs. D
will follow the format $D = [(d_0,t_0),(d_1,t_1),...,(d_n,t_n)]$, where $d_k$ will always be one of 'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'
, for the cardinal and primary intercardinal directions, and $t_k$ will be an integer for the number of 'ticks'.
Given these inputs, your program must output a tracking of the object in the plane.
Rules
The output must contain the plane's boundaries. E.g.:
- 21012 +
+âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
2â âÂÂ
1â âÂÂ
0â âÂÂ
1â âÂÂ
2â âÂÂ
-âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
would be an example of an empty $5ÃÂ5$ plane. The numbers above and to the side are for reference only and don't need to be printed.
You may use whatever character(s) for the boundaries, as long as it's not whitespace (or renders as whitespace). The characters you choose must delineate the full plane, meaning that there can be no gaps between them.
Some acceptable planes include:
âÂÂâÂÂâÂÂâ .... ---- +--+
â â . . | | | |
â â . . | | | |
âÂÂâÂÂâÂÂâÂÂ; ....; ----; +--+
Nonacceptable planes include:
.... .... ++++ . .
. . + + . .
. + + . .
; ....; ....; + +; . .
The object to be tracked may be whatever character you choose, as long as it only occupies 1 space on the plane and is different from the boundary characters.
The trace of the tracked object may also be whatever characters you choose, as long as they only occupy 1 space on the plane and are different from the object.
For each element $(d_k,t_k)$ in $D$, the object must move $t$ spaces towards $d$, and leave a trace behind.
If the object would hit a boundary, it'll be reflected. If the object still has any moves left, it'll keep moving in the direction it was reflected to.
For reference, these directions reflect to each other:
$Nrightleftharpoons S$ â when the top or bottom boundary is met;
$Erightleftharpoons W$ â when a lateral boundary is met;
The final output will contain the newest possible traces, that is, if the object would leave a trace in a space where there's already a trace, the newer trace character will overwrite the older.
As usual, standard loopholes are forbidden by default.
Scoring:
This is a code-golf challenge.
Examples:
Input: $n=5$, $s=(0,0)$, $D=[('NW',2),('S',2),('E',1)]$
Working it out:
$t=0$
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â âÂÂ
â âÂÂ
0â â âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
$t=2$
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂâ âÂÂ
â âÂÂ
0â âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
$t=4$
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂ⧠âÂÂ
âÂÂ| âÂÂ
0âÂÂâ âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
$t=5$, which will be the output.
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂ⧠âÂÂ
âÂÂ| âÂÂ
0âÂÂâÂÂâ âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
(The 0s are just for reference, and they don't need to be in the final output.)
Input: $n=9$, $s=(3,-1)$, $D=[('N',2),('SW',8),('SE',3),('NE',8)]$
Notice that, when $t=10$:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â âÂÂ
â âÂÂ
â âÂÂ
â â§ âÂÂ
0â /| âÂÂ
â â / | âÂÂ
âÂÂ⨠/ âÂÂ
â / âÂÂ
â â¨ âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
The object has been reflected twice: once when reaching the bottom of the plane while going towards the $SW$, where it reflects to the $NW$; then once again when reaching the left side of the plane, where $NW$ reflects to $NE$.
The final output comes at $t=21$:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â â âÂÂ
â âÂÂ
â âÂÂ
â âÂÂ
0â /|â©âÂÂ
â â§ / / âÂÂ
âÂÂ⨠/ / âÂÂ
â / âÂÂ
â â¨ ⨠âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
Test cases:
Input: $n=5$, $s=(0,0)$, $D=[('NW',2),('S',2),('E',1)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂ⧠âÂÂ
âÂÂ| âÂÂ
0âÂÂâÂÂâ âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
Input: $n=9$, $s=(3,-1)$, $D=[('N',2),('SW',8),('SE',3),('NE',8)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â â âÂÂ
â âÂÂ
â âÂÂ
â âÂÂ
0â /|â©âÂÂ
â â§ / / âÂÂ
âÂÂ⨠/ / âÂÂ
â / âÂÂ
â â¨ ⨠âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
Input: $n=3$, $s=(1,1)$, $D=[('N',5),('W',5)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂ
â |âÂÂ
0âÂÂ-âÂÂâÂÂâÂÂ
â |âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂ
Input: $n=11$, $s=(3,-5)$, $D=[('NW',8),('E',5),('SE',3),('SW',5),('N',6),('NE',10)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â â§ âÂÂ
â / âÂÂ
âÂÂâÂÂ---/ âÂÂ
â / âÂÂ
â / â©âÂÂ
0â | / / âÂÂ
â | / â âÂÂ
â | / âÂÂ
â | / âÂÂ
â |/ âÂÂ
â â¨ âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
code-golf ascii-art
1
@Arnauld I see your point. Since no answers have been posted yet, IâÂÂll edit the question to reflect like your example A rather than B, since it does make more sense in the context of the question.
â J. Sallé
2 hours ago
Forgot to mention that this challenge was sandboxed.
â J. Sallé
2 hours ago
Can we take'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'
as a 0-indexed (or 1-indexed) integer instead? So[('NW',2),('S',2),('E',1)]
becomes[[7,2],[4,2],[2,1]]
for example.
â Kevin Cruijssen
2 hours ago
@KevinCruijssen sure, no problem. Just make sure to point that out in the answer.
â J. Sallé
2 hours ago
1
@Arnauld yes, you're allowed to use a single trace character. I used more than one so it'd be easier to visualize the test cases, but it's not required. Just make sure the trace character is different from the character of the object being traced.
â J. Sallé
2 hours ago
 |Â
show 6 more comments
up vote
6
down vote
favorite
up vote
6
down vote
favorite
Description
The task of this challenge is to devise a program or function that tracks a given object in an $nÃÂn$ space.
I/O
Your program will be given 3 inputs, which may be taken in any sensible way:
n
will be the size of the plane's side. (so, for $n=5$, your plane will be $5ÃÂ5$). You may assume n
will always be an odd integer.
s
will be the starting position of the object, given as a pair of $(x, y)$ coordinates.
D
will be a vector of ordered pairs. D
will follow the format $D = [(d_0,t_0),(d_1,t_1),...,(d_n,t_n)]$, where $d_k$ will always be one of 'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'
, for the cardinal and primary intercardinal directions, and $t_k$ will be an integer for the number of 'ticks'.
Given these inputs, your program must output a tracking of the object in the plane.
Rules
The output must contain the plane's boundaries. E.g.:
- 21012 +
+âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
2â âÂÂ
1â âÂÂ
0â âÂÂ
1â âÂÂ
2â âÂÂ
-âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
would be an example of an empty $5ÃÂ5$ plane. The numbers above and to the side are for reference only and don't need to be printed.
You may use whatever character(s) for the boundaries, as long as it's not whitespace (or renders as whitespace). The characters you choose must delineate the full plane, meaning that there can be no gaps between them.
Some acceptable planes include:
âÂÂâÂÂâÂÂâ .... ---- +--+
â â . . | | | |
â â . . | | | |
âÂÂâÂÂâÂÂâÂÂ; ....; ----; +--+
Nonacceptable planes include:
.... .... ++++ . .
. . + + . .
. + + . .
; ....; ....; + +; . .
The object to be tracked may be whatever character you choose, as long as it only occupies 1 space on the plane and is different from the boundary characters.
The trace of the tracked object may also be whatever characters you choose, as long as they only occupy 1 space on the plane and are different from the object.
For each element $(d_k,t_k)$ in $D$, the object must move $t$ spaces towards $d$, and leave a trace behind.
If the object would hit a boundary, it'll be reflected. If the object still has any moves left, it'll keep moving in the direction it was reflected to.
For reference, these directions reflect to each other:
$Nrightleftharpoons S$ â when the top or bottom boundary is met;
$Erightleftharpoons W$ â when a lateral boundary is met;
The final output will contain the newest possible traces, that is, if the object would leave a trace in a space where there's already a trace, the newer trace character will overwrite the older.
As usual, standard loopholes are forbidden by default.
Scoring:
This is a code-golf challenge.
Examples:
Input: $n=5$, $s=(0,0)$, $D=[('NW',2),('S',2),('E',1)]$
Working it out:
$t=0$
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â âÂÂ
â âÂÂ
0â â âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
$t=2$
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂâ âÂÂ
â âÂÂ
0â âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
$t=4$
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂ⧠âÂÂ
âÂÂ| âÂÂ
0âÂÂâ âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
$t=5$, which will be the output.
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂ⧠âÂÂ
âÂÂ| âÂÂ
0âÂÂâÂÂâ âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
(The 0s are just for reference, and they don't need to be in the final output.)
Input: $n=9$, $s=(3,-1)$, $D=[('N',2),('SW',8),('SE',3),('NE',8)]$
Notice that, when $t=10$:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â âÂÂ
â âÂÂ
â âÂÂ
â â§ âÂÂ
0â /| âÂÂ
â â / | âÂÂ
âÂÂ⨠/ âÂÂ
â / âÂÂ
â â¨ âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
The object has been reflected twice: once when reaching the bottom of the plane while going towards the $SW$, where it reflects to the $NW$; then once again when reaching the left side of the plane, where $NW$ reflects to $NE$.
The final output comes at $t=21$:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â â âÂÂ
â âÂÂ
â âÂÂ
â âÂÂ
0â /|â©âÂÂ
â â§ / / âÂÂ
âÂÂ⨠/ / âÂÂ
â / âÂÂ
â â¨ ⨠âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
Test cases:
Input: $n=5$, $s=(0,0)$, $D=[('NW',2),('S',2),('E',1)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂ⧠âÂÂ
âÂÂ| âÂÂ
0âÂÂâÂÂâ âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
Input: $n=9$, $s=(3,-1)$, $D=[('N',2),('SW',8),('SE',3),('NE',8)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â â âÂÂ
â âÂÂ
â âÂÂ
â âÂÂ
0â /|â©âÂÂ
â â§ / / âÂÂ
âÂÂ⨠/ / âÂÂ
â / âÂÂ
â â¨ ⨠âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
Input: $n=3$, $s=(1,1)$, $D=[('N',5),('W',5)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂ
â |âÂÂ
0âÂÂ-âÂÂâÂÂâÂÂ
â |âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂ
Input: $n=11$, $s=(3,-5)$, $D=[('NW',8),('E',5),('SE',3),('SW',5),('N',6),('NE',10)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â â§ âÂÂ
â / âÂÂ
âÂÂâÂÂ---/ âÂÂ
â / âÂÂ
â / â©âÂÂ
0â | / / âÂÂ
â | / â âÂÂ
â | / âÂÂ
â | / âÂÂ
â |/ âÂÂ
â â¨ âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
code-golf ascii-art
Description
The task of this challenge is to devise a program or function that tracks a given object in an $nÃÂn$ space.
I/O
Your program will be given 3 inputs, which may be taken in any sensible way:
n
will be the size of the plane's side. (so, for $n=5$, your plane will be $5ÃÂ5$). You may assume n
will always be an odd integer.
s
will be the starting position of the object, given as a pair of $(x, y)$ coordinates.
D
will be a vector of ordered pairs. D
will follow the format $D = [(d_0,t_0),(d_1,t_1),...,(d_n,t_n)]$, where $d_k$ will always be one of 'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'
, for the cardinal and primary intercardinal directions, and $t_k$ will be an integer for the number of 'ticks'.
Given these inputs, your program must output a tracking of the object in the plane.
Rules
The output must contain the plane's boundaries. E.g.:
- 21012 +
+âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
2â âÂÂ
1â âÂÂ
0â âÂÂ
1â âÂÂ
2â âÂÂ
-âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
would be an example of an empty $5ÃÂ5$ plane. The numbers above and to the side are for reference only and don't need to be printed.
You may use whatever character(s) for the boundaries, as long as it's not whitespace (or renders as whitespace). The characters you choose must delineate the full plane, meaning that there can be no gaps between them.
Some acceptable planes include:
âÂÂâÂÂâÂÂâ .... ---- +--+
â â . . | | | |
â â . . | | | |
âÂÂâÂÂâÂÂâÂÂ; ....; ----; +--+
Nonacceptable planes include:
.... .... ++++ . .
. . + + . .
. + + . .
; ....; ....; + +; . .
The object to be tracked may be whatever character you choose, as long as it only occupies 1 space on the plane and is different from the boundary characters.
The trace of the tracked object may also be whatever characters you choose, as long as they only occupy 1 space on the plane and are different from the object.
For each element $(d_k,t_k)$ in $D$, the object must move $t$ spaces towards $d$, and leave a trace behind.
If the object would hit a boundary, it'll be reflected. If the object still has any moves left, it'll keep moving in the direction it was reflected to.
For reference, these directions reflect to each other:
$Nrightleftharpoons S$ â when the top or bottom boundary is met;
$Erightleftharpoons W$ â when a lateral boundary is met;
The final output will contain the newest possible traces, that is, if the object would leave a trace in a space where there's already a trace, the newer trace character will overwrite the older.
As usual, standard loopholes are forbidden by default.
Scoring:
This is a code-golf challenge.
Examples:
Input: $n=5$, $s=(0,0)$, $D=[('NW',2),('S',2),('E',1)]$
Working it out:
$t=0$
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â âÂÂ
â âÂÂ
0â â âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
$t=2$
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂâ âÂÂ
â âÂÂ
0â âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
$t=4$
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂ⧠âÂÂ
âÂÂ| âÂÂ
0âÂÂâ âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
$t=5$, which will be the output.
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂ⧠âÂÂ
âÂÂ| âÂÂ
0âÂÂâÂÂâ âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
(The 0s are just for reference, and they don't need to be in the final output.)
Input: $n=9$, $s=(3,-1)$, $D=[('N',2),('SW',8),('SE',3),('NE',8)]$
Notice that, when $t=10$:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â âÂÂ
â âÂÂ
â âÂÂ
â â§ âÂÂ
0â /| âÂÂ
â â / | âÂÂ
âÂÂ⨠/ âÂÂ
â / âÂÂ
â â¨ âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
The object has been reflected twice: once when reaching the bottom of the plane while going towards the $SW$, where it reflects to the $NW$; then once again when reaching the left side of the plane, where $NW$ reflects to $NE$.
The final output comes at $t=21$:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â â âÂÂ
â âÂÂ
â âÂÂ
â âÂÂ
0â /|â©âÂÂ
â â§ / / âÂÂ
âÂÂ⨠/ / âÂÂ
â / âÂÂ
â â¨ ⨠âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
Test cases:
Input: $n=5$, $s=(0,0)$, $D=[('NW',2),('S',2),('E',1)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
âÂÂ⧠âÂÂ
âÂÂ| âÂÂ
0âÂÂâÂÂâ âÂÂ
â âÂÂ
â âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
Input: $n=9$, $s=(3,-1)$, $D=[('N',2),('SW',8),('SE',3),('NE',8)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â â âÂÂ
â âÂÂ
â âÂÂ
â âÂÂ
0â /|â©âÂÂ
â â§ / / âÂÂ
âÂÂ⨠/ / âÂÂ
â / âÂÂ
â â¨ ⨠âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
Input: $n=3$, $s=(1,1)$, $D=[('N',5),('W',5)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂ
â |âÂÂ
0âÂÂ-âÂÂâÂÂâÂÂ
â |âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂ
Input: $n=11$, $s=(3,-5)$, $D=[('NW',8),('E',5),('SE',3),('SW',5),('N',6),('NE',10)]$
Output:
0
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
â â§ âÂÂ
â / âÂÂ
âÂÂâÂÂ---/ âÂÂ
â / âÂÂ
â / â©âÂÂ
0â | / / âÂÂ
â | / â âÂÂ
â | / âÂÂ
â | / âÂÂ
â |/ âÂÂ
â â¨ âÂÂ
âÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂâÂÂ
code-golf ascii-art
code-golf ascii-art
edited 6 mins ago
asked 3 hours ago
J. Sallé
1,528318
1,528318
1
@Arnauld I see your point. Since no answers have been posted yet, IâÂÂll edit the question to reflect like your example A rather than B, since it does make more sense in the context of the question.
â J. Sallé
2 hours ago
Forgot to mention that this challenge was sandboxed.
â J. Sallé
2 hours ago
Can we take'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'
as a 0-indexed (or 1-indexed) integer instead? So[('NW',2),('S',2),('E',1)]
becomes[[7,2],[4,2],[2,1]]
for example.
â Kevin Cruijssen
2 hours ago
@KevinCruijssen sure, no problem. Just make sure to point that out in the answer.
â J. Sallé
2 hours ago
1
@Arnauld yes, you're allowed to use a single trace character. I used more than one so it'd be easier to visualize the test cases, but it's not required. Just make sure the trace character is different from the character of the object being traced.
â J. Sallé
2 hours ago
 |Â
show 6 more comments
1
@Arnauld I see your point. Since no answers have been posted yet, IâÂÂll edit the question to reflect like your example A rather than B, since it does make more sense in the context of the question.
â J. Sallé
2 hours ago
Forgot to mention that this challenge was sandboxed.
â J. Sallé
2 hours ago
Can we take'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'
as a 0-indexed (or 1-indexed) integer instead? So[('NW',2),('S',2),('E',1)]
becomes[[7,2],[4,2],[2,1]]
for example.
â Kevin Cruijssen
2 hours ago
@KevinCruijssen sure, no problem. Just make sure to point that out in the answer.
â J. Sallé
2 hours ago
1
@Arnauld yes, you're allowed to use a single trace character. I used more than one so it'd be easier to visualize the test cases, but it's not required. Just make sure the trace character is different from the character of the object being traced.
â J. Sallé
2 hours ago
1
1
@Arnauld I see your point. Since no answers have been posted yet, IâÂÂll edit the question to reflect like your example A rather than B, since it does make more sense in the context of the question.
â J. Sallé
2 hours ago
@Arnauld I see your point. Since no answers have been posted yet, IâÂÂll edit the question to reflect like your example A rather than B, since it does make more sense in the context of the question.
â J. Sallé
2 hours ago
Forgot to mention that this challenge was sandboxed.
â J. Sallé
2 hours ago
Forgot to mention that this challenge was sandboxed.
â J. Sallé
2 hours ago
Can we take
'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'
as a 0-indexed (or 1-indexed) integer instead? So [('NW',2),('S',2),('E',1)]
becomes [[7,2],[4,2],[2,1]]
for example.â Kevin Cruijssen
2 hours ago
Can we take
'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'
as a 0-indexed (or 1-indexed) integer instead? So [('NW',2),('S',2),('E',1)]
becomes [[7,2],[4,2],[2,1]]
for example.â Kevin Cruijssen
2 hours ago
@KevinCruijssen sure, no problem. Just make sure to point that out in the answer.
â J. Sallé
2 hours ago
@KevinCruijssen sure, no problem. Just make sure to point that out in the answer.
â J. Sallé
2 hours ago
1
1
@Arnauld yes, you're allowed to use a single trace character. I used more than one so it'd be easier to visualize the test cases, but it's not required. Just make sure the trace character is different from the character of the object being traced.
â J. Sallé
2 hours ago
@Arnauld yes, you're allowed to use a single trace character. I used more than one so it'd be easier to visualize the test cases, but it's not required. Just make sure the trace character is different from the character of the object being traced.
â J. Sallé
2 hours ago
 |Â
show 6 more comments
1 Answer
1
active
oldest
votes
up vote
3
down vote
JavaScript (ES6), 228 bytes
Takes input as (n,x,y,[[dir,len],[dir,len],...])
where directions are encoded counterclockwise from $0$ for South to $7$ for South-West.
Outputs a string with 0
for a boundary, 1
for a trace and 3
for the final position.
(n,x,y,a)=>(g=X=>Y>n?'':(Y%n&&X%n&&a.map(([d,l],i)=>(M=H=>(h-X|v-Y||(k|=a[i+!l]?1:3),l--&&M(H=(h+H)%n?H:-H,h+=H,v+=V=(v+V)%n?V:-V)))(~-(D='12221')[d],V=~-D[d+2&7]),h=x+n/2,v=n/2-y,k=' ')&&k)+(X<n?'':`
`)+g(X++<n?X:!++Y))(Y=!++n)
Try it online!
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
JavaScript (ES6), 228 bytes
Takes input as (n,x,y,[[dir,len],[dir,len],...])
where directions are encoded counterclockwise from $0$ for South to $7$ for South-West.
Outputs a string with 0
for a boundary, 1
for a trace and 3
for the final position.
(n,x,y,a)=>(g=X=>Y>n?'':(Y%n&&X%n&&a.map(([d,l],i)=>(M=H=>(h-X|v-Y||(k|=a[i+!l]?1:3),l--&&M(H=(h+H)%n?H:-H,h+=H,v+=V=(v+V)%n?V:-V)))(~-(D='12221')[d],V=~-D[d+2&7]),h=x+n/2,v=n/2-y,k=' ')&&k)+(X<n?'':`
`)+g(X++<n?X:!++Y))(Y=!++n)
Try it online!
add a comment |Â
up vote
3
down vote
JavaScript (ES6), 228 bytes
Takes input as (n,x,y,[[dir,len],[dir,len],...])
where directions are encoded counterclockwise from $0$ for South to $7$ for South-West.
Outputs a string with 0
for a boundary, 1
for a trace and 3
for the final position.
(n,x,y,a)=>(g=X=>Y>n?'':(Y%n&&X%n&&a.map(([d,l],i)=>(M=H=>(h-X|v-Y||(k|=a[i+!l]?1:3),l--&&M(H=(h+H)%n?H:-H,h+=H,v+=V=(v+V)%n?V:-V)))(~-(D='12221')[d],V=~-D[d+2&7]),h=x+n/2,v=n/2-y,k=' ')&&k)+(X<n?'':`
`)+g(X++<n?X:!++Y))(Y=!++n)
Try it online!
add a comment |Â
up vote
3
down vote
up vote
3
down vote
JavaScript (ES6), 228 bytes
Takes input as (n,x,y,[[dir,len],[dir,len],...])
where directions are encoded counterclockwise from $0$ for South to $7$ for South-West.
Outputs a string with 0
for a boundary, 1
for a trace and 3
for the final position.
(n,x,y,a)=>(g=X=>Y>n?'':(Y%n&&X%n&&a.map(([d,l],i)=>(M=H=>(h-X|v-Y||(k|=a[i+!l]?1:3),l--&&M(H=(h+H)%n?H:-H,h+=H,v+=V=(v+V)%n?V:-V)))(~-(D='12221')[d],V=~-D[d+2&7]),h=x+n/2,v=n/2-y,k=' ')&&k)+(X<n?'':`
`)+g(X++<n?X:!++Y))(Y=!++n)
Try it online!
JavaScript (ES6), 228 bytes
Takes input as (n,x,y,[[dir,len],[dir,len],...])
where directions are encoded counterclockwise from $0$ for South to $7$ for South-West.
Outputs a string with 0
for a boundary, 1
for a trace and 3
for the final position.
(n,x,y,a)=>(g=X=>Y>n?'':(Y%n&&X%n&&a.map(([d,l],i)=>(M=H=>(h-X|v-Y||(k|=a[i+!l]?1:3),l--&&M(H=(h+H)%n?H:-H,h+=H,v+=V=(v+V)%n?V:-V)))(~-(D='12221')[d],V=~-D[d+2&7]),h=x+n/2,v=n/2-y,k=' ')&&k)+(X<n?'':`
`)+g(X++<n?X:!++Y))(Y=!++n)
Try it online!
edited 4 secs ago
answered 24 mins ago
Arnauld
64.9k581274
64.9k581274
add a comment |Â
add a comment |Â
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
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f172837%2ftrack-an-object-in-2d-space%23new-answer', 'question_page');
);
Post as a guest
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
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
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
1
@Arnauld I see your point. Since no answers have been posted yet, IâÂÂll edit the question to reflect like your example A rather than B, since it does make more sense in the context of the question.
â J. Sallé
2 hours ago
Forgot to mention that this challenge was sandboxed.
â J. Sallé
2 hours ago
Can we take
'N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'
as a 0-indexed (or 1-indexed) integer instead? So[('NW',2),('S',2),('E',1)]
becomes[[7,2],[4,2],[2,1]]
for example.â Kevin Cruijssen
2 hours ago
@KevinCruijssen sure, no problem. Just make sure to point that out in the answer.
â J. Sallé
2 hours ago
1
@Arnauld yes, you're allowed to use a single trace character. I used more than one so it'd be easier to visualize the test cases, but it's not required. Just make sure the trace character is different from the character of the object being traced.
â J. Sallé
2 hours ago