Wrapping Arithmetic - What's the best way?

AoC Day One Spoilers Ahead!

I’m trying to answer AoC Day 1, and I know the solution in my head, but I’m having a hard time getting the types right. I wrote up, almost line for line(!) this solution: AoC 2025: Day 1 - #6 by guidoschmidt , here are some of the relevant lines:

const max = 100;
var dial: i32 = 50;
// ...
const sign: i32 = if (row[0] == 'L') -1 else 1;
// ...
dial += sign;
dial = @mod(dial, max);

Now I know why the author made dial a signed integer, but it shouldn’t be, right? The dial can hold positions 0 - 99, those are all signed integers. I’m trying to find the most elegant, and zig-idiomatic way to type these variables such that the math is clear (without a thousand @casts), and the types are semantic (dial should be u7). Any ideas?

You probably meant dial *= sign. Keep in mind that in arithmetic modulo max, a-b is equal to a+max-b, and also to a+M*max-b – so you never have to end with a negative result.

You would need to treat maxInt as -1. Something like:

if (sign==1) dial += 1 else dial -%= 1;
if (dial == maxInt(u7)) dial = 99;
if (dial == 100) dial = 0;
// no @mod() needed

No, they are brute forcing the solution by adding 1 or -1 at a time.