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