diff --git a/.gitignore b/.gitignore index c313b0d..3639c96 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ # ---> Zig -zig-cache/ +.zig-cache/ zig-out/ build/ build-*/ diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..8bf641a --- /dev/null +++ b/build.zig @@ -0,0 +1,75 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) !void { + const allocator = b.allocator; + + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const run_all = b.step("run", "Run all days"); + const test_all = b.step("test", "Test all days"); + const check = b.step("check", "run checks"); + + const utils_mod = b.createModule(.{ .root_source_file = b.path("src/utils/mod.zig") }); + + var dirIt = (try std.fs.cwd().openDir( + "./src/days", + .{ .iterate = true }, + )).iterate(); + + while (try dirIt.next()) |entry| { + if (entry.kind == .directory) {} + // each day entry will have a main.zig + const source_file = try std.fs.path.join(allocator, &.{ "src", "days", entry.name, "main.zig" }); + defer allocator.free(source_file); + //make sure the main.zig exists + _ = std.fs.cwd().openFile(source_file, .{}) catch continue; + + const exe = b.addExecutable(.{ + .name = entry.name, + .root_source_file = b.path(source_file), + .target = target, + .optimize = optimize, + }); + exe.root_module.addImport("utils", utils_mod); + + b.installArtifact(exe); + const install_cmd = b.addInstallArtifact(exe, .{}); + const install_step = b.step( + b.fmt("install_{s}", .{entry.name}), + b.fmt("install {s}", .{entry.name}), + ); + install_step.dependOn(&install_cmd.step); + b.installArtifact(exe); + + const run_cmd = b.addRunArtifact(exe); + const run_step = b.step( + b.fmt("run_{s}", .{entry.name}), + b.fmt("run {s}", .{entry.name}), + ); + run_step.dependOn(&run_cmd.step); + run_all.dependOn(&run_cmd.step); + + const exe_test = b.addTest(.{ + .root_source_file = b.path(source_file), + .target = target, + .optimize = optimize, + }); + const test_cmd = b.addRunArtifact(exe_test); + const test_step = b.step( + b.fmt("test_{s}", .{entry.name}), + b.fmt("test {s}", .{entry.name}), + ); + test_step.dependOn(&test_cmd.step); + test_all.dependOn(&test_cmd.step); + + const exe_check = b.addExecutable(.{ + .name = entry.name, + .root_source_file = b.path(source_file), + .target = target, + .optimize = optimize, + }); + exe_check.root_module.addImport("utils", utils_mod); + check.dependOn(&exe_check.step); + } +} diff --git a/build.zig.zon b/build.zig.zon new file mode 100644 index 0000000..7f01cca --- /dev/null +++ b/build.zig.zon @@ -0,0 +1,46 @@ +.{ + // This is the default name used by packages depending on this one. For + // example, when a user runs `zig fetch --save `, this field is used + // as the key in the `dependencies` table. Although the user can choose a + // different name, most users will stick with this provided value. + // + // It is redundant to include "zig" in this name because it is already + // within the Zig package namespace. + .name = .aoc2024, + + // This is a [Semantic Version](https://semver.org/). + // In a future version of Zig it will be used for package deduplication. + .version = "0.0.1", + + // Together with name, this represents a globally unique package + // identifier. This field is generated by the Zig toolchain when the + // package is first created, and then *never changes*. This allows + // unambiguous detection of one package being an updated version of + // another. + // + // When forking a Zig project, this id should be regenerated (delete the + // field and run `zig build`) if the upstream project is still maintained. + // Otherwise, the fork is *hostile*, attempting to take control over the + // original project's identity. Thus it is recommended to leave the comment + // on the following line intact, so that it shows up in code reviews that + // modify the field. + .fingerprint = 0x25105d4386327470, // Changing this has security and trust implications. + + // Tracks the earliest Zig version that the package considers to be a + // supported use case. + .minimum_zig_version = "0.14.0", + + // Specifies the set of files and directories that are included in this package. + // Only files and directories listed here are included in the `hash` that + // is computed for this package. Only files listed here will remain on disk + // when using the zig package manager. As a rule of thumb, one should list + // files required for compilation plus any license(s). + // Paths are relative to the build root. Use the empty string (`""`) to refer to + // the build root itself. + // A directory listed here means that all files within, recursively, are included. + .paths = .{ + "build.zig", + "build.zig.zon", + "src", + }, +} diff --git a/src/days/template/input.txt b/src/days/template/input.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/days/template/lib.zig b/src/days/template/lib.zig new file mode 100644 index 0000000..95a0b68 --- /dev/null +++ b/src/days/template/lib.zig @@ -0,0 +1 @@ +const std = @import("std"); diff --git a/src/days/template/main.zig b/src/days/template/main.zig new file mode 100644 index 0000000..8c0f96f --- /dev/null +++ b/src/days/template/main.zig @@ -0,0 +1,10 @@ +const std = @import("std"); +const part1 = @import("part1.zig"); +const part2 = @import("part2.zig"); +const input = @embedFile("input.txt"); + +pub fn main() !void { + const stdout = std.io.getStdErr().writer(); + try stdout.print("Part1: {d}\n", .{part1.solve(input)}); + try stdout.print("Part2: {d}\n", .{part2.solve(input)}); +} diff --git a/src/days/template/part1.zig b/src/days/template/part1.zig new file mode 100644 index 0000000..2ded691 --- /dev/null +++ b/src/days/template/part1.zig @@ -0,0 +1,14 @@ +const lib = @import("lib.zig"); +const std = @import("std"); +const utils = @import("utils"); + +pub fn solve(comptime input: []const u8) i32 { + return input.len; +} + +test "part1 sample" { + const input = + \\0 + ; + try std.testing.expectEqual(solve(input), 1); +} diff --git a/src/days/template/part2.zig b/src/days/template/part2.zig new file mode 100644 index 0000000..30680d9 --- /dev/null +++ b/src/days/template/part2.zig @@ -0,0 +1,14 @@ +const lib = @import("lib.zig"); +const std = @import("std"); +const utils = @import("utils"); + +pub fn solve(comptime input: []const u8) i32 { + return input.len; +} + +test "part2 sample" { + const input = + \\0 + ; + try std.testing.expectEqual(solve(input), 1); +} diff --git a/src/utils/mod.zig b/src/utils/mod.zig new file mode 100644 index 0000000..95a0b68 --- /dev/null +++ b/src/utils/mod.zig @@ -0,0 +1 @@ +const std = @import("std");