started util stuff
This commit is contained in:
parent
a8391360e2
commit
996b2fb51f
5 changed files with 49 additions and 24 deletions
|
@ -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);
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
14
src/days/day2.zig
Normal file
14
src/days/day2.zig
Normal file
|
@ -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
|
||||
}
|
36
src/root.zig
36
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);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue