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 \n
s 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, l
is 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 L
returns 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, i
corresponds to either each integer in n
, which is a range object containing every number in the range start1=>end1+1
in increments of +1
if start1<end1
or -1
if the opposite is true. However, i
only corresponds to this as long as start1
does not equal end1
. Otherwise start1
is returned as many times as the length of m
, where m
is a range object containing each integer in the range start2=>end2+1
with the same conditions as n
, and p
corresponds to every integer in m
. 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 point 0,0
on 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 return A
.
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!
Somewhat related. – Martin Ender – 2016-03-05T13:15:59.547
2You should format the test cases differently. This is way too much repeated text. Just split them into the two grids and go with something like
(1,4),(4,4)
-> "LLLL" – Denker – 2016-03-05T13:23:44.347@DenkerAffe Done :) – reubn – 2016-03-05T13:29:29.510
Can I take the wordsearch as a listoflistofchars? – CalculatorFeline – 2016-03-05T20:15:47.360
Test case 4 is incorrect. The correct output is "WORDSEAR" – CalculatorFeline – 2016-03-05T20:21:42.890
@CatsAreFluffy Cheers! And hmm you can...makes it easier though ;) – reubn – 2016-03-05T20:44:25.520
I suggest to wait to accept an answer. Seeing the there is an accepted answer could discourage other players. – edc65 – 2016-03-06T11:08:43.197
@edc65 Thanks for the tip ;) – reubn – 2016-03-06T19:12:54.597