import numpy as np from part1 import read_inventory def get_disjunct_ranges(ranges): our_ranges = ranges.copy() changed = True while changed: changed = False for i in range(len(our_ranges)): a, b = our_ranges[i] for j in range(i + 1, len(our_ranges)): c, d = our_ranges[j] if c < a and a <= d and d <= b: our_ranges[i] = np.array([c, b]) our_ranges = np.delete(our_ranges, j, 0) changed = True break elif c < a and b < d: our_ranges[i] = np.array([c, d]) our_ranges = np.delete(our_ranges, j, 0) changed = True break elif a <= c and c <= b and a <= d and d <= b: our_ranges[i] = np.array([a, b]) our_ranges = np.delete(our_ranges, j, 0) changed = True break elif a <= c and c <= b and b < d: our_ranges[i] = np.array([a, d]) our_ranges = np.delete(our_ranges, j, 0) changed = True break if changed: break return our_ranges def get_fresh_id_counts(ranges): disjunct_ranges = get_disjunct_ranges(ranges) return np.sum(disjunct_ranges[:,1] - disjunct_ranges[:,0]) + len(disjunct_ranges) if __name__ == "__main__": test_ranges, test_items = read_inventory("testinput") assert get_fresh_id_counts(test_ranges) == 14 ranges, items = read_inventory("input") print(get_fresh_id_counts(ranges))