30 lines
646 B
Python
30 lines
646 B
Python
|
|
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)}')
|
|
|
|
|
|
|
|
|
|
|