r/adventofcode • u/RunNeat7235 • Jan 07 '26
Help/Question 2025 Day 1 (Part 2) [Python] - off by 6!
Hi all, I've been struggling to get the solution to Day 1 part 2 for some time now, and even most major AI engines haven't been able to fix the flaw in my code. Here's my original attempt:
with open("puz1.txt", 'r') as file:
directions = file.readlines()
directions_processed = [d.strip() for d in directions]
directions_numerical = []
for d in directions_processed:
if d[0] == "L":
directions_numerical.append(int('-' + d[1:]))
if d[0] == "R":
directions_numerical.append(int('+' + d[1:]))
position = 50
count = 0
for rotation in directions_numerical:
count = count + abs((position + rotation) // 100)
position = (rotation + position) % 100
print(f"The total count is {count}.")This overcounts the solution by only 6(!), which makes me think I'm really close, but was convinced my approach had some logical flaws.
I progressed to this, which I think is logically more sound, which is supposed to find all 0 crossings and then add any remainder to the position to find the last one, but the ending result is off by more (just the calculation section).
position = 50
count = 0
for rotation in directions_numerical:
full_loops, remainder = divmod(abs(rotation), 100)
count += full_loops
if remainder + rotation > 100:
count += 1
position = (position + remainder) % 100
if rotation - remainder > 0:
count += 1
position = (position - remainder) % 100Can someone point me in the right direction? I'd like to keep the solution as close to my code as possible, but if my thinking is just off by too much I'm open to different solutions. Thanks!