advent_of_code/2025/2/2_2.py
kleph 5ae76a626b
Some checks reported errors
continuous-integration/drone/push Build encountered an error
2025 - Day 2 part 2
2025-12-02 18:18:59 +01:00

34 lines
958 B
Python

#!/usr/bin/env python
# 2025 - Advent Of Code 2 - part2
# file = 'input_example.txt'
file = 'input.txt'
ranges = []
bad_id = []
with open(file, encoding="utf-8") as f:
for line in f.readlines():
for r in line.split(','):
(startr, endr) = r.split('-')
for i in range(int(startr), int(endr)+1):
ranges.append(str(i))
print(f"ranges to inspect {len(ranges)}")
# print(ranges)
for product_id in ranges:
for pivot in range(1, int(len(product_id) / 2) + 1):
bad = True
complete_pattern = 0
for cursor in range(0, len(product_id)):
if product_id[cursor % pivot] != product_id[cursor]:
bad = False
break
complete_pattern = (cursor + 1) % pivot
if bad and complete_pattern == 0:
bad_id.append(int(product_id))
print(f'bad id: {len(bad_id)}')
print(f'dedup bad id: {len(set(bad_id))}')
print(sum(set(bad_id)))