Skip to content

Instantly share code, notes, and snippets.

@titanjer
Created June 21, 2014 20:01
Show Gist options
  • Save titanjer/32cf6b3cf790d9322b3f to your computer and use it in GitHub Desktop.
Save titanjer/32cf6b3cf790d9322b3f to your computer and use it in GitHub Desktop.
geek.py Q1
import sys
import time
import string
import hashlib
import itertools
def main(filename, output):
with open(filename) as f:
T = int(f.readline())
L = int(f.readline())
questions = {}
for c in range(T):
questions[f.readline().strip()] = [c+1, ]
c = 0
start_time = time.time()
for t in itertools.product(string.ascii_lowercase, repeat=L):
t = ''.join(t)
h = hashlib.md5(t).hexdigest()
if h in questions:
questions[h].append(t)
c += 1
if c & 0b11111111111111111111 == 0:
solved = len([_ for _ in questions.values() if len(_) > 1])
print >> sys.stderr, "%10s: %ss, %s" % (c, time.time() - start_time, solved)
if solved == T:
break
print >> sys.stderr, questions.values()
with open(output, 'w') as o:
for p in sorted(questions.values(), key=lambda pair: pair[0]):
print >> o, 'Case #%s: %s' % (p[0], p[1])
if __name__ == '__main__':
if len(sys.argv) != 3:
sys.exit(-1)
filename = sys.argv[1]
output = sys.argv[2]
main(filename, output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment