50
8
Introduction
"Yarr!! We had a laddie who called himself a "programmer" make a map t' our hidden treasure! But 'tis written wit' weird numbers 'n letters! "E5, N2, E3"... what does it even mean? Madness! Can't even scribe a proper treasure map, t' useless cretin. Fix it fer us! We'll gift ye a share o' t' treasure!"
Challenge Description
A group of pirates are having trouble reading a treasure map. Can you write a program to convert it into a more... piratey form?
As input, you'll receive the original treasure map. It's a list of comma separated strings, each string which consists of a letter portion (which tells the pirates which direction they need to walk in), and a number portion (which tells the pirates how many steps to take in that direction). For instance, the following treasure map:
E2,N4,E5,S2,W1,S3
would mean, "walk two steps to the east, walk four steps to the north, walk five steps to the east, walk two steps to the south, walk one step to the west, then walk three steps to the south."
As output, you'll output the map in a graphical form, using the characters >
, ^
, v
, and <
as pointers. Here's the output for the above input:
>>>>>v
^ v
^ v<
^ v
>>^ X
Notice that we've replaced the last step to the south with an X
instead. This is because the last step is where the treasure is, and as we all know, pirates must have an X on their treasure maps, otherwise they won't know how to read it.
By the way, the map won't ever cross itself, so you don't need to worry about dealing with overlaps. Also, you are allowed to have a trailing new line at the end of the output.
Sample Inputs and Outputs
S5,W2
v
v
v
v
v
X<
N1,E1,S1,E1,N1,E1,S2
>v>v
^>^X
N1
X
N6,E6,S6,W5,N5,E4,S4,W3,N3,E2,S2,W1,N2
>>>>>>v
^>>>>vv
^^>>vvv
^^^Xvvv
^^^^<vv
^^^<<<v
^^<<<<<
E21,S2
>>>>>>>>>>>>>>>>>>>>>v
X
N12,E11,S12,W2,N4
>>>>>>>>>>>v
^ v
^ v
^ v
^ v
^ v
^ v
^ v
^ v
^ X v
^ ^ v
^ ^ v
^ ^<<
1Are we allowed to have trailing spaces on each line? Is the number always going to be less than ten? – Downgoat – 2015-08-06T03:59:00.113
@vihan You are not allowed to have trailing spaces on each line. The number is not always going to be less than ten. – absinthe – 2015-08-06T04:15:12.513
@Sp3000 The number of steps can be more than one digit. I added a new test case for more than one digit steps. – absinthe – 2015-08-06T05:57:23.470
@Sp3000 Leading spaces before lines are allowed (for some of the inputs, some of the lines need to be "padded" with spaces). Leading spaces after lines are not allowed. Leading spaces after or before the whole grid are allowed. – absinthe – 2015-08-06T06:19:29.550
9I really think the
X
should mark the step after the last move, like all other moves are counted. Imagine the last step is N3: you walk three steps to the north and dig, but there is nothing here, you had to walk 2 steps instead. I don't mind if you keep the existing rule, because it adds a little corner case to handle. But remember what happened to that laddie. – coredump – 2015-08-06T07:39:08.1476@coredump Or maybe we want to mislead the pirates, so that we can take the treasure for ourselves ;)
No, you're correct, the pirates are digging one step too soon. Given there are already three answers, I'll keep the rule as it is to avoid invalidating existing solutions. – absinthe – 2015-08-06T07:58:39.463
Is there any limit to how long a certain direction will be? – The_Basset_Hound – 2015-08-06T18:32:32.990
@BassetHound There's no limit, but you don't need to handle integer overflow or anything like that. – absinthe – 2015-08-07T01:53:11.053
What kind of pirates can't follow a compass? I don't think I'll be sailing with them anytime soon. – jpmc26 – 2015-08-07T08:33:57.583
4@jpmc26 Well, these pirates don't know much of the alphabet... they spent the last few years at C :) – absinthe – 2015-08-07T10:39:38.730
Can we store the output in a global variable after the function is called? (to avoid having to return) Sorry if there is a standard rule for that or something, I'm new at this... – jrich – 2015-08-07T23:24:38.267
4The fourth example is just trolling the pirates... – justhalf – 2015-08-08T03:12:20.490
1@UndefinedFunction No, you need to return a value. – absinthe – 2015-08-08T09:27:07.307