Follow the path
JavaScript (ES6), 204 211 210
Edit 1 Bug fix - output '*' for void input
Edit 2 Simpler decoding of direction to x and y diff
Here is my answer to The treasure map, revised to fulfill the specs.
F=m=>(m.replace(/\D(\d+)/g,(d,z)=>{for(;z--;r=[...r],r[x]=m,p[y]=r.join``)for(d<'>'?--x:d<'^'?++x:d<'v'?--y:++y,p=~x?~y?p:[y=0,...p]:p.map(r=>' '+r,x=0),r=p[y]||'';!r[x];)r+=' '},x=y=0,p=[m='*']),p.join`
`)
Less golfed and explained more or less
f=m=>(
x=y=0, // starting position
p=['*'], // output string array (initialized with minimum output)
m.replace( /\D(\d+)/g,
(d,z) => // execute the following for each group direction/length. Length in z, direction in d[0]
{
while( z--) // repeat for the len
{
// check d to change values of x and y
// all the comparison are with > and <, not equal
// so that they work with the whole d, not just d[0]
d<'>'?--x:d<'^'?++x:d<'v'?--y:++y,
// now if x or y are < 0 then p must be adjusted
p = ~x
? ~y
? p // both x and y are >= 0, p is not changed
: [y = 0, ...p] // y < 0, shift p by on adding a 0 element and set y to 0
: p.map(r=> ' ' + r, x = 0); // x < 0, add a space to the left for each row in p and set x to 0
r = p[y] || ''; // get current row in r
for( ; !r[x]; ) // if the current row is empty or too short
r += ' '; // ... add spaces up to position x
// set character in x position
r = [...r], // the shorter way is converting to array ...
r[x] = '*', // setting the element
p[y] = r.join`` // and the back to string using join
}
}),
p.join`\n` // return output array as a newline separated string
}
Test
F=m=>(m.replace(/\D(\d+)/g,(d,z)=>{for(;z--;r=[...r],r[x]='*',p[y]=r.join``)for(d<'>'?--x:d<'^'?++x:d<'v'?--y:++y,p=~x?~y?p:[y=0,...p]:p.map(r=>' '+r,x=0),r=p[y]||'';!r[x];)r+=' '},x=y=0,p=['*']),p.join`
`)
// TEST
console.log = x => O.textContent += x + '\n';
console.log(F('')+'\n')
console.log(F('v6>4^3<7')+'\n')
console.log(F('^2v2>3<3v3>4^5v5>3^5>4v2<4v3>4^3v3>3^5>4v2<4v3>7^5>4v2<4v3>9^3<2^2v2>4^2v2<2v3>8^5>2v4>2^4v5<3>6^5>5<5v2>5<5v2>5<4v1>8^3<1^2v2>1v2>2^3v3>2^2>1^2v2<1v3<3>11^3<2^2v2>4^2v2<2v3>5^5>5<5v2>5<5v2>5<4v1>7^5>4v2<4v3>4^3v3>3^5>4v2<3v1<1v2>3^1>1v1'))
<pre id=O></pre>
MATL, 71 bytes
1thj'.\d+'XX"@Z)XK6L)U:"K1)XK118=K94=-K62=K60=-hv]]YstY)X<1--lqg10*32+c
Uses current release (6.0.0) of the language/compiler. Works in Matlab and in Octave.
EDIT (June 21, 2016): due to changes in the language, the code requires a few modifications to run in current release (16.0.0). You can try it online including the needed modifications.
Examples
>> matl
> 1thj'.\d+'XX"@Z)XK6L)U:"K1)XK118=K94=-K62=K60=-hv]]YstY)X<1--lqg10*32+c
>
> ^4>3v2<1
****
* *
* **
*
*
>> matl
> 1thj'.\d+'XX"@Z)XK6L)U:"K1)XK118=K94=-K62=K60=-hv]]YstY)X<1--lqg10*32+c
>
> ^2v2>3<3v3>4^5v5>3^5>4v2<4v3>4^3v3>3^5>4v2<4v3>7^5>4v2<4v3>9^3<2^2v2>4^2v2<2v3>8^5>2v4>2^4v5<3>6^5>5<5v2>5<5v2>5<4v1>8^3<1^2v2>1v2>2^3v3>2^2>1^2v2<1v3<3>11^3<2^2v2>4^2v2<2v3>5^5>5<5v2>5<5v2>5<4v1>7^5>4v2<4v3>4^3v3>3^5>4v2<3v1<1v2>3^1>1v1
* * ***** ***** ***** * * *** * ****** * * * * ****** ***** *****
* * * * * * * * * * * * * * * * * * * * * * * *
***** ***** ***** ***** ***** * * * ****** ** * ** ***** ****** ***** *****
* * * * * * * * * * * * * * * * * * **
* * * * * * * * *** ****** ***** * ****** * * * **
*******************************************************************************************
Explanation
The program has four main steps:
- Read input string and split into its components.
- Build a 2-column matrix where each row describes a unit displacement in the appropriate direction. For example,
[0 -1]
indicates one step to the left. The first row is the origin of the path,[1 1]
- Compute the cumulative sum of that matrix along the first dimension . Now each row describes the coordinates of a
*
. Normalize to minumum value1
- Create a new matrix that contains
1
at the coordinates indicated by the matrix from step 3, and0
otherwise. This is then transformed into a char matrix.
Code:
1th % row vector [1 1]. Initiallize matrix of step 2
j % (step 1) read input string
'.\d+'XX % split into components. Creates cell array of substrings
" % (step 2) for each component
@Z)XK % unbox to obtain substring and copy
6L)U: % obtain number and build vector of that size
" % repeat as many times as that number
K1) % paste substring. Get first character: '^', 'v', '>', '<'
XK118=K94=- % vertical component of unit displacement: -1, 0 or 1
K62=K60=- % horizontal component of unit displacement: -1, 0 or 1
h % concatenate horizontally
v % append vertically to existing matrix
] % end
] % end
Ys % (step 3) cumulative sum along first dimension
tY)X<1-- % normalize to minimum value 1
lqg % (step 4) build matrix with 0/1
10*32+c % replace 0 by space and 1 by asterisk