Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
At least it works... solution in Clear category for Break Rings by Krischtopp
def break_rings(rings):
number_of_rings = max(max(connection) for connection in rings)
return break_rek(rings, [], number_of_rings)
def break_rek(rings, broken_rings, number_of_rings):
if all(any(ring in broken_rings for ring in connection) for connection in rings): # Check if all rings are separated
return len(broken_rings) # Number of broken rings
min_break = number_of_rings # Just take anything big enough
first_ring_to_break = broken_rings[-1] + 1 if broken_rings else 1 # Last broken ring + 1 is the first ring we need to break
for i in range(first_ring_to_break, number_of_rings + 1): # These rings we need to break
broken_rings.append(i) # Break a ring
min_break = min(min_break, break_rek(rings, broken_rings, number_of_rings)) # Update minimum number of broken rings
broken_rings.pop() # Put the ring back together
return min_break
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"
assert break_rings(({1,9},{1,2},{8,5},{4,6},{5,6},{8,1},{3,4},{2,6},{9,6},{8,4},{8,3},{5,7},{9,7},{2,3},{1,7},)) == 5, "Extra 4"
assert break_rings(({1,2},{2,3},{3,4},{4,5},{5,2},{1,6},{6,7},{7,8},{8,9},{9,6},{1,10},{10,11},{11,12},{12,13},{13,10},{1,14},{14,15},{15,16},{16,17},{17,14},)) == 8, "Extra 13"
Jan. 25, 2016