Wordsearches Take Long Unless You Code

Suppose you have a grid as follows:

And you are given the following list of words to find:

DOG

MARK

MATH

You must determine the positions (row and column position) of where each word STARTS (assume the words appear in rows and read forward - left to right - or backwards - right to left. You do not have to look for words vertically or diagonally). Let the "position sum" be the sum of the row position and the column position.

The word "DOG" starts (forwards) in row 1, column 1 and position sum = 1 + 1 = 2

The word "MARK" starts (forwards) in row 2, column 2 and position sum = 2 + 2 = 4

The word "MATH" starts (backwards) in row 4, column 4 and position sum = 4 + 4 = 8

Now, download the grid of letters here and the list of words here .

Calculate the the position sum for each word in the second file and report your answer as the sum of all these values.

So for the example above, your final answer will be 2 + 4 + 8 = 14


The answer is 2086.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

A quite simple Python solution.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
with open('Crossword.txt') as f:
    crossword = f.read().replace(' ','').splitlines()
with open('CrosswordWords.txt') as f:
    words = f.read().splitlines()

count = 0
for word in words:
    for j, line in enumerate(crossword):
        inv_line = line[::-1]
        if word in line:
            count += line.index(word) + j + 2
        elif word in inv_line:
            count += len(inv_line) - inv_line.index(word) + j + 1

print(count)

This provides the answer, which is 2086 \boxed{2086} .

Bill Bell
Jan 9, 2016

In Python.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
words=[word.strip() for word in file('CrosswordWords.txt').readlines()]

reversedWords=[]
for i in range(len(words)):
    reversedWords.append(words[i][::-1])

score=0
for lineNumber, line in enumerate(file('Crossword.txt').readlines()):
    line=''.join(line[:-1].split(' '))
    for word in words:
        p=line.find(word)
        if p!=-1:
           score+=lineNumber+p+2
    for reversedWord in reversedWords:
        p=line.find(reversedWord)
        if p!=-1:
            score+=lineNumber+1+p+len(reversedWord)
print score

Mark Mottian
Aug 5, 2015

Click here to see the full output for the JAVA program I wrote below:

import java.io.File;
import java.util.Scanner;

public class Simple_Coding {
    public static void main (String args []){
        int maxTotal=0;
        String words [] = new String [13]; 
        words[0] = "-1";
        int count = 0;
        try {
            Scanner fileWords = new Scanner (new File("Crossword2Words.txt"));
            while (fileWords.hasNext()){
                count++;
                words[count] = fileWords.nextLine();
            }

        }catch (Exception e){
            System.out.println(e);
        }

        for (int x = 1; x <= 12; x++){
            String word = words[x];
            String wordReversed = revWord(word);

            try{
                Scanner file = new Scanner (new File ("Crossword2.txt"));
                int row = 0;
                int column = -1;

                while (file.hasNext()){
                    String line = file.nextLine();
                    row++;
                    line = line.replaceAll(" ","");
                    if (line.indexOf(word)>-1){
                        column = line.indexOf(word)+1;
                        System.out.println(word + "\tRow: " + row + "\tColumn: "+column + "\tForwards\tPosition Sum = " + (row+column));
                        maxTotal = maxTotal + row + column;
                    }else if (line.indexOf(wordReversed) > -1){
                        column = line.indexOf(wordReversed)+word.length();
                        System.out.println(word + "\tRow: " + row + "\tColumn: "+column + "\tBackwards\tPosition Sum = " + (row+column));
                        maxTotal = maxTotal + row + column;
                    }
                }

            }catch (Exception e){
                System.out.println(e);
            }
        }
        System.out.println();
        System.out.println("Final Answer: " + maxTotal);
    }

public static String revWord(String string){
    String value = "";
    for (int x = string.length()-1; x >= 0; x--){
        value += string.charAt(x);
    }
    return value;
    }
}

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...