···11+const std = @import("std");
22+33+// Although this function looks imperative, it does not perform the build
44+// directly and instead it mutates the build graph (`b`) that will be then
55+// executed by an external runner. The functions in `std.Build` implement a DSL
66+// for defining build steps and express dependencies between them, allowing the
77+// build runner to parallelize the build automatically (and the cache system to
88+// know when a step doesn't need to be re-run).
99+pub fn build(b: *std.Build) void {
1010+ // Standard target options allow the person running `zig build` to choose
1111+ // what target to build for. Here we do not override the defaults, which
1212+ // means any target is allowed, and the default is native. Other options
1313+ // for restricting supported target set are available.
1414+ const target = b.standardTargetOptions(.{});
1515+ // Standard optimization options allow the person running `zig build` to select
1616+ // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
1717+ // set a preferred release mode, allowing the user to decide how to optimize.
1818+ const optimize = b.standardOptimizeOption(.{});
1919+ // It's also possible to define more custom flags to toggle optional features
2020+ // of this build script using `b.option()`. All defined flags (including
2121+ // target and optimize options) will be listed when running `zig build --help`
2222+ // in this directory.
2323+2424+ // This creates a module, which represents a collection of source files alongside
2525+ // some compilation options, such as optimization mode and linked system libraries.
2626+ // Zig modules are the preferred way of making Zig code available to consumers.
2727+ // addModule defines a module that we intend to make available for importing
2828+ // to our consumers. We must give it a name because a Zig package can expose
2929+ // multiple modules and consumers will need to be able to specify which
3030+ // module they want to access.
3131+ const mod = b.addModule("advent_of_code_2025", .{
3232+ // The root source file is the "entry point" of this module. Users of
3333+ // this module will only be able to access public declarations contained
3434+ // in this file, which means that if you have declarations that you
3535+ // intend to expose to consumers that were defined in other files part
3636+ // of this module, you will have to make sure to re-export them from
3737+ // the root file.
3838+ .root_source_file = b.path("src/root.zig"),
3939+ // Later on we'll use this module as the root module of a test executable
4040+ // which requires us to specify a target.
4141+ .target = target,
4242+ });
4343+4444+ // Here we define an executable. An executable needs to have a root module
4545+ // which needs to expose a `main` function. While we could add a main function
4646+ // to the module defined above, it's sometimes preferable to split business
4747+ // logic and the CLI into two separate modules.
4848+ //
4949+ // If your goal is to create a Zig library for others to use, consider if
5050+ // it might benefit from also exposing a CLI tool. A parser library for a
5151+ // data serialization format could also bundle a CLI syntax checker, for example.
5252+ //
5353+ // If instead your goal is to create an executable, consider if users might
5454+ // be interested in also being able to embed the core functionality of your
5555+ // program in their own executable in order to avoid the overhead involved in
5656+ // subprocessing your CLI tool.
5757+ //
5858+ // If neither case applies to you, feel free to delete the declaration you
5959+ // don't need and to put everything under a single module.
6060+ const exe = b.addExecutable(.{
6161+ .name = "advent_of_code_2025",
6262+ .root_module = b.createModule(.{
6363+ // b.createModule defines a new module just like b.addModule but,
6464+ // unlike b.addModule, it does not expose the module to consumers of
6565+ // this package, which is why in this case we don't have to give it a name.
6666+ .root_source_file = b.path("src/main.zig"),
6767+ // Target and optimization levels must be explicitly wired in when
6868+ // defining an executable or library (in the root module), and you
6969+ // can also hardcode a specific target for an executable or library
7070+ // definition if desireable (e.g. firmware for embedded devices).
7171+ .target = target,
7272+ .optimize = optimize,
7373+ // List of modules available for import in source files part of the
7474+ // root module.
7575+ .imports = &.{
7676+ // Here "advent_of_code_2025" is the name you will use in your source code to
7777+ // import this module (e.g. `@import("advent_of_code_2025")`). The name is
7878+ // repeated because you are allowed to rename your imports, which
7979+ // can be extremely useful in case of collisions (which can happen
8080+ // importing modules from different packages).
8181+ .{ .name = "advent_of_code_2025", .module = mod },
8282+ },
8383+ }),
8484+ });
8585+8686+ // This declares intent for the executable to be installed into the
8787+ // install prefix when running `zig build` (i.e. when executing the default
8888+ // step). By default the install prefix is `zig-out/` but can be overridden
8989+ // by passing `--prefix` or `-p`.
9090+ b.installArtifact(exe);
9191+9292+ // This creates a top level step. Top level steps have a name and can be
9393+ // invoked by name when running `zig build` (e.g. `zig build run`).
9494+ // This will evaluate the `run` step rather than the default step.
9595+ // For a top level step to actually do something, it must depend on other
9696+ // steps (e.g. a Run step, as we will see in a moment).
9797+ const run_step = b.step("run", "Run the app");
9898+9999+ // This creates a RunArtifact step in the build graph. A RunArtifact step
100100+ // invokes an executable compiled by Zig. Steps will only be executed by the
101101+ // runner if invoked directly by the user (in the case of top level steps)
102102+ // or if another step depends on it, so it's up to you to define when and
103103+ // how this Run step will be executed. In our case we want to run it when
104104+ // the user runs `zig build run`, so we create a dependency link.
105105+ const run_cmd = b.addRunArtifact(exe);
106106+ run_step.dependOn(&run_cmd.step);
107107+108108+ // By making the run step depend on the default step, it will be run from the
109109+ // installation directory rather than directly from within the cache directory.
110110+ run_cmd.step.dependOn(b.getInstallStep());
111111+112112+ // This allows the user to pass arguments to the application in the build
113113+ // command itself, like this: `zig build run -- arg1 arg2 etc`
114114+ if (b.args) |args| {
115115+ run_cmd.addArgs(args);
116116+ }
117117+118118+ // Creates an executable that will run `test` blocks from the provided module.
119119+ // Here `mod` needs to define a target, which is why earlier we made sure to
120120+ // set the releative field.
121121+ const mod_tests = b.addTest(.{
122122+ .root_module = mod,
123123+ });
124124+125125+ // A run step that will run the test executable.
126126+ const run_mod_tests = b.addRunArtifact(mod_tests);
127127+128128+ // Creates an executable that will run `test` blocks from the executable's
129129+ // root module. Note that test executables only test one module at a time,
130130+ // hence why we have to create two separate ones.
131131+ const exe_tests = b.addTest(.{
132132+ .root_module = exe.root_module,
133133+ });
134134+135135+ // A run step that will run the second test executable.
136136+ const run_exe_tests = b.addRunArtifact(exe_tests);
137137+138138+ // A top level step for running all tests. dependOn can be called multiple
139139+ // times and since the two run steps do not depend on one another, this will
140140+ // make the two of them run in parallel.
141141+ const test_step = b.step("test", "Run tests");
142142+ test_step.dependOn(&run_mod_tests.step);
143143+ test_step.dependOn(&run_exe_tests.step);
144144+145145+ // Just like flags, top level steps are also listed in the `--help` menu.
146146+ //
147147+ // The Zig build system is entirely implemented in userland, which means
148148+ // that it cannot hook into private compiler APIs. All compilation work
149149+ // orchestrated by the build system will result in other Zig compiler
150150+ // subcommands being invoked with the right flags defined. You can observe
151151+ // these invocations when one fails (or you pass a flag to increase
152152+ // verbosity) to validate assumptions and diagnose problems.
153153+ //
154154+ // Lastly, the Zig build system is relatively simple and self-contained,
155155+ // and reading its source code will allow you to master it.
156156+}
+81
build.zig.zon
···11+.{
22+ // This is the default name used by packages depending on this one. For
33+ // example, when a user runs `zig fetch --save <url>`, this field is used
44+ // as the key in the `dependencies` table. Although the user can choose a
55+ // different name, most users will stick with this provided value.
66+ //
77+ // It is redundant to include "zig" in this name because it is already
88+ // within the Zig package namespace.
99+ .name = .advent_of_code_2025,
1010+ // This is a [Semantic Version](https://semver.org/).
1111+ // In a future version of Zig it will be used for package deduplication.
1212+ .version = "0.0.0",
1313+ // Together with name, this represents a globally unique package
1414+ // identifier. This field is generated by the Zig toolchain when the
1515+ // package is first created, and then *never changes*. This allows
1616+ // unambiguous detection of one package being an updated version of
1717+ // another.
1818+ //
1919+ // When forking a Zig project, this id should be regenerated (delete the
2020+ // field and run `zig build`) if the upstream project is still maintained.
2121+ // Otherwise, the fork is *hostile*, attempting to take control over the
2222+ // original project's identity. Thus it is recommended to leave the comment
2323+ // on the following line intact, so that it shows up in code reviews that
2424+ // modify the field.
2525+ .fingerprint = 0xe67df0676dcfbe28, // Changing this has security and trust implications.
2626+ // Tracks the earliest Zig version that the package considers to be a
2727+ // supported use case.
2828+ .minimum_zig_version = "0.15.2",
2929+ // This field is optional.
3030+ // Each dependency must either provide a `url` and `hash`, or a `path`.
3131+ // `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
3232+ // Once all dependencies are fetched, `zig build` no longer requires
3333+ // internet connectivity.
3434+ .dependencies = .{
3535+ // See `zig fetch --save <url>` for a command-line interface for adding dependencies.
3636+ //.example = .{
3737+ // // When updating this field to a new URL, be sure to delete the corresponding
3838+ // // `hash`, otherwise you are communicating that you expect to find the old hash at
3939+ // // the new URL. If the contents of a URL change this will result in a hash mismatch
4040+ // // which will prevent zig from using it.
4141+ // .url = "https://example.com/foo.tar.gz",
4242+ //
4343+ // // This is computed from the file contents of the directory of files that is
4444+ // // obtained after fetching `url` and applying the inclusion rules given by
4545+ // // `paths`.
4646+ // //
4747+ // // This field is the source of truth; packages do not come from a `url`; they
4848+ // // come from a `hash`. `url` is just one of many possible mirrors for how to
4949+ // // obtain a package matching this `hash`.
5050+ // //
5151+ // // Uses the [multihash](https://multiformats.io/multihash/) format.
5252+ // .hash = "...",
5353+ //
5454+ // // When this is provided, the package is found in a directory relative to the
5555+ // // build root. In this case the package's hash is irrelevant and therefore not
5656+ // // computed. This field and `url` are mutually exclusive.
5757+ // .path = "foo",
5858+ //
5959+ // // When this is set to `true`, a package is declared to be lazily
6060+ // // fetched. This makes the dependency only get fetched if it is
6161+ // // actually used.
6262+ // .lazy = false,
6363+ //},
6464+ },
6565+ // Specifies the set of files and directories that are included in this package.
6666+ // Only files and directories listed here are included in the `hash` that
6767+ // is computed for this package. Only files listed here will remain on disk
6868+ // when using the zig package manager. As a rule of thumb, one should list
6969+ // files required for compilation plus any license(s).
7070+ // Paths are relative to the build root. Use the empty string (`""`) to refer to
7171+ // the build root itself.
7272+ // A directory listed here means that all files within, recursively, are included.
7373+ .paths = .{
7474+ "build.zig",
7575+ "build.zig.zon",
7676+ "src",
7777+ // For example...
7878+ //"LICENSE",
7979+ //"README.md",
8080+ },
8181+}
+54
src/day01.zig
···11+const std = @import("std");
22+33+const data = @embedFile("01.txt");
44+55+pub fn main() !void {
66+ const result_1 = try part1(data);
77+ std.debug.print("{d}\n", .{result_1});
88+}
99+1010+// Count the number of times the dial lands on 0 (zero).
1111+fn part1(input: []const u8) !usize {
1212+ // Our starting point.
1313+ const start: i16 = 50;
1414+1515+ // Our current position.
1616+ var pos = start;
1717+1818+ // Number of times we have landed on zero.
1919+ var hit_zero: usize = 0;
2020+2121+ var iter = std.mem.tokenizeScalar(u8, input, '\n');
2222+ while (true) {
2323+ const line = iter.next() orelse break;
2424+ std.debug.print("POS {d}", .{pos});
2525+2626+ const direction = line[0];
2727+ const amount = try std.fmt.parseInt(i16, line[1..], 10);
2828+ std.debug.print(" {c} {d}", .{ direction, amount });
2929+3030+ const next = switch (direction) {
3131+ 'R' => pos + @mod(amount, 100),
3232+ 'L' => pos - @mod(amount, 100),
3333+ else => return error.InvalidDirection,
3434+ };
3535+ std.debug.print(" -> {d}", .{next});
3636+3737+ if (next < 0) {
3838+ pos = 100 - (next * -1);
3939+ } else if (next >= 100) {
4040+ pos = 0 + (next - 100);
4141+ } else {
4242+ pos = next;
4343+ }
4444+4545+ if (pos == 0) {
4646+ std.debug.print(" ZERO", .{});
4747+ hit_zero += 1;
4848+ }
4949+5050+ std.debug.print("\n", .{});
5151+ }
5252+5353+ return hit_zero;
5454+}
+27
src/main.zig
···11+const std = @import("std");
22+const advent_of_code_2025 = @import("advent_of_code_2025");
33+44+pub fn main() !void {
55+ // Prints to stderr, ignoring potential errors.
66+ std.debug.print("All your {s} are belong to us.\n", .{"codebase"});
77+ try advent_of_code_2025.bufferedPrint();
88+}
99+1010+test "simple test" {
1111+ const gpa = std.testing.allocator;
1212+ var list: std.ArrayList(i32) = .empty;
1313+ defer list.deinit(gpa); // Try commenting this out and see if zig detects the memory leak!
1414+ try list.append(gpa, 42);
1515+ try std.testing.expectEqual(@as(i32, 42), list.pop());
1616+}
1717+1818+test "fuzz example" {
1919+ const Context = struct {
2020+ fn testOne(context: @This(), input: []const u8) anyerror!void {
2121+ _ = context;
2222+ // Try passing `--fuzz` to `zig build test` and see if it manages to fail this test case!
2323+ try std.testing.expect(!std.mem.eql(u8, "canyoufindme", input));
2424+ }
2525+ };
2626+ try std.testing.fuzz(Context{}, Context.testOne, .{});
2727+}
+23
src/root.zig
···11+//! By convention, root.zig is the root source file when making a library.
22+const std = @import("std");
33+44+pub fn bufferedPrint() !void {
55+ // Stdout is for the actual output of your application, for example if you
66+ // are implementing gzip, then only the compressed bytes should be sent to
77+ // stdout, not any debugging messages.
88+ var stdout_buffer: [1024]u8 = undefined;
99+ var stdout_writer = std.fs.File.stdout().writer(&stdout_buffer);
1010+ const stdout = &stdout_writer.interface;
1111+1212+ try stdout.print("Run `zig build test` to run the tests.\n", .{});
1313+1414+ try stdout.flush(); // Don't forget to flush!
1515+}
1616+1717+pub fn add(a: i32, b: i32) i32 {
1818+ return a + b;
1919+}
2020+2121+test "basic add functionality" {
2222+ try std.testing.expect(add(3, 7) == 10);
2323+}