Day 2: Part 2
This commit is contained in:
16
day2/main.py
16
day2/main.py
@@ -1,9 +1,15 @@
|
||||
def is_invalid_id(n: int) -> bool:
|
||||
def is_invalid_part2(n: int) -> bool:
|
||||
s = str(n)
|
||||
if len(s) % 2 != 0:
|
||||
L = len(s)
|
||||
for k in range(1, L // 2 + 1):
|
||||
if L % k != 0:
|
||||
continue
|
||||
block = s[:k]
|
||||
if block[0] == "0":
|
||||
continue
|
||||
if block * (L // k) == s:
|
||||
return True
|
||||
return False
|
||||
half = len(s) // 2
|
||||
return s[:half] == s[half:]
|
||||
|
||||
|
||||
total = 0
|
||||
@@ -14,7 +20,7 @@ with open("input.txt") as f:
|
||||
for part in line.split(","):
|
||||
lo, hi = map(int, part.split("-"))
|
||||
for n in range(lo, hi + 1):
|
||||
if is_invalid_id(n):
|
||||
if is_invalid_part2(n):
|
||||
total += n
|
||||
|
||||
print(total)
|
||||
|
||||
Reference in New Issue
Block a user