Created
January 29, 2015 22:11
-
-
Save Pwootage/d5ea24d4e35ff39d0671 to your computer and use it in GitHub Desktop.
Python crossword one-liner (i'm so sorry)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import itertools | |
class Crossword: | |
def __init__(self, path): | |
f = open(path, 'r') | |
lines = filter(lambda x: len(x) > 0, map(lambda x: x.strip(), f.readlines())) | |
f.close() | |
self.size = len(lines[0]) | |
self.grid = lines[0:self.size] | |
self.toFind = lines[self.size:] | |
def findAll(self): # Sorry... | |
return filter(lambda (w, sr, sc, er, ec): w in self.toFind, itertools.chain(*itertools.imap(lambda (r, c): itertools.imap(lambda (er, ec, w): (w, r, c, er, ec), itertools.chain(*itertools.starmap(lambda dr, dc: itertools.imap(lambda (er, ec): (er, ec, reduce(lambda a, b: a + b, itertools.starmap(lambda cr, cc: self.grid[cr][cc], itertools.takewhile(lambda (cr, cc): cr != er or cc != ec, itertools.imap(lambda x: (r + (1 if er > r else -1 if er < r else 0) * x, c + (1 if ec > c else -1 if ec < c else 0) * x), itertools.count()))), "")), itertools.takewhile(lambda (er, ec): 0 <= er < self.size and 0 <= ec < self.size, itertools.imap(lambda x: (r + dr * x, c + dc * x), itertools.count(0)))), itertools.ifilter(lambda (a, b): not (a == 0 and b == 0), itertools.product(range(-1, 2), repeat=2))))), itertools.product(range(0, self.size), range(0, self.size))))) | |
def main(): | |
if len(sys.argv) != 2: | |
print("Must provide a path to a file") | |
else: | |
crossword = Crossword(sys.argv[1]) | |
for w in crossword.findAll(): | |
print "{} found at start: {}, {} end: {}, {}".format(*w) | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
HGAMONIHRA | |
AOMOKAWONS | |
NFROLBOBDN | |
ARFSIHCAGE | |
LNIEEWONOK | |
GOLFUNDTHC | |
KOCATAOHBI | |
AMRERCGANH | |
SLGFAMALLC | |
ALLIGATORX | |
HORSE | |
COW | |
RHINO | |
JABBERWOCKY | |
CAT | |
DOG | |
ALLIGATOR | |
CHICKEN | |
FROG | |
BANTHA | |
MOOSE | |
LLAMA |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment