diff --git a/.gitignore b/.gitignore index 7e99e36..d8a6d46 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -*.pyc \ No newline at end of file +*.pyc +.zig-cache +zig-out/ \ No newline at end of file diff --git a/src/day1.py b/python/day1.py similarity index 100% rename from src/day1.py rename to python/day1.py diff --git a/src/utils/load.py b/python/utils/load.py similarity index 100% rename from src/utils/load.py rename to python/utils/load.py diff --git a/zig/build.zig b/zig/build.zig new file mode 100644 index 0000000..ea42a43 --- /dev/null +++ b/zig/build.zig @@ -0,0 +1,59 @@ +const std = @import("std"); + +// Although this function looks imperative, it does not perform the build +// directly and instead it mutates the build graph (`b`) that will be then +// executed by an external runner. The functions in `std.Build` implement a DSL +// for defining build steps and express dependencies between them, allowing the +// build runner to parallelize the build automatically (and the cache system to +// know when a step doesn't need to be re-run). +pub fn build(b: *std.Build) void { + // Standard target options allow the person running `zig build` to choose + // what target to build for. Here we do not override the defaults, which + // means any target is allowed, and the default is native. Other options + // for restricting supported target set are available. + const target = b.standardTargetOptions(.{}); + // Standard optimization options allow the person running `zig build` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not + // set a preferred release mode, allowing the user to decide how to optimize. + const optimize = b.standardOptimizeOption(.{}); + // It's also possible to define more custom flags to toggle optional features + // of this build script using `b.option()`. All defined flags (including + // target and optimize options) will be listed when running `zig build --help` + // in this directory. + + const exe = b.addExecutable(.{ + .name = "zig", + .root_module = b.createModule(.{ + .root_source_file = b.path("src/main.zig"), + .target = target, + .optimize = optimize, + .imports = &.{}, + }), + }); + + b.installArtifact(exe); + + const run_step = b.step("run", "Run the app"); + + const run_cmd = b.addRunArtifact(exe); + run_step.dependOn(&run_cmd.step); + + run_cmd.step.dependOn(b.getInstallStep()); + + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const exe_tests = b.addTest(.{ + .root_module = exe.root_module, + }); + + // A run step that will run the second test executable. + const run_exe_tests = b.addRunArtifact(exe_tests); + + // A top level step for running all tests. dependOn can be called multiple + // times and since the two run steps do not depend on one another, this will + // make the two of them run in parallel. + const test_step = b.step("test", "Run tests"); + test_step.dependOn(&run_exe_tests.step); +} diff --git a/zig/build.zig.zon b/zig/build.zig.zon new file mode 100644 index 0000000..014bd18 --- /dev/null +++ b/zig/build.zig.zon @@ -0,0 +1,81 @@ +.{ + // 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 = .zig, + // This is a [Semantic Version](https://semver.org/). + // In a future version of Zig it will be used for package deduplication. + .version = "0.0.0", + // 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 = 0xc1ce10815a5d4f65, // 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.15.2", + // This field is optional. + // Each dependency must either provide a `url` and `hash`, or a `path`. + // `zig build --fetch` can be used to fetch all dependencies of a package, recursively. + // Once all dependencies are fetched, `zig build` no longer requires + // internet connectivity. + .dependencies = .{ + // See `zig fetch --save ` for a command-line interface for adding dependencies. + //.example = .{ + // // When updating this field to a new URL, be sure to delete the corresponding + // // `hash`, otherwise you are communicating that you expect to find the old hash at + // // the new URL. If the contents of a URL change this will result in a hash mismatch + // // which will prevent zig from using it. + // .url = "https://example.com/foo.tar.gz", + // + // // This is computed from the file contents of the directory of files that is + // // obtained after fetching `url` and applying the inclusion rules given by + // // `paths`. + // // + // // This field is the source of truth; packages do not come from a `url`; they + // // come from a `hash`. `url` is just one of many possible mirrors for how to + // // obtain a package matching this `hash`. + // // + // // Uses the [multihash](https://multiformats.io/multihash/) format. + // .hash = "...", + // + // // When this is provided, the package is found in a directory relative to the + // // build root. In this case the package's hash is irrelevant and therefore not + // // computed. This field and `url` are mutually exclusive. + // .path = "foo", + // + // // When this is set to `true`, a package is declared to be lazily + // // fetched. This makes the dependency only get fetched if it is + // // actually used. + // .lazy = false, + //}, + }, + // 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", + // For example... + //"LICENSE", + //"README.md", + }, +} diff --git a/zig/src/days/day01.zig b/zig/src/days/day01.zig new file mode 100644 index 0000000..1c64655 --- /dev/null +++ b/zig/src/days/day01.zig @@ -0,0 +1,13 @@ +const std = @import("std"); +const debug = std.debug; +const day = @import("../utils/day.zig"); + +pub const Day = struct { + pub const nb = 1; + pub fn part1(_: []const u8) void { + debug.print("hello from day01 - part 1", .{}); + } + pub fn part2(_: []const u8) void { + debug.print("hello from day01 - part 2", .{}); + } +}; diff --git a/zig/src/main.zig b/zig/src/main.zig new file mode 100644 index 0000000..854189a --- /dev/null +++ b/zig/src/main.zig @@ -0,0 +1,40 @@ +const std = @import("std"); +const debug = std.debug; +const Day = @import("utils/day.zig").Day; +const Map = std.AutoHashMap(usize, Day); + +const import_days = struct { + pub const day01 = @import("days/day01.zig"); +}; + +pub fn main() !void { + var gpa = std.heap.GeneralPurposeAllocator(.{}){}; + defer _ = gpa.deinit(); + const alloc = gpa.allocator(); + + const args = try std.process.argsAlloc(alloc); + defer std.process.argsFree(alloc, args); + if (args.len < 2) return error.InvalidArgs; + + const arg_day_nb = try std.fmt.parseInt(usize, args[1], 10); + var days = Map.init(alloc); + defer days.deinit(); + + debug.print("Running day {d}\n", .{arg_day_nb}); + inline for (comptime std.meta.declarations(import_days)) |decl| { + const day_str = decl.name[3..]; // Skip "day" prefix + const day_number = try std.fmt.parseInt(usize, day_str, 10); + + if (day_number == arg_day_nb) { + const imported_day = @field(import_days, decl.name).Day; + const day = Day{ .nb = imported_day.nb, .part1 = imported_day.part1, .part2 = imported_day.part2 }; + + // TODO: get the correct input values + const buffer = "coucou"; + day.part1(buffer); + day.part2(buffer); + + break; + } + } +} diff --git a/zig/src/utils/day.zig b/zig/src/utils/day.zig new file mode 100644 index 0000000..0c021fa --- /dev/null +++ b/zig/src/utils/day.zig @@ -0,0 +1,8 @@ +const std = @import("std"); + +const fn_part_run = *const fn (str_buffer: []const u8) void; +pub const Day = struct { + nb: u32 = 0, + part1: fn_part_run, + part2: fn_part_run, +}; diff --git a/zig/src/utils/input.zig b/zig/src/utils/input.zig new file mode 100644 index 0000000..801a399 --- /dev/null +++ b/zig/src/utils/input.zig @@ -0,0 +1,8 @@ +const std = @import("std"); + +pub fn read_input(day_nb: comptime_int, alloc: std.mem.Allocator) void { + var path = "../../inputs/day01.txt"; + const file = try std.fs.cwd().readFile( + path, + ); +}