Last active
January 28, 2018 21:13
-
-
Save wray/6edc606c078cab10519c976567f0e278 to your computer and use it in GitHub Desktop.
Break up the problem to reduce memory needed
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 time | |
import itertools | |
def brute_force_digits(pin): | |
""" Uses itertools to create a cartesian product of the possible pins """ | |
for guess in list(itertools.product(list(range(10)),repeat=len(str(pin)))): | |
if pin == int(len(str(pin))*'%d' % (guess)): | |
return guess | |
def brute_force_split(pin,n): | |
""" Breaking up the problem space to minimize needed memory for brute_force_digits """ | |
split = len(pin)//n | |
l = [ int(pin[i*n:(i+1)*n]) for i in range(split) ] | |
return [ brute_force_digits(ppin) for ppin in l ] | |
if __name__ == '__main__': | |
""" Actually works really well on single machine when dividing by 2. | |
Somewhat contrived here in order to illustrate parallel (lambda) processing advantages in | |
certain situations. | |
In this case, dividing by 6; breaks down with pin size of 360 (i7 16GB) | |
""" | |
print('Provide a digit-only pin whose length is divisible by 6 to be brute-forced guessed') | |
pin = input() | |
t1 = time.time() | |
guess = brute_force_split(pin,6) | |
print('Found: ' + str(guess)) | |
t2 = time.time() | |
print('Elapsed time was ' + str(t2-t1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment