Skip to content

Instantly share code, notes, and snippets.

@knutwalker
Created May 9, 2012 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knutwalker/2649134 to your computer and use it in GitHub Desktop.
Save knutwalker/2649134 to your computer and use it in GitHub Desktop.
Python: Solve GLAT question 1
#!/usr/bin/env python
"""
A brute-force approach to solve the first quesiton of GLAT
More about GLAT: http://googleblog.blogspot.de/2004/09/pencils-down-people.html
The question is as follows:
1. Solve this cryptic equation, realizing of course that values for M and E
could be interchanged. No leading zeros are allowed.
WWWDOT - GOOGLE = DOTCOM
The current solution ignores the hint. Well, it ignores pretty much everything.
"""
import itertools
import sys
chars = [list(word) for word in "WWWDOT,GOOGLE,DOTCOM".lower().split(',')]
uchars = set(itertools.chain(*chars))
uchars.add('')
permutations = itertools.permutations(uchars, 10)
equations = [list(reversed(c)) for c in reversed(zip(*chars))]
def map_(per, equations):
o3 = 0
for op1, op2, su in equations:
o1, o2, s = per.index(op1), per.index(op2), per.index(su)
if not (o1 + o2 + o3) % 10 == s:
return False
o3 = (o1 + o2 + o3) / 10
return True
def reduce_(iterable, equations):
for per in iterable:
if map_(per, equations):
return per
return False
exitcode = 1
sol = reduce_(permutations, equations)
if sol:
exitcode = 0
for i, p in enumerate(sol):
if p:
print "{0}: {1}".format(p.upper(), i)
if exitcode:
print "boohoo!"
sys.exit(exitcode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment