In case anyone is using this for learning best practices, I want to repeat my suggestions from the zig subreddit:
(1) Use std.fs.Dir.readFileAlloc instead of your readInputFile function.
(2) No need to append lines into an array list. Just iterate over the memory directly.
(4) std.fmt.digitToChar
(6) std.fifo.LinearFifo. I’m not happy with how this data structure is named; I will likely rename it at some point.
(9) Please be aware of what @intCast does which is asserting that the mathematical value is representable in the destination type. Also, the % operator is available but only when modulus division and remainder division would yield the same result, i.e., when the inputs are unsigned.
I want to repeat my suggestions from the zig subreddit
can you link it? wondering if you wrote more that i can read. i want to do AOC in zig as well but since i have little experience with low level programming it would help me a lot.
In other languages, modulo can be calculated using the % operator. This is not the case in Zig. You have to use the @mod◹ function instead:
This is incorrect. It might only be defined for unsigned integers, but % is a working operator.
In my own zig experience for advent of code, I find myself preferring to use a std.io.reader rather than the two ways of reading the entire file in to memory at the same time. I don’t know if it saves any time, but I like the idea of streaming a large file in and processing it as it comes.
Of these tricks, the one I knew least was the for loop trick of capturing a pointer. And I agree with the statement at the end: when writing zig I keep my local checkout of the standard library open.
Two things the author missed.
Pass in your allocator from the top. This lets you use std.testing.allocator which is a superpower during…
Write tests. The zig syntax makes this very easy. In my development I keep a window open running watch zig test src/main.zig so I can see compiler errors and also confirm my code is working without off by one bugs.
Here is one of my functions from advent of code earlier, in fact the same one as given in an example in the original post for this section
In case anyone is using this for learning best practices, I want to repeat my suggestions from the zig subreddit:
(1) Use
std.fs.Dir.readFileAlloc
instead of yourreadInputFile
function.(2) No need to append lines into an array list. Just iterate over the memory directly.
(4)
std.fmt.digitToChar
(6)
std.fifo.LinearFifo
. I’m not happy with how this data structure is named; I will likely rename it at some point.(9) Please be aware of what
@intCast
does which is asserting that the mathematical value is representable in the destination type. Also, the%
operator is available but only when modulus division and remainder division would yield the same result, i.e., when the inputs are unsigned.can you link it? wondering if you wrote more that i can read. i want to do AOC in zig as well but since i have little experience with low level programming it would help me a lot.
It’s a copy and paste, no linking required.
Thanks
This is incorrect. It might only be defined for unsigned integers, but % is a working operator.
In my own zig experience for advent of code, I find myself preferring to use a std.io.reader rather than the two ways of reading the entire file in to memory at the same time. I don’t know if it saves any time, but I like the idea of streaming a large file in and processing it as it comes.
Of these tricks, the one I knew least was the for loop trick of capturing a pointer. And I agree with the statement at the end: when writing zig I keep my local checkout of the standard library open.
Two things the author missed.
watch zig test src/main.zig
so I can see compiler errors and also confirm my code is working without off by one bugs.Here is one of my functions from advent of code earlier, in fact the same one as given in an example in the original post for this section
The test is right there. You don’t need to go anywhere else to figure out what is involved.