Advent of Code style coding challenges in Zig.
Each task is organized in its own folder under src/:
src/
├── day01/
│ ├── solution.zig # Solution code and tests
│ └── input.txt # Input data (optional)
├── day02/
│ ├── solution.zig
│ └── input.txt
└── ...
Run all tests for all tasks:
zig build testzig build runThis simply prints a message directing you to run tests.
When watching variables in the VSCode debugger, you can change the display format by adding a format specifier after the variable name:
variable,b # binary (useful for bit patterns)
variable,x # hexadecimal
variable,d # decimal
variable,o # octal
variable,c # character
Example: To view a u8 byte as binary instead of hex, add byte,b to the Watch window.
This is particularly useful when debugging instruction encoding where you need to see individual bits.
Add debug output in your tests or solution code:
const std = @import("std");
// Print to stderr (won't interfere with test output)
std.debug.print("byte value: 0b{b:0>8}\n", .{byte}); // binary with leading zeros
std.debug.print("byte value: 0x{x:0>2}\n", .{byte}); // hex with leading zeros
std.debug.print("byte value: {d}\n", .{byte}); // decimalRun tests for a specific task:
zig build test --summary allFilter tests by name:
zig test src/part1.zig --test-filter "specific test name"- Create a new folder:
src/dayXX/ - Add a
solution.zigfile with this template:
const std = @import("std");
pub fn solve(input: []const u8) !u32 {
// Your solution here
_ = input;
return 0;
}
test "dayXX example" {
const input = "example input";
const result = try solve(input);
try std.testing.expectEqual(@as(u32, 0), result);
}- (Optional) Add an
input.txtfile and load it with@embedFile("input.txt") - Add your task name to the
tasksarray in build.zig:
const tasks = [_][]const u8{
"day01",
"day02",
"dayXX", // Add your new task here
};- Run
zig build testto verify your solution