input = open("input", 'r') ranges = [] for i, line in enumerate(input): raw_ranges = line.rstrip().split(",") for id_range in raw_ranges: id_start, id_end = id_range.split("-") ranges.append((id_start,id_end)) # Part 1 # invalid_ids = [] # for start_id, end_id in ranges: # start_id_length = len(start_id) # greater_half = start_id[:start_id_length//2] # lesser_half = start_id[start_id_length//2:] # # # Find the closest invalid_id # if start_id_length % 2 != 0: # greater_half = "1" + "0"*len(greater_half) # elif int(greater_half) < int(lesser_half): # greater_half = str(int(greater_half) + 1) # halves = greater_half # # while int(halves*2) <= int(end_id): # invalid_ids.append(int(halves*2)) # halves = str(int(halves) + 1) # # print(sum(invalid_ids)) # Part 1 & 2 invalid_ids = set() invalid_ids_p1 = [] for start_id, end_id in ranges: init_start_id = start_id for sections in range(2,len(end_id)+1): start_id = init_start_id # ensure sections divides start_id while len(end_id) >= len(start_id) and len(start_id) % sections != 0: start_id = "1" + "0"*len(start_id) if len(end_id) < len(start_id): continue divide_length = len(start_id) // sections # Check starting position greatest_sect = start_id[:divide_length] if int(greatest_sect*sections) >= int(start_id) and \ int(greatest_sect*sections) <= int(end_id): invalid_ids.add(int(greatest_sect*sections)) if sections == 2: invalid_ids_p1.append(int(greatest_sect*sections)) greatest_sect = str(int(greatest_sect) + 1) # Check all other invalid_ids while int(greatest_sect*sections) <= int(end_id): invalid_ids.add(int(greatest_sect*sections)) if sections == 2: invalid_ids_p1.append(int(greatest_sect*sections)) greatest_sect = str(int(greatest_sect) + 1) print(sum(invalid_ids_p1)) print(sum(invalid_ids))