diff --git a/build.zig b/build.zig index ef38018..cea50dc 100644 --- a/build.zig +++ b/build.zig @@ -21,15 +21,12 @@ pub fn build(b: *std.Build) void { options.addOption(?[]const u8, "day_path", day_path); options.addOption(?u8, "day", day); - const lib = b.addStaticLibrary(.{ - .name = "aoc-2022", + const aoc_lib = b.addModule("aoc", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, }); - b.installArtifact(lib); - const exe = b.addExecutable(.{ .name = "aoc-2022", .root_source_file = b.path("src/main.zig"), @@ -37,6 +34,7 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); exe.root_module.addOptions("build-options", options); + exe.root_module.addImport("aoc", aoc_lib); b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); diff --git a/src/days/day.zig b/src/days/day.zig index 3064fc2..aaebfee 100644 --- a/src/days/day.zig +++ b/src/days/day.zig @@ -1,6 +1,8 @@ const std = @import("std"); const mem = std.mem; +const aoc = @import("aoc"); + pub fn run(alloc: mem.Allocator, progress: *std.Progress.Node) !void { _ = alloc; // autofix _ = progress; // autofix diff --git a/src/days/day1.zig b/src/days/day1.zig index 1d4e1ce..90e9928 100644 --- a/src/days/day1.zig +++ b/src/days/day1.zig @@ -1,6 +1,8 @@ const std = @import("std"); const mem = std.mem; +const aoc = @import("aoc"); + const input = @embedFile("inputs/day01.txt"); pub fn run(alloc: mem.Allocator, progress: *std.Progress.Node) !void { @@ -14,7 +16,8 @@ pub fn run(alloc: mem.Allocator, progress: *std.Progress.Node) !void { const n2 = progress.start("Running", 0); defer n2.end(); - std.mem.sort(u64, parsed.items, .{}, BiggerThan(u64).sort); + // std.mem.sort(u64, parsed.items, .{}, BiggerThan(u64).sort); + aoc.Sort.descending(u64, parsed.items); std.log.info("Result Part 1: {}", .{parsed.items[0]}); std.log.info("Result Part 2: {}", .{parsed.items[0] + parsed.items[1] + parsed.items[2]}); @@ -43,14 +46,6 @@ pub fn parse(alloc: mem.Allocator, buffer: []const u8) !std.ArrayList(u64) { return ret; } -fn BiggerThan(comptime T: type) type { - return struct { - fn sort(_: @TypeOf(.{}), lhs: T, rhs: T) bool { - return lhs > rhs; - } - }; -} - test "day 01" { const testing = std.testing; @@ -74,7 +69,7 @@ test "day 01" { const parsed = try parse(testing.allocator, test_input); defer parsed.deinit(); - std.mem.sort(u64, parsed.items, .{}, BiggerThan(u64).sort); + aoc.Sort.descending(u64, parsed.items); try testing.expectEqual(parsed.items[0], 24000); } diff --git a/src/days/day2.zig b/src/days/day2.zig new file mode 100644 index 0000000..aaebfee --- /dev/null +++ b/src/days/day2.zig @@ -0,0 +1,14 @@ +const std = @import("std"); +const mem = std.mem; + +const aoc = @import("aoc"); + +pub fn run(alloc: mem.Allocator, progress: *std.Progress.Node) !void { + _ = alloc; // autofix + _ = progress; // autofix +} + +test "day 00" { + const testing = std.testing; + _ = testing; // autofix +} diff --git a/src/root.zig b/src/root.zig index f034ca3..0826778 100644 --- a/src/root.zig +++ b/src/root.zig @@ -1,13 +1,29 @@ -//! By convention, root.zig is the root source file when making a library. If -//! you are making an executable, the convention is to delete this file and -//! start with main.zig instead. +//! Util library for the 2022 advent of code const std = @import("std"); -const testing = std.testing; -export fn add(a: i32, b: i32) i32 { - return a + b; -} +/// Sort a slice in place +pub const Sort = struct { + pub fn Ascending(comptime T: type) type { + return struct { + fn sort(_: @TypeOf(.{}), lhs: T, rhs: T) bool { + return lhs < rhs; + } + }; + } -test "basic add functionality" { - try testing.expect(add(3, 7) == 10); -} + pub fn ascending(comptime T: type, items: []T) void { + std.mem.sort(T, items, .{}, Ascending(T).sort); + } + + pub fn Descending(comptime T: type) type { + return struct { + fn sort(_: @TypeOf(.{}), lhs: T, rhs: T) bool { + return lhs > rhs; + } + }; + } + + pub fn descending(comptime T: type, items: []T) void { + std.mem.sort(T, items, .{}, Descending(T).sort); + } +};