Skip to content

Instantly share code, notes, and snippets.

@ABcDexter
Forked from rep-movsd/puzzle.py
Created May 16, 2021 10:00
Show Gist options
  • Save ABcDexter/a47dcc2cd9b3699c582a15edfcd1f5b8 to your computer and use it in GitHub Desktop.
Save ABcDexter/a47dcc2cd9b3699c582a15edfcd1f5b8 to your computer and use it in GitHub Desktop.
Word puzzle checker
#/usr/bin/python
import sys
import json
# open grid file
with open(sys.argv[1]) as f:
lines = [line.rstrip('\n') for line in f]
# make the grid but padded on left right and bottom sides with a sentinel like this (makes it easy to grab diagonals)
# *X*
# m = width and n = height, w is full padded width
grid = lines
m = len(lines[0])
n = len(lines)
w=m*3
# left and right padding for each row
hstars = '*' * m
grid = [ (hstars + row + hstars) for row in grid ]
print('Grid')
for row in range(n):
print(grid[row])
print()
# List of strings to search horizontally, vertically, primary and secondary diagonal
horz = lines
vert = [''.join([grid[row][col] for row in range(n)]) for col in range(m,m*2)]
diag1 = [''.join([grid[row][row+col] for row in range(n)]) for col in range(m*2)]
diag2 = [''.join([grid[row][col-row] for row in range(n)]) for col in range(m,w)]
# combine all of these including their reverse into a set
gridwords = horz + vert + diag1 + diag2
gridwords += [word[::-1] for word in gridwords]
gridwords = list(set(gridwords))
gridwords.sort()
# Make one big string with all words separated by ',' filter out the '*'
wordcat = ','.join([word for word in gridwords])
wordcat = ''.join([c for c in wordcat if c != '*'])
print('All words in grid')
print(wordcat)
print()
# open words file
with open(sys.argv[2]) as f:
words = [line.rstrip('\n') for line in f]
print('List of words')
print(words)
print()
print('Checking if words exist in grid')
for word in words:
count = wordcat.count(word)
if count != 1:
print('Word occured %d times: %s' % (count, word))
print('Done')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment