From 90b0b79a783c4a5af790b45efd42ebf5ce903c04 Mon Sep 17 00:00:00 2001 From: Julia Lange Date: Tue, 2 Dec 2025 14:46:41 -0800 Subject: [PATCH] 2025 02 --- Python/2025/02/main.py | 60 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Python/2025/02/main.py diff --git a/Python/2025/02/main.py b/Python/2025/02/main.py new file mode 100644 index 0000000..1ffd43f --- /dev/null +++ b/Python/2025/02/main.py @@ -0,0 +1,60 @@ +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))