Skip to content

Instantly share code, notes, and snippets.

@jsundram
Last active August 23, 2025 04:54
Show Gist options
  • Save jsundram/599697c798e1f9e43ed0517121c89fac to your computer and use it in GitHub Desktop.
Save jsundram/599697c798e1f9e43ed0517121c89fac to your computer and use it in GitHub Desktop.
from itertools import permutations
def main():
A = [24, 3, 'A', 6, 27, 0, 9, 'AB', 17, 'ABC', 14, 'AC']
B = [20, 4, 23, 7, 'B', 1, 26, 'BC', 16, 'ABC', 17, 'AB']
C = [11, 5, 22, 8, 'C', 2, 25, 'AC', 14, 'ABC', 16, 'BC']
# Figure out what numbers are missing.
N = list(range(28))
n = set(i for i in A + B + C if type(i) == int)
missing = sorted(set(N) - n)
print(missing)
# we have 7 blanks: 'A', 'AB', 'ABC', 'AC', 'B', 'BC', 'C'
# we have 7 missing numbers.
# brute force means trying all the mappings of 7 to 7, 7! = 5040, so that's doable.
partial_sum_a = sum([a for a in A if type(a) == int])
partial_sum_b = sum([b for b in B if type(b) == int])
partial_sum_c = sum([c for c in C if type(c) == int])
print('A: %d' % (partial_sum_a))
print('B: %d' % (partial_sum_b))
print('C: %d' % (partial_sum_c))
for (a, ab, abc, ac, b, bc, c) in permutations(missing):
asum = partial_sum_a + a + ab + abc + ac
bsum = partial_sum_b + b + bc + abc + ab
csum = partial_sum_c + c + ac + abc + bc
if asum == bsum == csum:
print("3 ring circuit total: %d" % asum)
print(f"'A': {a}, 'B': {b}, 'C': {c}, 'AB': {ab}, 'AC': {ac}, 'BC': {bc}, 'ABC': {abc}")
print(f'ring A: 24 + 3 + {a} + 6 + 27 + 0 + 9 + {ab} + 17 + {abc} + 14 + {ac} = {asum}')
print(f'ring B: 20 + 4 + 23 + 7 + {b} + 1 + 26 + {bc} + 16 + {abc} + 17 + {ab} = {bsum}')
print(f'ring C: 11 + 5 + 22 + 8 + {c} + 2 + 25 + {ac} + 15 + {abc} + 16 + {bc} = {csum}')
if __name__ == '__main__':
main()

3 Ring Circuit

3ring.jpg

Approach:

  1. Label the blank spots in the diagram (see above).
  2. Problem now boils down to:
Ring A Sum = 24 + 3 + A + 6 + 27 + 0 + 9 + AB + 17 + ABC + 14 + AC
Ring B Sum = 20 + 4 + 23 + 7 + B + 1 + 26 + BC + 16 + ABC + 17 + AB
Ring C Sum = 11 + 5 + 22 + 8 + C + 2 + 25 + AC + 14 + ABC + 16 + BC

Find A, AB, B, BC, C, AC, ABC such that Ring A Sum = Ring B Sum = Ring C Sum.
  1. Find the numbers that are missing from the range that is used and are candidates for the solution:

Missing numbers: [10, 12, 13, 15, 18, 19, 21]

  1. This is a mapping of 7 numbers onto 7 blank spots (A, AB, B, BC, C, AC, ABC); there are 7! = 5040 ways to do this.
  2. Brute force is your friend, see code above :)

Solutions

  • 3 ring circuit total: 164

    • A = 19, B = 13, C = 21, AB = 15, AC = 18, BC = 10, ABC = 12
    • Ring A: 24 + 3 + 19 + 6 + 27 + 0 + 9 + 15 + 17 + 12 + 14 + 18 = 164
    • Ring B: 20 + 4 + 23 + 7 + 13 + 1 + 26 + 10 + 16 + 12 + 17 + 15 = 164
    • Ring C: 11 + 5 + 22 + 8 + 21 + 2 + 25 + 18 + 15 + 12 + 16 + 10 = 164
  • 3 ring circuit total: 167

    • A = 21, B = 10, C = 19, AB = 13, AC = 15, BC = 12, ABC = 18
    • Ring A: 24 + 3 + 21 + 6 + 27 + 0 + 9 + 13 + 17 + 18 + 14 + 15 = 167
    • Ring B: 20 + 4 + 23 + 7 + 10 + 1 + 26 + 12 + 16 + 18 + 17 + 13 = 167
    • Ring C: 11 + 5 + 22 + 8 + 19 + 2 + 25 + 15 + 15 + 18 + 16 + 12 = 167
  • 3 ring circuit total: 168

    • A = 21, B = 12, C = 15, AB = 10, AC = 18, BC = 13, ABC = 19
    • Ring A: 24 + 3 + 21 + 6 + 27 + 0 + 9 + 10 + 17 + 19 + 14 + 18 = 168
    • Ring B: 20 + 4 + 23 + 7 + 12 + 1 + 26 + 13 + 16 + 19 + 17 + 10 = 168
    • Ring C: 11 + 5 + 22 + 8 + 15 + 2 + 25 + 18 + 15 + 19 + 16 + 13 = 168
  • 3 ring circuit total: 170

    • A = 19, B = 10, C = 15, AB = 12, AC = 18, BC = 13, ABC = 21
    • Ring A: 24 + 3 + 19 + 6 + 27 + 0 + 9 + 12 + 17 + 21 + 14 + 18 = 170
    • Ring B: 20 + 4 + 23 + 7 + 10 + 1 + 26 + 13 + 16 + 21 + 17 + 12 = 170
    • Ring C: 11 + 5 + 22 + 8 + 15 + 2 + 25 + 18 + 15 + 21 + 16 + 13 = 170
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment