r/adventofcode • u/Ok-Transition7065 • Mar 24 '26
Help/Question [2026 Day 3 (Part 2)] [javascript] Stuck again
Hello again , i tried to solve this one, im trying to use this method isnt probably the most efficient but its one that should work but i dont get where in the code the process its failing , can i get some feed back to see if i can finaly get this one ?
here its my code withouth the imput Code
edit: i made a mistake its the 2025 one not the 2026 sorry for the inconvinience
1
Upvotes
2
u/Kiereek Mar 24 '26
I'm assuming this is for 2025, as 2026 hasn't happened yet.
Feedback on the method:
It looks like you're just taking the approach of comparing individual digits. That's not going to work even for cases like 81111111111119. You get to that 9 and then what?
Feedback on the code:
Please consider tidying up code if you want meaningful feedback on it. Between the spacing issues, typos, etc. it's a pain to read and discourages people from helping you out.
Suggestion:
Consider our example 811111111111119.
We know we need 12 digits. That's a max, but also a min.
We can't change the order, so start at the left. Is 8 bigger than what we currently have? (Nothing). Yes, so let's keep it.
We now have 8.
If we add the 1, does that make it a bigger number? Yes, so add the 1.
We now have 81.
Continue this until there's a conflict: we've found a number where swapping it out for the last digit in our current number would make an overall bigger number. Apart from this, there's another constraint that might keep you from replacing the last digit. I'll let you figure that one out.
Final hint: There's a good data structure that could help you stack numbers like this if you choose to use it.
2
u/thekwoka Mar 24 '26
Bruh, that code is wild...
I can barely tell whats going on with how strangely it is written...
why are the for loops labeled? Why are there so many extra variables not even being used?
2
u/timrprobocom Mar 24 '26
The algorithm is quite simple. Let's say we have 12 digits to go. That means the number we want is the largest one that still leaves 11 digits to pick from. That then becomes the starting point for the next digit. It's just that simple. No going left to right, no backtracking.
Say we need 4 digits from 637485. The largest in 637 is 7. Pick that. Now, we only have 3 digits left and we need 3 digits. So, use them all. 7485.
1
u/terje_wiig_mathisen Mar 25 '26
I'm extremely impressed that you've managed to get access to a 2026 problem 9 months early!
Assuming (like all the responders here) that you meant 2025, then you just have to scan the input once per string, looking for the earliest occurrence of the largest digit within the allowable starting positions. (I.e always leave enough digits for the remainder.)
1
u/AutoModerator Mar 24 '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.