Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
brute-force solution in Clear category for Break Rings by masamish
from functools import reduce
from itertools import combinations
def break_rings(rings):
# number of rings
n_rings = max(reduce(set.union, rings))
def trial(n):
# rings for remove
combi = combinations(range(1,n_rings+1), n)
for c in combi:
# count saved rings after removing rings
rings_ = map(lambda x: len(x - set(c)), rings)
if max(rings_) == 1:
return True
return False
# brute force search
for n in range(1, n_rings+1):
if trial(n):
return n
return 0
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert break_rings(({1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {4, 6})) == 3, "example"
assert break_rings(({1, 2}, {1, 3}, {1, 4}, {2, 3}, {2, 4}, {3, 4})) == 3, "All to all"
assert break_rings(({5, 6}, {4, 5}, {3, 4}, {3, 2}, {2, 1}, {1, 6})) == 3, "Chain"
assert break_rings(({8, 9}, {1, 9}, {1, 2}, {2, 3}, {3, 4}, {4, 5}, {5, 6}, {6, 7}, {8, 7})) == 5, "Long chain"
Feb. 28, 2015