Skip to content

Instantly share code, notes, and snippets.

@matmunn
Created February 20, 2014 04:15
Show Gist options
  • Save matmunn/9107002 to your computer and use it in GitHub Desktop.
Save matmunn/9107002 to your computer and use it in GitHub Desktop.
Headline Puzzle Hat Solver
import re, string
global english, letters
letters = list(string.ascii_uppercase)
def without_nls(item):
for line in range(len(item)):
item[line] = item[line].rstrip('\n').upper()
return item
def alpha_order(word):
next_val = 1
order = ['' for i in range(100)]
for i in letters:
starts = [match.start() for match in re.finditer(re.escape(i), word)]
for j in starts:
order[j] = str(next_val)
next_val += 1
for i in range(len(order)):
if len(order[i]) == 0:
chopped = order[:i]
break;
return ' '.join(chopped)
fp = open('bigenwords.txt', 'r')
english = without_nls(fp.readlines())
fp.close()
possibles = []
characters = int(raw_input('How many characters in the hat? '))
order = raw_input('Word order separated by spaces: ')
for i in english:
if len(i) == characters:
if order == alpha_order(i):
possibles.append(i)
if len(possibles) > 0:
fp = open('poss_hats.txt', 'w')
fp.write('\n'.join(possibles))
fp.close()
print
print str(len(possibles)) + ' results were generated.'
if raw_input('Show now? (Y/N) ').upper() == 'Y':
print
print '\n'.join(possibles)
@matmunn
Copy link
Author

matmunn commented Feb 20, 2014

This script is able to solve possibilities for the hat when solving headline puzzles (https://sites.google.com/site/theheadlinepuzzle/).

Configuration

To use this script it requires a dictionary list file, which is not provided. You will have to find one and substitute the file name in the script, replacing 'bigenwords.txt' with the name of your file.

Usage

To run the script all you have to do is set it to executable by running:

$ chmod a+x HatSolver.py

Then you can double click it or type $ /path/to/HatSolver.py and you will be prompted with the inputs.
Alternatively you can run the script from the command line by issuing:

$ python HatSolver.py

Output
The script will automatically create a file called 'poss_hats.txt' in the current working directory. After this file has been generated it will tell you how many results were found and ask you if you want the results printed on the screen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment