aoc2025/bob/day02/day02-p2.py

62 lines
1.7 KiB
Python

def check_id(start_id, end_id):
false_ids = []
for id in range(int(start_id), int(end_id)+1):
id_str = str(id)
id_str_len = len(id_str)
for substr_len in range(1,(id_str_len//2)+1):
if id_str_len % substr_len > 0:
# Substring being checked must fit a whole number of times
continue
dup = False
# Substring position is determined by the amount of times the substring fits in the string
# That makes up the range to be checked (as multipliet with the substring length)
for substr_pos in range(1, id_str_len // substr_len):
h1 = id_str[:substr_len]
# Get the substring at right position, and only with size of substring
h2 = id_str[substr_len*substr_pos:][:substr_len]
if h1 != h2:
dup = False
break
else:
dup = True
if dup:
false_ids.append(id)
break
return false_ids
def read_input(filename):
id_ranges = []
for line in open(filename).read().splitlines():
for id_range in line.split(","):
start = id_range.split("-")[0]
end = id_range.split("-")[1]
id_ranges.append((start,end))
return id_ranges
# filename = "day02/my_example_input" #
# filename = "day02/example_input" # 4174379265
filename = "day02/input" # 44143124633
id_ranges = read_input(filename)
adding = 0
for id_range in id_ranges:
for false_id in check_id(id_range[0], id_range[1]):
adding += false_id
print(f"Added total of false IDs: {adding}")