Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Guide the Alphabet

Given an array of directions, where the directions are defined as follows:

NW  N  NE
W   .   E
SW  S  SE

Or as indexes (you may use these indices for directions instead of the strings above):

0 1 2
3 . 4
5 6 7

You may choose either format, supporting both is not required by the challenge. Generate the corresponding alphabet String that represents the drawn path, starting with A. The number of directions will never exceed 25, meaning that it can never go past Z, so you don't have to handle this scenario. No other input formats will be accepted unless you can explain why these formats do not work due to a language limitation. This is easier to explain using a verbosely worked out example.


So, lets look at a quick example:

[E,SE,N,S,S,S,NW,W,N] or [4,7,1,6,6,6,0,3,1]

Always start with A.

A

Go East to B.

A-B

Go South East to C.

A-B 
   \
    C

Go North to D.

A-B D
   \|
    C

Go back South to E, overwriting C.

A-B D
   \|
    E

Continue South for 2 cycles to both F and G.

A-B D
   \|
    E
    |
    F
    |
    G

Go North West to H.

A-B D
   \|
    E
    |
  H F
   \|
    G

Go West to I.

A-B D
   \|
    E
    |
I-H F
   \|
    G

End to the North at point J.

A-B D
   \|
J   E
|   |
I-H F
   \|
    G

The final value you would return is by reading the final graph left to right, top to bottom:

ABD
JE
IHF
G

Resulting in:

ABDJEIHFG

This is , lowest byte-count wins.

share|improve this question
1  
Related – FlipTack 13 hours ago
    
@KodosJohnson inputs will not go past 25 directions. – carusocomputing 13 hours ago
1  
Can we take as input 7 distinct pair of values instead of your proposed values. for example instead of 0 we get [-1 -1] or for 1 we get [-1 0]? – rahnema1 13 hours ago
    
@rahnema1 no, the input is as described. – carusocomputing 12 hours ago
    
Does our program need to support both styles of input? – Alex Howansky 12 hours ago

JavaScript (ES6), 108 107 bytes

let f =

a=>[4,...a].map(d=>r[p+=[-99,-98,-97,-1,1,97,98,99][d]]=String.fromCharCode(i++),r=[],i=65,p=i*i)&&r.join``

console.log(f([4,7,1,6,6,6,0,3,1]));

share|improve this answer

Octave, 145 138 131 123 105 103 90 87 85 bytes

@(a){[~,k]=unique(cumsum([1 fix((z=a+(a>3))/3);1 mod(z,3)]'-1),'rows');[k'+64 '']}{2}

Try It Online

Thanks to Suever 2 bytes saved!

Previous answer 103 bytes:

@(a)[nonzeros(accumarray(cumsum([1 fix((z=a+(a>3))/3);1 mod(z,3)]'-1)+30,65:nnz(++a)+65,[],@max)')' '']

Try It Online!

First try 145 byts

@(a){[x y]=find(~impad(1,1,1));c=cumsum([0 0;([y x]-2)(++a,:)]);c=c-min(c)+1;n=nnz(a);[nonzeros(sparse(c(:,1),c(:,2),65:n+65,'unique')')' '']}{5}

Some Explanations

@(a){
    [x y]=find([1 0 1]|[1;0;1]);                            %generate 2d coordinates corresponding to 1d input indices
    XY = [y x]-2;
    c=cumsum([0 0;XY(++a,:)]);                              %cumulative sum of coordinates to find position of characters
    c=c-min(c)+1;n=nnz(a);
    [nonzeros(sparse(c(:,1),c(:,2),65:n+65,'unique')')' ''] %using sparse matrix to place characters at specified positions
    }{5}
share|improve this answer
1  
I believe that since you need the image package part of your bytecount has to be loading the image package pkg load image – Suever 10 hours ago
    
Thanks,no need to load if the package is properly installed you can test it in Octave Online – rahnema1 10 hours ago
    
I believe that's only because Octave Online calls pkg load * at the beginning. ideone.com may be a better choice – Suever 10 hours ago
    
package should be installed this way pkg install -auto image-1.0.0.tar.gz so can it load automatically Please see the manual – rahnema1 9 hours ago
    
Ok then maybe it's fine. I was just going off of what I had seen before here. – Suever 9 hours ago

MATL, 64 58 57 50 46 40 37 bytes

nQ:64+cPFF3Y62#fwh2-GQY)&vYsPHH$6#Xu)

Try it online!

Explanation

        % Implicitly grab the input
nQ      % Determine the number of inputs and add 1 (N)
:       % Create an array from [1...N]
64+     % Add 64 (converts this to ASCII codes [65....65+N])
c       % Convert from ASCII codes to actual characters (['A'...Nth_letter])
P       % Reverse the string
FF      % Push the array [0, 0] to the stack
3Y6     % Push the 2D matrix [1 1 1; 1 0 1; 1 1 1] to the stack
2#f     % Get the rows, columns of the 1's in the previous matrix
wh      % Horizontally concatenate these row/column indices
2-      % Subtract two from each of them so that we get values between -1 and 1
GQY)    % Grab the input, add one (1-based indexing) and index into this row/col matrix
&v      % Vertically concatenate the [0 0] with this new matrix
Ys      % Compute the cumulative sum down the columns which yields 2D coordinates for each
        % successive letter
P       % Flip the result (this is to work around a bug in MATL)
HH$6#Xu % Compute the unique rows (sorted) and find the first occurrence of each unique row
)       % Use this to index into the initial character array. Since we flipped
        % the initial array of letters and the input to the unique function we
        % will get the LAST letter to occur at each point
        % Implicitly display the result
share|improve this answer

Python 2, 180 178 bytes

def f(d,a=[[""]*26 for _ in[1]*26],x=0,y=0,j=1):
 a[0][0]="A"
 for i in d:y+=(i>4)-(i<3);x+=(`i`in'247')-(`i`in'035');a[y][x]=chr(j+65);j+=1
 return"".join("".join(i)for i in a)
share|improve this answer

MATLAB, 87 bytes

function a=f(s);i='%&''?AYZ['-64;[~,a]=unique([0 cumsum(i(s+1))],'last');a=char(a'+64);
share|improve this answer
    
'%&''?AYZ['-64 nice trick... actually 66 bytes if you rewrite in octave – rahnema1 1 hour ago

PHP, 125 bytes

function g($d){$r[0]=chr(65);foreach($d as$i=>$v)$r[$c+=[-27,-26,-25,-1,1,25,26,27][$v]]=chr($i+66);ksort($r);echo join($r);}

Run it like this:

g([4,7,1,6,6,6,0,3,1]);

If strings are OK as input, then this is 121 bytes and will work on the command line:

$r[0]=chr(65);for(;($n=$argv[1][$i])!=null;)$r[$c+=[-27,-26,-25,-1,1,25,26,27][$n]]=chr($i+++66);ksort($r);echo join($r);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.