8
\$\begingroup\$

Your challenge is to find all occurences of a word in the 3d matrix. There is no restriction on I/O format. In the samples below, the word is presented, then a blank line, then the 2-dimensional layers from top to bottom, and the output, for each line, consists of a coordinate and a direction (x, then y, then z, where + is positive direction, - is negative direction, and 0 is no change). However, you may choose any other format, for instance a 3D Array, list of list of lists, pre-existing values on tape/stack, etc. Similarly, you may output the coordinate after the direction, etc. However, the format must be bijectional (always output 1 is not a valid output format) and consistent.

Sample Input

CODEGOLF

N Y R Z O F K G   Y G B V E T P M   O J F K Y O O K   O Z N Q F A R P   M X E T N O I Y   F H C U O F Z A   G V A V O O F B   B V K U O V L F    
W Y L W U H H K   Z M Z X D R K Q   G D D A B I D F   P Y G U I D L I   J Y D O M D Q W   F H B Q Q N B B   T A C F J Q L K   H R Y R Y B Z Q    
L F C D Z B Z W   L E A J O F F J   Z O X Q G A R C   W N N W Y Z S U   S G E V T A C F   K F E O R O N V   K D G Z N W O P   L I W W J L C U    
K L Z Q M A G C   M R Q E F M O I   O K T K T U A U   S E X A Y K C D   N J D V G E S G   X O F P T S F I   H Z B X E X U T   X R Q G V P Q O    
B H F C J P Y A   P I Z G R X N A   A W Z H A Z H V   Q X T E T B Z A   Q A V I Z H G D   E H N J L G G W   V K A O Q U S G   N K M M X R G Z    
B Q K R Y O R I   O J C Q K C P F   F U D R M U J G   E K B F A A C I   K G P O B M N E   M P M B K X X T   V B V N Z O R P   K N Q N J B M D
M L R C O U C F   A O H U H R E P   M L E T B F R Y   W J S U C Y A N   M X S W E C C X   C U F U V Q U H   J C Z W Y E J S   Z D C U I R F Z    
C H D I M M C W   F W G N I I Z U   C X W Q M C O N   Y O W K X E Z J   U G Y U W Q V V   C N B T A T E Z   W C X Z E O W Z   N S C J P V X X    

Sample Output

0 0 7 ++-

Sample Input

AA

A A
A A

Sample Output

0 0 0 +00
0 0 0 0+0
0 0 0 ++0
1 0 0 -00
1 0 0 0+0
1 0 0 -+0
0 1 0 +00
0 1 0 0-0
0 1 0 +-0
1 1 0 -00
1 1 0 0-0
1 1 0 --0

Sample Input

SNY

Y X  N X  S X

Sample Output

0 0 0 00+

Sample Input

SNY

Y  N  S

Sample Output

0 0 0 00+
\$\endgroup\$
5
  • \$\begingroup\$ Inspired by this comment \$\endgroup\$
    – Sny
    Commented Feb 4 at 2:34
  • \$\begingroup\$ Great challenge! Could you elaborate on the direction notation (e.g. ++-)? What does it mean? \$\endgroup\$
    – enzo
    Commented Feb 4 at 3:52
  • \$\begingroup\$ Done@%#&R^*&T1%&*!(!%#&^*&( \$\endgroup\$
    – Sny
    Commented Feb 4 at 3:54
  • \$\begingroup\$ Can we assume the size of the matrix is the same as the length of the word? If not you should probably add another testcase \$\endgroup\$
    – emanresu A
    Commented Feb 4 at 4:43
  • \$\begingroup\$ I think the sample cases are enough, but yah i'll add one \$\endgroup\$
    – Sny
    Commented Feb 4 at 5:18

3 Answers 3

1
\$\begingroup\$

Jelly, 17 bytes

œẹⱮŒp_ƝỊjEƊẠƊƇQƑƇ

A dyadic Link that accepts a multidimensional* array of characters (the Board) on the left, and a list of characters (the Word) on the right and yields a list of lists of 1-indexed coordinates.

* Of any dimension!

Try it online! ("CODEGOLF" will take too long, and "ODE" is there twice, so I used the latter instead.)

How?

œẹⱮŒp_ƝỊjEƊẠƊƇQƑƇ - Main Link: multidimensional array, Board; list, Word
  Ɱ               - map across {C in Word} with:
œẹ                -   get all coordinates of {C} in {Board}
   Œp             - Cartesian product
                     -> all possible coordinate lists spelling {Word}
             Ƈ    - keep if:
            Ɗ     -   last three links as a monad - f(Coordinates):
      Ɲ           -     for neighbouring pairs:
     _            -       subtract
          Ɗ       -     last three links as a monad - f(Deltas=that):
       Ị          -       {Deltas} insignificant? (vectorises) -> is 0 or 1?
         E        -       {Deltas} all equal?
        j         -       {is 0 or 1(Deltas)} join {all Deltas are equal}
           Ạ      -     all?
              QƑƇ - keep if all distinct -> remove any with repeated coordinates
\$\endgroup\$
0
\$\begingroup\$

Charcoal, 88 bytes

UMη⊞O⁺ιψ ⊞η FLηFL§ηιFL§§ηικF³F³F³¿∨⊖μ∨⊖ν⊖ξ¿⬤θ⁼𧧧η⁺ι×ρ⊖μ⁺κ×ρ⊖ν⁺λ×ρ⊖ξ⟦⪫⟦ικλ⭆⟦μνξ⟧§-0+π⟧ 

Try it online! Link is to verbose version of code. Explanation:

UMη⊞O⁺ιψ ⊞η 

Add padding bytes in each dimension. (If I was using the newer version of Charcoal on ATO then I would use ψ consistently but unfortunately the version of Charcoal on TIO has what I would call a bug when indexing an array containing ψ so I use spaces instead.)

FLηFL§ηιFL§§ηικ

Loop through every possible starting position (including the padding, but that will get filtered out later anyway).

F³F³F³¿∨⊖μ∨⊖ν⊖ξ

Loop through every possible direction except no direction (fixes the AA test case).

¿⬤θ⁼𧧧η⁺ι×ρ⊖μ⁺κ×ρ⊖ν⁺λ×ρ⊖ξ

If each character of the array starting at that position and working in that direction equals the target string, then...

⟦⪫⟦ικλ⭆⟦μνξ⟧§-0+π⟧ 

... output the position and direction in a readable form.

\$\endgroup\$
0
\$\begingroup\$

Python3, 345 bytes

from itertools import*
D=[0,1,-1]
E=enumerate
def f(w,b):
 d={}
 for z,Z in E(b):
  for x,X in E(Z):
   for y,v in E(X):d[v]=d.get(v,[])+[(x,y,z)]
 q=[(w[1:],*i,*j,[i])for i in d[w[0]]for j in product(*[D,D,D])if any(j)]
 while q:
  w,x,y,z,X,Y,Z,p=q.pop(0)
  if''==w:yield p;continue
  if(T:=(x+X,y+Y,z+Z))in d[w[0]]:q+=[(w[1:],*T,X,Y,Z,p+[T])]

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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