Word Search Helper

12

I've been doing word searches recently, and I thought it would be so much easier if all of the words read left-to-right. But rewriting all the lines takes a lot of effort! So I'm enlisting code golfers to help.

(Disclaimer: The above story may or may not be remotely accurate.)

Your code will take a rectangular grid and output all of the lines through it in both directions.

The output must contain all 8 rotations of the grid (cardinals and main diagonals), 'read off' top to bottom, left to right. (This means that every "row" will be duplicated - once forwards, and once backwards.)

The line divisions can either be a space or a line break. If you choose spaces, the grid rotation divisions must be line breaks; otherwise, the grid rotation divisions must be two line breaks.

Example input (taken as an array of characters, multiline string, or other reasonable format)

ABCDE
FGHIJ
KLMNO
PQRST

Example output (using the first option for divisions)

ABCDE FGHIJ KLMNO PQRST
E DJ CIO BHNT AGMS FLR KQ P
EJOT DINS CHMR BGLQ AFKP
T OS JNR EIMQ DHLP CGK BF A
TSRQP ONMLK JIHGF EDBCA
P QK RLF SMGA TNHB OIC JD E
PKFA QLGB RMHC SNID TOJE
A FB KGC PLHD QMIE RNJ SO T

The order of the rotations "read off" does not matter as long as all eight cardinals and primary intercardinals are done once.

This is , so shortest code wins. Standard loopholes apply.

Deusovi

Posted 2016-03-11T05:43:47.303

Reputation: 1 420

Does the grid only contain uppercase letters or can it be the whole printable ASCII? – Denker – 2016-03-11T10:14:35.753

Almost duplicate (without the diagonals) http://codegolf.stackexchange.com/questions/37940/word-search-puzzle

– Digital Trauma – 2016-03-11T16:39:52.353

@DigitalTrauma: No, not really - this one doesn't ask you to find any words at all. – Deusovi – 2016-03-11T16:50:01.213

Answers

4

Python 3, 181 bytes

def f(s):
 for k in [1,0]*4:
  b=list(zip(*[([' ']*(len(s)-1-n)*k+list(i)+[' ']*n*k)[::-1] for n,i in enumerate(s)]))
  print([' '.join(i).replace(' ','') for i in b])
  if k==0:s=b

Explanation

def f(s):
 for k in [0]*4:                  # loop 4 times, we don't need the index so [0]*4 is shorter than range(4)
  l=len(s)-1                      # number of line

  # rotation of 45°
  a=[(['.']*(l-n)+list(i)+['.']*n)[::-1] for n,i in enumerate(s)]
  # tranform matrice :
  #  ABC      ..ABC      CBA..
  #  DEF  --> .DEF.  --> .FED.
  #  GHI      GHI..      ..IHG
  b=list(zip(*a))                 # transpose 
  #  CBA..      C..
  #  .FED.  --> BF.
  #  ..IHG      AEI
  #             .DH
  #             ..G
  print(' '.join(''.join(i).replace('.','') for i in b))

  # rotation of 90°
  a=[(list(i))[::-1] for n,i in enumerate(s)]
  # tranform matrice :
  #  ABC      CBA
  #  DEF  --> FED
  #  GHI      IHG
  b=list(zip(*a))                 # transpose 
  #  CBA       CFI
  #  FED   --> BEH
  #  IHG       ADG
  print(' '.join(''.join(i) for i in b))
  s=b

Results

>>> f(['ABCDE','FGHIJ','KLMNO','PQRST'])
['E', 'DJ', 'CIO', 'BHNT', 'AGMS', 'FLR', 'KQ', 'P']
['EJOT', 'DINS', 'CHMR', 'BGLQ', 'AFKP']
['T', 'OS', 'JNR', 'EIMQ', 'DHLP', 'CGK', 'BF', 'A']
['TSRQP', 'ONMLK', 'JIHGF', 'EDCBA']
['P', 'QK', 'RLF', 'SMGA', 'TNHB', 'OIC', 'JD', 'E']
['PKFA', 'QLGB', 'RMHC', 'SNID', 'TOJE']
['A', 'FB', 'KGC', 'PLHD', 'QMIE', 'RNJ', 'SO', 'T']
['ABCDE', 'FGHIJ', 'KLMNO', 'PQRST']

>>> f(['ABCDEF','GHIJKL','MNOPQR','STUVWX'])
['F', 'EL', 'DKR', 'CJQX', 'BIPW', 'AHOV', 'GNU', 'MT', 'S']
['FLRX', 'EKQW', 'DJPV', 'CIOU', 'BHNT', 'AGMS']
['X', 'RW', 'LQV', 'FKPU', 'EJOT', 'DINS', 'CHM', 'BG', 'A']
['XWVUTS', 'RQPONM', 'LKJIHG', 'FEDCBA']
['S', 'TM', 'UNG', 'VOHA', 'WPIB', 'XQJC', 'RKD', 'LE', 'F']
['SMGA', 'TNHB', 'UOIC', 'VPJD', 'WQKE', 'XRLF']
['A', 'GB', 'MHC', 'SNID', 'TOJE', 'UPKF', 'VQL', 'WR', 'X']
['ABCDEF', 'GHIJKL', 'MNOPQR', 'STUVWX']

with a cleaner output (189 bytes)

j=' '.join
def f(s):
 for k in [1,0]*4:
  b=list(zip(*[([' ']*(len(s)-1-n)*k+list(i)+[' ']*n*k)[::-1] for n,i in enumerate(s)]))
  print(j(j(i).replace(' ','') for i in b))
  if k==0:s=b

.

>>> f(['ABCDE','FGHIJ','KLMNO','PQRST'])
E DJ CIO BHNT AGMS FLR KQ P
EJOT DINS CHMR BGLQ AFKP
T OS JNR EIMQ DHLP CGK BF A
TSRQP ONMLK JIHGF EDCBA
P QK RLF SMGA TNHB OIC JD E
PKFA QLGB RMHC SNID TOJE
A FB KGC PLHD QMIE RNJ SO T
ABCDE FGHIJ KLMNO PQRST

Erwan

Posted 2016-03-11T05:43:47.303

Reputation: 691

How do you tranforme a matrix? (code block 2) – CalculatorFeline – 2016-03-11T14:35:51.720

@CatsAreFluffy I added one more step in explanations – Erwan – 2016-03-11T15:27:17.180

I'm pretty sure tranforme isn't a word. (transform?) – CalculatorFeline – 2016-03-11T15:34:24.600

@CatsAreFluffy ^^ ok, i think that was obvious that English is not my native language – Erwan – 2016-03-11T15:44:30.337

Very nice answer! – Timtech – 2016-03-11T15:52:58.950

1

MATL, 40 bytes

t!tP!tP!GXKZyqXI"IZ}w_w2$:"K@2$Xd!]K!XKx

Input is a 2D char array in Matlab notation:

['ABCDE'; 'FGHIJ'; 'KLMNO'; 'PQRST']

The output contains each "word" on a separate line.

Try it online!

t          % input 2D char array. Duplicate. The original copy will produce
           % the words left to right when displayed
!          % transpose. This will produce the words up to down
tP!        % duplicate, flip upside down, transpose. This will produce the
           % words right to left
tP!        % Same. This will produce the words down to up 
GXK        % push input again. Copy to clipboard K
Zy         % get size (length-2 vector; [4 5] in the example)
q          % decrement by 1 (gives  [3 4] in the example)
XI         % copy to clipboard I
"          % loop: do this twice, consuming the decremented length-2 vector
  I        %   push decremented size vector again
  Z}       %   split into its two elements (pushes 3 and 4 in the example)
  w_w      %   swap, negave, swap (results in -3, 4 in the example)
  2$:      %   binary range: indices of diagonals  ([-3 -2 -1 0 1 2 3 4]
           %   in the first iteration in the example, [-4 -3 -2 -1 0 1 2 3]
           %   in the second)
  "        %   for each
    K      %     push input (first iteration) or its tranposed version (second)
    @      %     push index of diagonal
    2$Xd!  %     extract that diagonal as a row vector
  ]        %   end for each
  K!XKx    %   update clipboard K with transposed version for next iteration
           % end loop. Display

Luis Mendo

Posted 2016-03-11T05:43:47.303

Reputation: 87 464