1. 10
    1. 11

      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.

      1. 1

        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.

        1. 2

          It’s a copy and paste, no linking required.

          1. 1


    2. 5

      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.

      1. Pass in your allocator from the top. This lets you use std.testing.allocator which is a superpower during…
      2. 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

      fn charToPriority(in: u8) u8 {
          switch (in) {
              'a'...'z' => return (in - 'a') + 1,
              'A'...'Z' => return (in - 'A') + 27,
              else => unreachable,
      test "Chars become priorities" {
          try std.testing.expect(charToPriority('a') == 1);
          try std.testing.expect(charToPriority('z') == 26);
          try std.testing.expect(charToPriority('A') == 27);
          try std.testing.expect(charToPriority('Z') == 52);
          try std.testing.expect(charToPriority('p') == 16);
          try std.testing.expect(charToPriority('L') == 38);
          try std.testing.expect(charToPriority('P') == 42);
          try std.testing.expect(charToPriority('v') == 22);
          try std.testing.expect(charToPriority('t') == 20);
          try std.testing.expect(charToPriority('s') == 19);

      The test is right there. You don’t need to go anywhere else to figure out what is involved.