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:
\$N\rightleftharpoons S\$ → when the top or bottom boundary is met;
\$E\rightleftharpoons 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│ | / ⟩│ │ |\ / / │ │ | / ○ │ │ |/ \ │ │ ∨ \ │ │ \ │ └───────────┘
'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. \$\endgroup\$