Yarr! A map to the hidden treasure!
Python 2, 249 248 244 239 237 bytes
D={}
m=X=Y=0
for s in input().split(","):d=ord(s[0])%10%7;exec"a,b=X,Y;E=D[Y]=D.get(Y,{});E[X]='<^>v'[d];m=min(m,X);%c+=d-2|1;"%(88+d%2)*int(s[1:])
D[b][a]="X"
for Y in sorted(D):print"".join(D[Y].get(n," ")for n in range(m,max(D[Y])+1))
Input like "E2,N4,E5,S2,W1,S3"
.
NSEW
is mapped to [1, 3, 2, 0]
by d=ord(c)%10%7
. Whether to change y
or x
is then decided by d%2
, and whether to increment or decrement is decided by d-2|1
. The first and third expressions were found by brute force.
Other than that, it's a simple usage of a nested dictionary of the form {y: {x: char}}
.
(Thanks to @joriki for help with mapping)
Javascript (ES6), 260
This was an interesting one...
Thanks @ETHproductions, @edc65, and @vihan for the help!
s=>{z=o=""
m=[]
q=x=y=2e3
s.split`,`.map(v=>z+=v[0].repeat(+v.slice(1)))
for(i=0;d=z[i];q=x<q?x:q)(m[y]=m[y]||[])[x]=z[++i]?d=="N"&&--y?"^":d=="S"&&++y?"v":d=="W"&&--x?"<":++x?">":o:"X"
m.map(a=>a.map((b,j)=>o+=" ".repeat(-p-1+(p=j))+b,p=q-1,o+=`
`))
return o}
This defines an anonymous function, so to call it add f=
to the beginning to give it a name.
To test: console.log(f("E2,N4,E5,S2,W1,S3"))
Explanation:
s=>{ //define function w/ parameter s
z=o="" //z=modified input, o=final output
m=[] //map of characters
q=x=y=2e3 //q=minimum value of x; x+y=coordinates. These start high to leave room to go backwards
s.split`,`.map(v=>z+=v[0].repeat(+v.slice(1))) //change "N3,E4" -> "NNNEEEE", and put in z
for(i=0;d=z[i];q=x<q?x:q) //for each direction d in z, while updating q:
(m[y]=m[y]||[])[x]= //in the right place on the map, put:
z[++i]? //if last character of z, "X"
d=="N"&&--y?"^":
d=="S"&&++y?"v": //otherwise get the right character and change x+y accordingly
d=="W"&&--x?"<":
++x?">":o
:"X"
m.map(a=>a.map((b,j)=>o+=" ".repeat(-p-1+(p=j))+b,p=q-1,o+=`
`)) //dump map to o, with correct padding
return o} //return
Ruby, 213 209 198 186 178
M={};x=0,m=q=0
gets.scan(/.(\d+)/){?1.upto($1){m,y=x
x[d=$&.ord%10%7-2]+=1|($u=M[y]||={})[m]=d
m<q&&q=m}}
$u[m]=2
puts M.sort.map{|_,b|(q..b.max[0]).map{|k|">vX <^"[b[k]||3]}*""}
Pass input via stdin.
This uses a y -> x -> char
dictionary to construct the map, where both x
and y
can be negative. Once the input has been parsed, the global minimum of the x coordinate is extracted. For each row, it then iterates over a range going from the global minimum to the maximum index for the current line, and prints the correct character for that index.
To stay with the theme, the expressions to turn NESW
into the proper indices were shamelessly pirated from Sp3000's answer.
Original version that used a [x,y] -> char
dictionary:
M={};x=0,0
gets.scan(/.(\d+)/){(?1..$1).map{x[d=$&.ord%10%7-2]+=1|M[$y=x+[]]=d}}
M[$y]=2
a,*q=M.minmax.flatten
M.map{|(x,y),v|($*[y-M.map{|a,|a[1]}.min]||=?\s.*q[2]-a)[x-a]=">vX<^"[v]}
puts$*.map &:rstrip