2025(02): solve part 2

Grafpuzzels zijn het soms.
This commit is contained in:
2025-12-02 09:10:51 +01:00
parent 0a96499f76
commit 1e01c9ee38
2 changed files with 88 additions and 0 deletions

View File

@ -41,3 +41,27 @@ Your job is to find all of the invalid IDs that appear in the given ranges. In t
Adding up all the invalid IDs in this example produces _`1227775554`_. Adding up all the invalid IDs in this example produces _`1227775554`_.
_What do you get if you add up all of the invalid IDs?_ _What do you get if you add up all of the invalid IDs?_
### Part Two
The clerk quickly discovers that there are still invalid IDs in the ranges in your list. Maybe the young Elf was doing other silly patterns as well?
Now, an ID is invalid if it is made only of some sequence of digits repeated _at least_ twice. So, `12341234` (`1234` two times), `123123123` (`123` three times), `1212121212` (`12` five times), and `1111111` (`1` seven times) are all invalid IDs.
From the same example as before:
* `11-22` still has two invalid IDs, _`11`_ and _`22`_.
* `95-115` now has two invalid IDs, _`99`_ and _`111`_.
* `998-1012` now has two invalid IDs, _`999`_ and _`1010`_.
* `1188511880-1188511890` still has one invalid ID, _`1188511885`_.
* `222220-222224` still has one invalid ID, _`222222`_.
* `1698522-1698528` still contains no invalid IDs.
* `446443-446449` still has one invalid ID, _`446446`_.
* `38593856-38593862` still has one invalid ID, _`38593859`_.
* `565653-565659` now has one invalid ID, _`565656`_.
* `824824821-824824827` now has one invalid ID, _`824824824`_.
* `2121212118-2121212124` now has one invalid ID, _`2121212121`_.
Adding up all the invalid IDs in this example produces _`4174379265`_.
_What do you get if you add up all of the invalid IDs using these new rules?_

64
2025/02/part2.py Normal file
View 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)