lucamasepo
lucamasepo

Reputation: 11

Find the solution from a word search game

I have a file with a matrix that is my wordsearch and some words that i need to find in it.

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

For the solution I have thought about something like that:

 with open('cp5_Colori.txt', 'r') as f:
        import pprint
        data=f.read().replace("\t","")
        A=[]
        B=set()
        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 ((x+dx*i)<len(puzzle)) and ((y+dy*i)<len(lista)) == True:
                                    if puzzle[x+dx*i][y+dy*i].upper()== parola[i]:
                                        puzzle[x+dx*i][y+dy*i]=puzzle[x+dx*i][y+dy*i].lower()







        pprint.pprint(puzzle)

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. I don't know how to continue and how to find the solution that is "SANGUEBLU"

Upvotes: 0

Views: 530

Answers (2)

blhsing
blhsing

Reputation: 106618

Given the following initialization:

puzzle = [l.split() for l in '''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'''.splitlines()]

word_list = '''ACIDO
ARGENTO
BLU
CIELO
CORALLO
CORPOSO
ELETTRICO
LATTE
LIMONE
MALVA
NERO
OCRA
OPACITA
ORO
PAGLIERINO
PIENO
PORPORA
PRIMITIVO
VIOLA
VIOLETTO'''.splitlines()

The following code will solve your problem:

from itertools import product
removals = []
for word in word_list:
    for row in range(len(puzzle)):
        for col in range(len(puzzle[row])):
            for dr, dc in product(range(-1, 2), repeat=2):
                if dr or dc:
                    removal = []
                    for i in range(len(word)):
                        r = row + dr * i
                        c = col + dc * i
                        if not (0 <= r < len(puzzle) and 0 <= c < len(puzzle[row])) or puzzle[r][c] != word[i]:
                            break
                        removal.append((r, c))
                    else:
                        removals.append(removal)
for removal in removals:
    for row, col in removal:
        puzzle[row][col] = None
print(''.join(char for row in puzzle for char in row if char))

This outputs:

SANGUEBLU

Upvotes: 1

iGian
iGian

Reputation: 11193

Once you downcased the letter using your code, you can get the result by:

crossed = [['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']]

import re
result = ''.join([ ''.join(line) for line in crossed ])
print(result)
hidden_word = ''.join(re.findall(r'[A-Z]', result))
print (hidden_word) #=> SUGU

But it seems you code downcased too many words.

Upvotes: 0

Related Questions