Solving a wordsearch and delete the found characters

401 views Asked by At

This is the problem:

To solve the puzzle you need to search and then delete from the wordsearch all the OCCURRENCES (if multiple) of the words in the list.

The letters of the diagram that will remain, taken all in their order by rows and by columns, they will form the solution of the game.

Words can appear in the diagram horizontally (from right to left, or from left to right), vertically (downwards or downwards towards the top) and diagonally (from top to bottom or from bottom to top).

Define a function es1 (ftxt), which takes the address of a text file, contains a word diagram of a crucipuzzle and returns the string solution of the game.

The fname file contains the wordsearch following the word list. A series of 1 or more blank lines preceding the diagram separates the diagram from the list of words and follows the word list. The diagram is recorded by lines (one line per line and consecutive lines) tab of each row are separated by a single character ('\ t'). The list of consecutive busy words, one word for each line.

O   T   N   E   G   R   A   S   A   E
R   N   N   C   O   R   A   L   L   O
O   A   I   B   L   U   E   E   V   G
U   T   O   R   E   N   T   I   I   A
V   I   O   L   E   T   T   O   O   R
O   C   R   A   R   I   A   E   L   O
D   A   B   I   M   A   L   V   A   P
I   P   C   I   E   L   O   G   L   R
C   O   R   P   O   S   O   U   A   O
A   P   I   E   N   O   M   I   L   P


ACIDO
ARGENTO
BLU
CIELO
CORALLO
CORPOSO
ELETTRICO
LATTE
LIMONE
MALVA
NERO
OCRA
OPACITA
ORO
PAGLIERINO
PIENO
PORPORA
PRIMITIVO
VIOLA
VIOLETTO

I have found all the rows, columns and 50% of the diagonals, but I don't know how to find the coordinates of characters found in all directions to delete it and then have the solution.

This is my code:

  with open('cp5_Colori.txt', 'r') as f:

  data=f.read().replace("\t","")
    data=data.split("\n\n")
    lista_parole=data[1].split()
    lista_orizzontale=data[0].split()
    oriz_contraria=[x[::-1] for x in lista_orizzontale]
    diz={}
    c=0
    b=0

    cruzi_verticali=[]
    for x in lista_parole:                  #loop to find rows and add the 
                                             found 
                                             words to a diz

        for y in lista_orizzontale:
            if x in y:
                diz[x]=1
        for z in oriz_contraria:
            if x in z:
                diz[x]=1

        while c <= len(lista_orizzontale):
            cruzi_verticali.append(lista_orizzontale[c][b])     #loop for 
                                                                  columns
            c+=1
            if c==len(lista_orizzontale):
                cruzi_verticali.append("///")
                c=0
                b+=1
                if b==len(lista_orizzontale):
                    c=len(lista_orizzontale)+1




    joinata="".join(cruzi_verticali)
    parole_verticali=joinata.split("///")
    vert_contraria=[k[::-1] for k in parole_verticali]    #convert to a list 
                                                            of 
                                                            strings and find 
                                                            the 
                                                            reversed of 
                                                             colums
    conta=0
    conta2=0


    for x in lista_parole:
        for y in parole_verticali:
            if x in y:                          #loop to add search word to  
                                                  the diz
                diz[x]=1
        for z in vert_contraria:
            if x in z:
                diz[x]=1


    cruzi_diagonali=[]            
    parole_diagonali=[]
    diag_contraria=[]            
    prova=[]            
    itera=len(parole_verticali)**2            
    while len(prova)!=len(parole_verticali)-1:
        cruzi_diagonali.append(parole_verticali[conta][conta2])        
        conta+=1
        conta2+=1
        if conta==len(lista_orizzontale):
            cruzi_diagonali.append("///")                                    
                                                    #loop to find a part of 
                                                     diagonals
        if conta==len(parole_verticali)-1:
            conta=0
            if conta==0:
                prova.append(0)
                conta=conta+len(prova)
                conta2=0
    prova2=[]            
    conta3=0
    conta4=1
    while len(prova2)!=len(parole_verticali)-1:
        cruzi_diagonali.append(parole_verticali[conta3][conta4])
        conta3+=1
        conta4+=1
        if conta4==len(lista_orizzontale):
            cruzi_diagonali.append("///")
                                                #loop to find lower 
                                                 diagonals
        if conta4==len(parole_verticali)-1:
            conta4=0
            if conta4==0:
                prova2.append(0)
                conta4=conta4+len(prova2)
                conta3=0

    joinata2="".join(cruzi_diagonali)
    parole_diagonali=joinata2.split("///")               #convert diagonals 
                                                            into 
                                                          a list of strings
    diag_contraria=[k[::-1] for k in parole_diagonali]   

    for x in lista_parole:
       for y in set(parole_diagonali):
           if x in y:                          #loop to add the found words in 
                                               the dictionary as keys
               diz[x]=1
       for z in set(diag_contraria):
          if x in z:
             diz[x]=1

    soluzione=[]            
    lista_totale=[]
    lista_orizzontale2=lista_orizzontale[:]
    for k in diz.keys():
        for k2 in lista_orizzontale2:             #all the found words in 
                                                  the 

                                                   row replaced with "*"
            if k in k2:
                hg=len(k)*"*"
                k3=k2.replace(k,hg)
                lista_orizzontale2.append(k3)
                if "*" not in k2:
                    lista_orizzontale2.remove(k2)

Could someone help me by finding all the coordinates of the found letters in the wordsearch?

2

There are 2 answers

0
Nuclearz On

this is my new code from your idea:

with open('cp5_Colori.txt', 'r') as f:
import pprint
data=f.read().replace("\t","")
A=[]
data=data.split("\n\n")
word_list=data[1].split()
lista_orizzontale=data[0].split()
puzzle=[list(row) for row in lista_orizzontale]
for parola in word_list:
    for lista in puzzle:
        x=puzzle.index(lista)
        for carattere in lista:
            y=lista.index(carattere)
            if carattere.upper() == parola[0]:
                for direction in [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]:
                    (dx, dy) = direction
                    for i in range(len(parola)):    
                        if ((puzzle[x+dx*i][y+dy*i].upper()== parola[i]) and ((x+dx*i)<len(puzzle)) and ((y+dy*i)<len(lista))) == True: 
                            A.append(puzzle[x+dx*i][y+dy*i])
                            if "".join(A)==parola:



                        else:
                            break
pprint.pprint(puzzle)

here

i don't rescue to find the solution. the solution should be "sangueblu" what is wrong?

3
r3mainer On

You're making things rather difficult for yourself. Instead of transforming the puzzle array in every direction, just visit each letter in turn and look for words in each of the eight directions starting from this letter.

Since this appears to be some sort of homework, I'll leave you to fill in the details, but the basic outline of your code should look something like this:

puzzle = [list(row) for row in 'OTNEGRASAE', 'RNNCORALLO', 'OAIBLUEEVG',
        'UTORENTIIA', 'VIOLETTOOR', 'OCRARIAELO', 'DABIMALVAP', 'IPCIELOGLR',
        'CORPOSOUAO', 'APIENOMILP']
word_list = ['ACIDO', 'ARGENTO', 'BLU', 'CIELO', 'CORALLO', 'CORPOSO', 'ELETTRICO',
        'LATTE', 'LIMONE', 'MALVA', 'NERO', 'OCRA', 'OPACITA', 'ORO', 'PAGLIERINO',
        'PIENO', 'PORPORA', 'PRIMITIVO', 'VIOLA', 'VIOLETTO']

for {each word in word_list}:
    for {each cell in puzzle}:
            if {cell.upper() == first character of word}:
                for direction in [(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1),(1,-1)]:
                    (dx, dy) = direction
                    {Does the puzzle contain all the other characters of word in this direction?}
                    {If so change these cells to lower case, and skip to next word}

{extract all the remaining upper case letters from the puzzle}