solved 2024 Day 2 Parts 1 & 2
This commit is contained in:
parent
cc2ff9309d
commit
14a422d4e2
@ -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)}')
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
1
2025/day2/part2/input.txt
Normal file
1
2025/day2/part2/input.txt
Normal file
@ -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
|
||||
48
2025/day2/part2/main.py
Normal file
48
2025/day2/part2/main.py
Normal file
@ -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)}')
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
1
2025/day2/part2/test.txt
Normal file
1
2025/day2/part2/test.txt
Normal file
@ -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
|
||||
Loading…
x
Reference in New Issue
Block a user