| Bytes | Lang | Time | Link |
|---|---|---|---|
| 017 | Uiua | 250704T005814Z | ErikDaPa |
| 010 | Japt | 250703T215257Z | Shaggy |
| 013 | Vyxal 3 | 250703T204322Z | pacman25 |
| 251 | Python 3.5 using Numpy | 160516T110143Z | R. Kap |
| 108 | JavaScript ES6 | 160305T213727Z | edc65 |
Uiua, 17 bytes
⊡⊓◴⇌\+˜⊂↯⊣⊃⍆±⤚-∩⇌
Explanation:
⊡⊓◴⇌\+˜⊂↯⊣⊃⍆±⤚-∩⇌
∩⇌ => reverse both coordinates (due to Uiua's coordinate system)
⊃⍆±⤚- => get both direction vector and distance
↯⊣ => direction vector repeated by amount of the distance
\+˜⊂ => combine with the 1st coordinate, then do accumulative sum
⇌ => arrange grid for proper indexing
⊓◴ => remove duplicate coordinates
⊡ => finally pick the coordinates from grid
Japt, 14 10 bytes
Takes the end coordinates as the first input, start coordinates as the second, and the grid, as a 2D array of characters, as the third. Outputs a character array.
íõV ÕËrgWz
íõV ÕËrgWz :Implicit input of arrays U=end & V=start, and 2D-array W=wordsearch
í V :Interleave U & V
õ : Reduce each pair by reversed inclusive range (i.e., XõY=[Y,X])
Õ :Transpose
Ë :Map
r : Reduce by
g : Indexing
Wz : With a starting value of W rotated 90° clockwise
Vyxal 3, 13 bytes
-∥±GY⁰pσV⎇⇄Þi
-1 because dip
-∥±GY⁰pσ$⇄T$Þi
- # difference between two coords
∥±G # the direction vector of the difference and the maximum distance
Y # direction vector repeated that many times
⁰pσ # prepend the initial coordinate and cumulative sum for coords of all letters
$⇄T$ # arrange the grid so indexing works properly
Þi # get letter at coordinate
💎
Created with the help of Luminespire.
Python 3.5 using Numpy, 251 bytes:
def r(t,y,z):import numpy;l=numpy.array([[*i.split()]for i in z.split('\n')]);A,B,C,D=t[0],y[0],t[1],y[1];p=[1,-1];a=p[A>B];b=p[C>D];n=range(A,B+a,a);m=range(C,D+b,b);w=[l[:,i][::-1][p]for i,p in zip([[A]*len(m),n][A!=B],[[C]*len(n),m][C!=D])];return w
Takes input in the following format:
print(''.join(r((start1,start2),(end1,end2),'''grid''')))Outputs in the format of a string (e.g.
APPLE) as long as the function is called using the format above. Otherwise, a list containing each letter (e.g.['A','P','P','L','E']) is returned.
Will golf more over time where and when I can.
Try It Online! (Ideone) (Here, input is taken such that the grid is surrounded with double quotes ("") and input on one line, with \ns between each line of the grid. Then, the points are provided in a simple tuple form, with the starting on the second line, and the end on the third.)
Ungolfed code along with Explanation
def r(t,y,z):
import numpy
l=numpy.array([[*i.split()]for i in z.split('\n')])
A,B,C,D=t[0],y[0],t[1],y[1]
p=[1,-1]
a=p[A>B]
b=p[C>D]
n=range(A,B+a,a)
m=range(C,D+b,b)
w=[l[:,i][::-1][p]for i,p in zip([[A]*len(m),n][A!=B],[[C]*len(n),m][C!=D])]
return w
For the purposes of this explanation, assume this program was run with the inputs ((0,4),(4,0)) and the first grid of the question. Here, I will go through the 2 main parts of the code:
l=numpy.array([[*i.split()]for i in z.split('\n')])Here,
lis a numpy array containing each line of the input in a separate "list". For instance, the first grid in the question, which is:A G O A T C A T E A T M E N O W W O R D S E A R A K L L K J H G N P L F G H F D A S P L K J H G O P I L F G H J T F A S E J K Lreturns this numpy array:
[['A' 'G' 'O' 'A' 'T' 'C' 'A' 'T'] ['E' 'A' 'T' 'M' 'E' 'N' 'O' 'W'] ['W' 'O' 'R' 'D' 'S' 'E' 'A' 'R'] ['A' 'K' 'L' 'L' 'K' 'J' 'H' 'G'] ['N' 'P' 'L' 'F' 'G' 'H' 'F' 'D'] ['A' 'S' 'P' 'L' 'K' 'J' 'H' 'G'] ['O' 'P' 'I' 'L' 'F' 'G' 'H' 'J'] ['T' 'F' 'A' 'S' 'E' 'J' 'K' 'L']]w=[l[:,i][::-1][p]for i,p in zip([[A]*len(m),n][A!=B],[[C]*len(n),m][C!=D])]This is the main list of the function in which all of the letters corresponding to each point on the grid is found. Here,
icorresponds to either each integer inn, which is a range object containing every number in the rangestart1=>end1+1in increments of+1ifstart1<end1or-1if the opposite is true. However,ionly corresponds to this as long asstart1does not equalend1. Otherwisestart1is returned as many times as the length ofm, wheremis a range object containing each integer in the rangestart2=>end2+1with the same conditions asn, andpcorresponds to every integer inm. That being said, let us now walk through this step by step:l[:,i]basically returns a row vector for each column,i, in the array,l. for instance,l[:,0]would return:['A' 'E' 'W' 'A' 'N' 'A' 'O' 'T']l[:,1]would return:['G' 'A' 'O' 'K' 'P' 'S' 'P' 'F']so on and so forth. You can read more about different ways of indexing in numpy, including this method, here.
Then, after that, the function reverses each array returned, using
l[:,i][::-1], since each array is indexed from left to right, but since the point0,0on the grid in on the lower left hand corner of the grid, reversing each array would return the index values as if they were being looked for from right to left. For instance,l[:,0][::-1]would return:['T' 'O' 'A' 'N' 'A' 'W' 'E' 'A']After this, the function then indexes through that reversed array for the index value corresponding to
p, which is your letter, and then adds that to the list being created. For instance,l[:,0][::-1][4], which corresponds to point(0,4), would returnA.This process keeps on repeating and adding new values to the list until the range objects are exhausted.
After all that, the output, which is list w, is finally returned. In this case, that would be APPLE if called with print(''.join(r((0,4),(4,0),'''The Grid'''))) or ['A','P','P','L','E'] if it is called without ''.join(). Either way, it returns the correct answer, and we are done!
JavaScript (ES6) 108
(x,y,t,u,g)=>eval("for(g=g.split`\n`.reverse(),r=g[y][2*x];x-t|y-u;)r+=g[y+=u<y?-1:u>y][2*(x+=t<x?-1:t>x)]")
Less golfed
(x,y,t,u,g)=>{
g=g.split`\n`.reverse();
for(r = g[y][2*x]; x-t | y-u; )
r+=g[y += u<y ? -1 : u>y][2*( x += t<x ? -1 : t>x)];
return r
}