Created
July 5, 2023 17:41
-
-
Save jmccardle/d93d1b72b6dd8f27c5eef85e21b08da3 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
# (Major issues) - not enough equations to solve this SOE! | |
# See diagram: https://i.imgur.com/UWDrqcH.png | |
# We need 7 equations to solve the diagram, but I can only come up with 5. | |
# For two more equations, we will assume a value for X and A. | |
# 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 = 10 # 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): | |
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) | |
] | |
return solve(equations) | |
# 7 choose 4 combination setup ( https://www.geeksforgeeks.org/permutation-and-combination-in-python ) | |
from itertools import combinations | |
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 | |
print(f"X = sum({comb})/2 = {X}, A = {A}") | |
print(solve_soe(X, A)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment