Yet Another Advent of Code Template (YAAoCT ?)

I was messing around with the build system, and made an Advent of Code solution printer that lives entirely inside of a build.zig. Maybe not really the intended use of the build system, but I had fun making this template.

Put it in a directory like this:

input/
    YYYY/
        day01.txt
        day02.txt
YYYY/
    day_01.zig
    day_02.zig
build.zig

In each day_DD.zig you define functions part1([]u8) and part2([]u8), that take a mutable or constant slice of u8, and can return anything printable with {s}, {any}, or {f}, including error unions, which either print their error or the inner value.

$ zig build solve -Dday=..3
[01/1] (43.3 µs)    foo
I am stderr print from within Day 1 Part 2
[01/2]  failed: Overflow
[02/1] (26.361 ms)  27
...
[03/2] (1.652 ms)   bar
Total elapsed solution time: 104.833 ms (excluding 1 failure)

I wanted to make it as thoughtless as possible to jump into puzzle exercises and ended up with this. You only have to write your own solution code inside of each day_DD.zig, and it all runs as a single build step. I hope someone finds it useful!

3 Likes

You throw out the number of bytes written

if (days.color) _ = try writer.write("\x1b[2m");

Why don’t you use writeAll()?

2 Likes

That should definitely be a writeAll, write returns the number of written bytes precisely because it’s allowed to only write a subset of the data given to it, and it’s on you to progress the writing process in a loop in that case.

And that’s what writeAll does for you.

4 Likes

My first time with new IO if I’m being honest. I appreciate it, that’s already corrected

Might be a good idea to rename

write -> writePartial
writeAll -> write

?

I think one of the regrets of Rust is that write name follows C/POSIX tradition, rather than breaking away from it.

One would think that Zig’s mandatory use of return value would be a sufficient guardrail here, but I’ve seen it more than once that someone incorrectly uses write instead of writeAll in Zig.

10 Likes

Arguments for keeping the current names: throwing away the result with _ = should be quite the warning that you miss something. And the partial write is more basic, so could earn the more basic name. (But a whole write seems conceptually simpler than a partial one?)

But I experiencend it being a very common mistake with C beginners, so I’d support your renaming proposal.

1 Like

For anyone using this template and only following this thread: I have fixed a few issues, and added benchmarking, since this was first posted. The build.zig can drop-in replace an outdated one. This should be the final version save for any breaking updates to the language. If you’ve enjoyed, thanks!