"mod but it actually works properly" is one of my standard functions I have sitting around in another file because of how often I end up needing it for one reason or another.
(a,b)=>(a%b+b)%b, by the way. (Using a better language would also work, but I don't mind this sort of workaround.)
1.2.3 is called "adjusted mod" and is useful in some applications.
In general the modulus/remainder operator should be defined such that b * (a div b) + (a mod b) = a, where div is integer division. If your integer division rounds toward 0, then a mod b should have the same sign as a. If your integer division is always floor(a / b), then a mod b should have the same sign as b. (Or, if you have no integer division operator and have to pick how to round every time, then go the other way and choose floor() ve trunc() or whatever according to how your mod function behaves.)
Along with the other 50% of participants. 😂 I decided not to use mod at all though, because I’m lazy and just did each increment individually which ended up making part two dead simple.
I actually managed to find a division-based solution to part 2! Just needed to implement "division rounding towards +/- infinity" rather than the usual truncating division
I have a sacred Christmas tradition where I completely forget that % is not a mathematically correct modulo operator in many (if not most) programming languages.
I want to scream. I thought that % was the mathematically correct modulo but only worked with unsigned integers. thank you for your insightful comment.
In JavaScript the % symbol actually is... remainder. (Which was helpful, because then I didn't get tripped up with negative numbers, which always confuse me with modulo.)
I remember a few years ago I did AoC and learned about the mod operator... a few weeks or months later I got a job interview in which the solution to a coding challenge wholly depended on knowing how to use the mod operator! So in a way AoC landed me a job!!
Hey man, don't let your sloped forehead get you down. I've unga-bungaed my way through more stars than I care to admit. I drag my knuckles with, if not exactly pride, a certain resigned acceptance.
I knew that mod is weird with negatives and so made a "wrap" function that does the right thing.
Part 2, though still stumped me. I don't see the right way to do one rotation of n units using math + minimal if statements. Like I still don't.
I tried some things that didnt work and ended up just brute forcing the solution by calling my part 1 rotate function n times, doing n +1 or -1 increments for each right or left rotation of n.
Can anyone explain the non-brute force function to count the zero crossings in one rotation in part 2?
First how many times does the the dial completely rotate? Rounded down integer clicks/100 (all of these "touch" zero)
What's left? Add or subtract that amount to the previous dial value. Did it go <=0 or >= 100? If yes, add 1.
One gotcha to my approach is you need to also see if the previous value was a zero because if it was and you see the value be -5 or 105, then you are double counting a zero.
I'm omitting other code for brevity but this is my zero counting logic. Consider that it's not quite enough to just check the remainder because it might move a few clicks beyond an even rotation without actually crossing 0. There may be a more elegant way but this is what I came up with.
that was it. thanks. yeah i see it now ... without using the remainder there you are adding on zeros that you already counted in the "complete rotations" logic.
It it’s slightly different if you’re going left or right, but they both follow the same basic principle: for each move, how far do you need to go to hit zero the first time, and then how many multiples of 100 after
Right: Take the amount you’re moving, add your current position. Integer division by 100, and that’s how many times you crossed 0.
Say you start at 80. You need 20 to cross 0, and then increments of 100 each time after, so a value of R20, R120, R220, gives 1, 2, 3 etc.
The amount you need to move to hit zero the first time is 100 - current position. So if you add your current position to your move amount, that gives your total displacement as if you started from 0, so integer division from 100 is how many times you crossed 0
Left: same idea, just in reverse. You current position is how much extra you need to move to hit zero for the first time, so you subtract your current position from the move amount, and then integer division 100 to get how many times you crossed.
Guys, need help from different pairs of eye, can anyone tell me why this is wrong?
psw=0
pos=50
for step in ${steps}; do
slice=${step:1}
slice=${slice//$'\r'/}
slice="$(echo -n "$slice")"
if [ "${step:0:1}" == "L" ]; then
pos=$(( pos - slice ))
else
pos=$(( pos + slice ))
fi
if [ ${pos} -gt 99 ]; then
pos=$(( pos % 100 ))
fi
if [ ${pos} -lt 0 ]; then
pos=$(( 100 + (pos % 100) ))
fi
if [ ${pos} -eq 0 ]; then
psw=$(( psw + 1))
fi
done
81
u/1234abcdcba4321 Dec 01 '25
"mod but it actually works properly" is one of my standard functions I have sitting around in another file because of how often I end up needing it for one reason or another.
(a,b)=>(a%b+b)%b, by the way. (Using a better language would also work, but I don't mind this sort of workaround.)40
u/captainAwesomePants Dec 01 '25
Me, who decided to learn Rust this Advent: oh no
31
u/Alan_Reddit_M Dec 01 '25
I just used a fucking loop as though I was actually rotating a lock because I could not for the love of God get the mod function to work properly
29
u/AlternativePeace1121 Dec 01 '25
Idiot me was using modding with 99 instead of 100
Also TIL, java returns -ve vales for %
22
u/Valuable_Leopard_799 Dec 01 '25
Same,
truncatereturning negative numbers,modonly positive, etc. took me for a ride, I keep forgetting.And
modcan also behave differently in different languages.19
u/Walnoot Dec 01 '25
Today I was quite puzzled for a while until I learned about the difference between
1andl12
u/The_Real_Slim_Lemon Dec 01 '25
Not me sleeping on advent and forgetting to start it in December, but I have stupid work now ): stupid coding job stopping me from coding
7
u/SunPotatoYT Dec 01 '25
In my head I always pronounce it as remainder to remind myself what it does
7
u/staubwirbel Dec 01 '25
I have a sacred Christmas tradition where I completely forget that % is not a mathematically correct modulo operator in many (if not most) programming languages.
5
u/gadgetygirl Dec 01 '25
In JavaScript the % symbol actually is... remainder. (Which was helpful, because then I didn't get tripped up with negative numbers, which always confuse me with modulo.)
5
u/BlazingThunder30 Dec 01 '25
Fuck Java for improperly implementing this operator. It has tripped me up every year for the past few years
5
u/_benj Dec 02 '25
I remember a few years ago I did AoC and learned about the mod operator... a few weeks or months later I got a job interview in which the solution to a coding challenge wholly depended on knowing how to use the mod operator! So in a way AoC landed me a job!!
9
u/JohnDalyProgrammer Dec 01 '25
Man after seeing everyone bring up modulo today I wish I had thought of using it in my solution lol. I went full caveman on my solution
3
u/jwezorek Dec 01 '25 edited Dec 01 '25
I knew that mod is weird with negatives and so made a "wrap" function that does the right thing.
Part 2, though still stumped me. I don't see the right way to do one rotation of n units using math + minimal if statements. Like I still don't.
I tried some things that didnt work and ended up just brute forcing the solution by calling my part 1 rotate function n times, doing n +1 or -1 increments for each right or left rotation of n.
Can anyone explain the non-brute force function to count the zero crossings in one rotation in part 2?
1
u/[deleted] Dec 01 '25
not in js
console.log(-5 % 3); // Output: -2
1
u/Best-Gas-2203 Dec 02 '25 edited Dec 02 '25
Guys, need help from different pairs of eye, can anyone tell me why this is wrong?
1
u/_Mark_ Dec 02 '25
Ada95 (which I haven't used this century) recognizes that there are two different but similar functions, and has both
remandmod...1
u/pinkwar Dec 02 '25
I can't believe I missed AOC start.
Thanks for reminding me.
1
u/StunningRegular8489 Dec 10 '25
Day 1 triggered me so hard I switched from Rust to Go