diff --git a/2025/day2/part1/main.py b/2025/day2/part1/main.py index e69de29..b773e42 100644 --- a/2025/day2/part1/main.py +++ b/2025/day2/part1/main.py @@ -0,0 +1,29 @@ + +INPUT_FILE = "input.txt" + +# Read File +with open(INPUT_FILE, 'r') as file: + groups: list[str] = file.read().strip().split(',') + +# Parse Ranges +ranges: list[tuple[int, int]] = [ (int(n1), int(n2)) for n1, n2 in [group.split('-') for group in groups] ] + + +# Check for invalid ids +invalid_ids: list[int] = [] + +for (n1, n2) in ranges: + for n in range(n1, n2 + 1): + str_n: str = str(n) + if len(str_n) % 2 == 0: + mid: int = int(len(str_n) / 2) + if str_n[:mid] == str_n[mid:]: + invalid_ids.append(n) + print(f'Invalid Id: {n}') + +print(f'Sum of Invalid Ids: {sum(invalid_ids)}') + + + + + diff --git a/2025/day2/part2/input.txt b/2025/day2/part2/input.txt new file mode 100644 index 0000000..853bfba --- /dev/null +++ b/2025/day2/part2/input.txt @@ -0,0 +1 @@ +92916254-92945956,5454498003-5454580069,28-45,4615-7998,4747396917-4747534264,272993-389376,36290651-36423050,177-310,3246326-3418616,48-93,894714-949755,952007-1003147,3-16,632-1029,420-581,585519115-585673174,1041-1698,27443-39304,71589003-71823870,97-142,2790995-2837912,579556301-579617006,653443-674678,1515120817-1515176202,13504-20701,1896-3566,8359-13220,51924-98061,505196-638209,67070129-67263432,694648-751703,8892865662-8892912125 diff --git a/2025/day2/part2/main.py b/2025/day2/part2/main.py new file mode 100644 index 0000000..0d84f61 --- /dev/null +++ b/2025/day2/part2/main.py @@ -0,0 +1,48 @@ +import math + +INPUT_FILE = "input.txt" + +# Read File +with open(INPUT_FILE, 'r') as file: + groups: list[str] = file.read().strip().split(',') + +# Parse Ranges +ranges: list[tuple[int, int]] = [ (int(n1), int(n2)) for n1, n2 in [group.split('-') for group in groups] ] + + +# Check for invalid ids +invalid_ids: set[int] = set() + +for (n1, n2) in ranges: + for n in range(n1, n2 + 1): + str_n: str = str(n) + n_len: int = len(str_n) + mid: int = math.ceil(n_len / 2) + + for pattern_length in range(1, mid + 1): + + # If number cannot be split by pattern length it cannot be repeated at least twice + if n_len % pattern_length != 0: + continue + + pattern = str_n[:pattern_length] + + all_match = True + repeats = 0 + for i in range(0, n_len, pattern_length): + if str_n[i:i + pattern_length] != pattern: + all_match = False + break + else: + repeats += 1 + + if all_match and n not in invalid_ids and repeats >= 2: + invalid_ids.add(n) + print(f'Invalid Id: {n}') + +print(f'Sum of Invalid Ids: {sum(invalid_ids)}') + + + + + diff --git a/2025/day2/part2/test.txt b/2025/day2/part2/test.txt new file mode 100644 index 0000000..bd04584 --- /dev/null +++ b/2025/day2/part2/test.txt @@ -0,0 +1 @@ +11-22,95-115,998-1012,1188511880-1188511890,222220-222224,1698522-1698528,446443-446449,38593856-38593862,565653-565659,824824821-824824827,2121212118-2121212124 \ No newline at end of file