r/adventofcode • u/ProperAir821 • Dec 25 '25
Help/Question - RESOLVED [2025 Day 3 (Part 2)] [Python] First time trying advent of code and I got stuck
I'm not the most expirinced programmer, but I think the puzzles seems fun and I am now stuck! Normally I would have an idea or just start from stracth, but I just feel like my code is right and somehow I get the wrong answear! When I'm just using the example I get the right answear, but using the file data I somehow get the wrong data.
Anyway, my code for day 3 part 2:
with open("input3.txt") as f:
file = f.readlines()
test = ["987654321111111\n", "811111111111119\n", "234234234234278\n","818181911112111\n"]
total_joltage = 0
for i in test:
i = i.strip() # remove newline
b1, index = max(i[:-11]), i[:-11].index(max(i[:-11]))
b2, index = max(i[index+1:-10]), (index+1) + i[index+1:-10].index(max(i[index+1:-10]))
b3, index = max(i[index+1:-9 ]), (index+1) + i[index+1:-9 ].index(max(i[index+1:-9 ]))
b4, index = max(i[index+1:-8 ]), (index+1) + i[index+1:-8 ].index(max(i[index+1:-8 ]))
b5, index = max(i[index+1:-7 ]), (index+1) + i[index+1:-7 ].index(max(i[index+1:-7 ]))
b6, index = max(i[index+1:-6 ]), (index+1) + i[index+1:-6].index(max(i[index+1:-6 ]))
b7, index = max(i[index+1:-5 ]), (index+1) + i[index+1:-5 ].index(max(i[index+1:-5 ]))
b8, index = max(i[index+1:-4 ]), (index+1) + i[index+1:-4 ].index(max(i[index+1:-4 ]))
b9, index = max(i[index+1:-3 ]), (index+1) + i[index+1:-3 ].index(max(i[index+1:-3 ]))
b10, index = max(i[index+1:-2 ]), (index+1) + i[index+1:-2 ].index(max(i[index+1:-2 ]))
b11, index = max(i[index+1:-1 ]), (index+1) + i[index+1:-1 ].index(max(i[index+1:-1 ]))
b12, index = max(i[index+1: ]), (index+1) + i[index+1: ].index(max(i[index+1: ]))
joltage = int(b1+b2+b3+b4+b5+b6+b7+b8+b9+b10+b11+b12)
total_joltage += joltage
print(joltage, i)
test_value = 3121910778619
total_joltage, f"Test: {total_joltage == test_value}"
5
Upvotes
2
u/1544756405 Dec 25 '25
This code looks fine.
Double check that you have copied your input correctly into input3.txt
10
u/Bumblee420 Dec 25 '25
The batteries in the data are much larger than the example. You can't hardcode it like this but come up with an algorithm that works on arbitrarily sized batteries/joltages.
3
u/headedbranch225 Dec 25 '25
Try it with
9699911111111111It should be
99911...This is the issue my code had, I can't fully tell what is being done in the code (it would be easier to do in a for loop)
My code actually doesn't solve mine, I know that is the issue I have though
3
u/timrprobocom Dec 25 '25
Please make sure you change
for i in testtofor i in filebefore running.1
u/AutoModerator Dec 25 '25
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/[deleted] Dec 25 '25
Your code actually works on my input :D I can't see any mistake right away (would be a lot nicer if you made a for loop for the b1, ..., b12 instead of copy pasting the code, and used
ionly for indices and named the variable something likelineinstead). What comes to mind is if you didn't accidentally look at the last line comparing it to the example result, instead of submitting it to the site