solved 2024 Day 2 Parts 1 & 2
This commit is contained in:
1 parent
cc2ff9309d
commit
14a422d4e2
4 files changed
+79
No files matched your search
@@ -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)}')
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user