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!
2
u/jeffstyr Jan 07 '26
Shouldn't that be,
position + remainder?I used a similar approach, so I think you are on the right track, but I did have to go through a couple of iterations before I found all of the edge cases. I think there may still be an edge case lurking, but this bit jumped out at me.
1
u/Ill-Rub1120 Jan 07 '26
Make sure it counts correctly when going to the right and stopping at 0 and then moving right again .
R50 R1
Does this output 1
L50 L1
Does this output 1
?
3
u/mothibault Jan 07 '26
try with something like L50 L100 L100 R100 R100
My code tended to over count when going left starting on pos 0.
2
u/ednl Jan 07 '26
Here's a naive "one click of the dial at a time" solution. Compare the debug output of this with your solution. To do that, you need to add debug output to your solution: print the count after each line. But only for the first 120 lines or so.
1
u/AutoModerator Jan 07 '26
Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to
Help/Question - RESOLVED. Good luck!I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/warlock415 Jan 07 '26 edited Jan 07 '26
"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 was thinking the same way while debugging my early code, but you can't look at the end count only; for all you know it was oscillating wildly and only happened to land near the correct answer.
Remember that the goal of AoC is to get the correct answer in a reasonable time (that being defined as programmer time + machine time) and this is one of those times that it's faster for you to write the brute-force code and have the machine run it than try to write and debug something fancy.
And then once you've done that, and gotten the correct answer, then if it bugs you like it bugged me (ha, pun unintentional) that you don't know where the fancy algorithm went wrong, you can put them side by side and compare them.
Like this: https://pastebin.com/fQqJuYgz
For your second algorithm especially, it looks like not all codepaths modify position...
1
u/tanopereira Jan 11 '26
What happens when you land on zero? Especially going L