Last active
January 17, 2021 03:03
-
-
Save trbarron/4a7d23bb1e347edd2dd846b630cfaee8 to your computer and use it in GitHub Desktop.
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
# This is a script to find the answer to the Riddler. | |
# https://fivethirtyeight.com/features/can-you-hunt-for-the-mysterious-numbers/ | |
import random, time | |
def find_1_digit_factors(target): | |
factors = [] | |
for value in range(1, 10): | |
if (target % value == 0): | |
factors.append(value) | |
return factors + factors + factors | |
def winnow_factors(factors,target): | |
finished = False | |
while not finished: | |
random.shuffle(factors) | |
if factors[0] * factors[1] * factors[2] == target: | |
finished = True | |
return factors[:3] | |
def brute_force_answer(): | |
r_matrix = [294,216,135,98,112,84,245,40] | |
b_matrix = [8890560, 156800, 55566] | |
found_sol = False | |
s_time = time.time() | |
while not found_sol: | |
a = winnow_factors(find_1_digit_factors(r_matrix[0]), r_matrix[0]) | |
b = winnow_factors(find_1_digit_factors(r_matrix[1]), r_matrix[1]) | |
c = winnow_factors(find_1_digit_factors(r_matrix[2]), r_matrix[2]) | |
d = winnow_factors(find_1_digit_factors(r_matrix[3]), r_matrix[3]) | |
e = winnow_factors(find_1_digit_factors(r_matrix[4]), r_matrix[4]) | |
f = winnow_factors(find_1_digit_factors(r_matrix[5]), r_matrix[5]) | |
g = winnow_factors(find_1_digit_factors(r_matrix[6]), r_matrix[6]) | |
h = winnow_factors(find_1_digit_factors(r_matrix[7]), r_matrix[7]) | |
if a[0] * b[0] * c[0] * d[0] * e[0] * f[0] * g[0] * h[0] == b_matrix[0]: | |
if a[1] * b[1] * c[1] * d[1] * e[1] * f[1] * g[1] * h[1] == b_matrix[1]: | |
if a[2] * b[2] * c[2] * d[2] * e[2] * f[2] * g[2] * h[2] == b_matrix[2]: | |
print("time to solve: ", time.time() - s_time) | |
print("a: ", a) | |
print("b: ", b) | |
print("c: ", c) | |
print("d: ", d) | |
print("e: ", e) | |
print("f: ", f) | |
print("g: ", g) | |
print("h: ", h) | |
found_sol = True | |
# Press the green button in the gutter to run the script. | |
if __name__ == '__main__': | |
brute_force_answer() | |
# See PyCharm help at https://www.jetbrains.com/help/pycharm/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment