diff --git a/2025/02/part_1_solution.py b/2025/02/part_1_solution.py new file mode 100644 index 0000000..c943368 --- /dev/null +++ b/2025/02/part_1_solution.py @@ -0,0 +1,50 @@ +testing = False +if not testing: + with open(r"2025/02/input.txt", "r") as file: + input = file.read() +else: + input = '''11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124''' + +product_id_list = input.split(',') + +bad_products_list = [] + +def is_invalid_id(id_num): + """ + Check if an ID is invalid (made of some sequence repeated twice) + """ + id_str = str(id_num) + length = len(id_str) + + # If length is odd, can't be made of two equal parts + if length % 2 != 0: + return False + + # Split into two halves and check if they're identical + half_length = length // 2 + first_half = id_str[:half_length] + second_half = id_str[half_length:] + + return first_half == second_half + + +for product_id in product_id_list: + # print(product_id) + first_id,second_id = product_id.split('-') + print(first_id, 'to', second_id) + for product in range(int(first_id), int(second_id)+1): + # print(product) + if is_invalid_id(product): + print(f'{product} invalid') + bad_products_list.append(product) + else: + pass + # print(f'product {product} looks valid') + print(bad_products_list) + +total_sum = 0 + +for bad_id in bad_products_list: + total_sum += int(bad_id) + +print(total_sum) \ No newline at end of file diff --git a/2025/02/part_2_solution.py b/2025/02/part_2_solution.py new file mode 100644 index 0000000..ec0291e --- /dev/null +++ b/2025/02/part_2_solution.py @@ -0,0 +1,56 @@ +testing = False +if not testing: + with open(r"2025/02/input.txt", "r") as file: + input = file.read() +else: + input = '''11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124''' + +product_id_list = input.split(',') + +bad_products_list = [] + +def is_invalid_id(id_num): + """ + Check if an ID is invalid (made of some sequence repeated at least twice) + """ + id_str = str(id_num) + length = len(id_str) + + # Try all possible pattern lengths from 1 to half the string length + for pattern_length in range(1, length // 2 + 1): + # Check if the string can be divided evenly by this pattern length + if length % pattern_length == 0: + # Get the pattern (first part) + pattern = id_str[:pattern_length] + # Calculate how many times this pattern should repeat + num_repeats = length // pattern_length + # If it repeats at least twice, it's invalid + if num_repeats >= 2: + # Check if the entire string is made up of this pattern repeated + if pattern * num_repeats == id_str: + return True + + return False + + +for product_id in product_id_list: + # print(product_id) + first_id,second_id = product_id.split('-') + print(first_id, 'to', second_id) + for product in range(int(first_id), int(second_id)+1): + # print(product) + if is_invalid_id(product): + print(f'{product} invalid') + bad_products_list.append(product) + else: + pass + # print(f'product {product} looks valid') + print(bad_products_list) + +total_sum = 0 + +for bad_id in bad_products_list: + total_sum += int(bad_id) + +print(total_sum) + diff --git a/2025/03/part_1_solution.py b/2025/03/part_1_solution.py new file mode 100644 index 0000000..6cfb206 --- /dev/null +++ b/2025/03/part_1_solution.py @@ -0,0 +1,7 @@ +testing = True +if not testing: + with open(r"2025/03/input.txt", "r") as file: + input = file.read() +else: + input = ''' +''' diff --git a/main.py b/main.py deleted file mode 100644 index 0f30f1a..0000000 --- a/main.py +++ /dev/null @@ -1,6 +0,0 @@ -def main(): - print("Hello from advent-of-code!") - - -if __name__ == "__main__": - main()