2025(02): solve part 2
Grafpuzzels zijn het soms.
This commit is contained in:
64
2025/02/part2.py
Normal file
64
2025/02/part2.py
Normal file
@ -0,0 +1,64 @@
|
||||
def is_invalid_id(num):
|
||||
# print("Testing", num)
|
||||
num_str = str(num)
|
||||
num_len = len(num_str)
|
||||
for period in range(1, num_len):
|
||||
# print("period =", period)
|
||||
# Period should be a factor of num_len
|
||||
if num_len % period != 0:
|
||||
continue
|
||||
|
||||
# Start with True, make False when it fails
|
||||
palin = True
|
||||
for idx in range(period, num_len, period):
|
||||
# print("idx =", idx)
|
||||
# print(num_str[:period], "=?=", num_str[idx:idx + period])
|
||||
if num_str[:period] != num_str[idx:idx + period]:
|
||||
# print(False)
|
||||
palin = False
|
||||
# print(False)
|
||||
if palin:
|
||||
# print("Returning palin")
|
||||
return True
|
||||
|
||||
# No period has yielded repetition:
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
with open("input", "r") as fp:
|
||||
rangestrs = fp.read().split(",")
|
||||
ranges = []
|
||||
for rangestr in rangestrs:
|
||||
a, b = rangestr.split("-")
|
||||
a_int, b_int = int(a), int(b)
|
||||
ranges.append((a, b))
|
||||
assert a_int < b_int, f"Range invalid: {a} is not smaller than {b}"
|
||||
|
||||
illegal_counter = 0
|
||||
illegal_sum = 0
|
||||
for a, b in ranges:
|
||||
a_int, b_int = int(a), int(b)
|
||||
for num in range(a_int, b_int + 1):
|
||||
if is_invalid_id(num):
|
||||
print(num, "is a palindrome thing")
|
||||
illegal_counter += 1
|
||||
illegal_sum += num
|
||||
# num_str = str(num)
|
||||
# num_len = len(num_str)
|
||||
# palin = False
|
||||
# # Look at the different options for lengths
|
||||
# for partlen in range(2, num_len + 1):
|
||||
# # Only continue if it's a factor
|
||||
# if num_len % partlen != 0:
|
||||
# continue
|
||||
# for idx in range(partlen, num_len, partlen):
|
||||
# if num_str[:partlen] == num_str[idx:idx + partlen]:
|
||||
# continue
|
||||
# palin = True
|
||||
# break
|
||||
# if palin:
|
||||
# print(num_str, "is a palindrome thing")
|
||||
# illegal_counter += 1
|
||||
# illegal_sum += num
|
||||
|
||||
print(illegal_counter, illegal_sum)
|
||||
Reference in New Issue
Block a user