Last active
July 5, 2023 18:02
-
-
Save jmccardle/d9395ddf6c59299069958977556c90ac 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
# See diagram: https://i.imgur.com/UWDrqcH.png | |
# We need 8 equations to solve the diagram, but I can only come up with 5. | |
# For three more equations, we will assume a value for X, A, and D. | |
# If sympy times out, we will cancel the attempt. | |
# Values for X: 7 choose 4, 35 possible values. | |
# Values for A: 7. | |
# (Naive) Maximum loops: 7*35 = 245. | |
# (actual) maximum loops: 4 * 35 = 140. A has to be in the circle for the solution for X we're using. | |
# Timeout setup ( https://stackoverflow.com/questions/40915527/kill-function-after-a-given-amount-of-time ) | |
TIMEOUT = 5 # seconds | |
import signal | |
class TimeoutException(Exception): | |
pass | |
def timeout_handler(s, f): | |
raise TimeoutException | |
signal.signal(signal.SIGALRM, timeout_handler) | |
def limited_execution(func): | |
def wraps(*args, **kwargs): | |
try: | |
signal.alarm(TIMEOUT) | |
result = func(*args, **kwargs) | |
signal.alarm(0) | |
return result | |
except TimeoutException: | |
return None | |
return wraps | |
# System of Equations setup | |
from sympy import Eq, solve, var | |
var('a, b, c, d, e, f, g, x') | |
#@limited_execution | |
def solve_soe(X, A, D): | |
equations = [ | |
Eq(a + d + f + g, 2 * x), | |
Eq(b + d + e + g, 3 * x), | |
Eq(c + f + e + g, 4 * x), | |
Eq(a + b + c + d + e + f + g, 28), | |
Eq(a * b * c * d * e * f * g, 5040), | |
Eq(x, X), | |
Eq(a, A), | |
Eq(d, D) | |
] | |
return solve(equations) | |
# 7 choose 4 combination setup ( https://www.geeksforgeeks.org/permutation-and-combination-in-python ) | |
from itertools import combinations | |
def main(): | |
for comb in combinations([1,2,3,4,5,6,7], 4): | |
X = sum(comb) / 2.0 | |
for A in range(1,7+1): | |
if A not in comb: continue # A is in the 2X sum group; if A is not in this combination, it's not a solution | |
for D in range(1,7+1): | |
if D == A: continue | |
elif D not in comb: continue | |
print(f"X = sum({comb})/2 = {X}, A = {A}, D = {D}") | |
print(solve_soe(X, A, D)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment