diff options
-rw-r--r-- | day-1/main.zig | 11 | ||||
-rw-r--r-- | day-1/main_1.zig | 29 | ||||
l--------- | day-1/util | 1 | ||||
-rw-r--r-- | util/file.zig | 15 | ||||
-rw-r--r-- | util/mem.zig | 17 |
5 files changed, 39 insertions, 34 deletions
diff --git a/day-1/main.zig b/day-1/main.zig index b2760fb..fa89e3f 100644 --- a/day-1/main.zig +++ b/day-1/main.zig @@ -1,18 +1,11 @@ const std = @import("std"); +const slurp = @import("util/file.zig").slurp; var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); pub fn main() !void { - var path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined; - const path = try std.fs.realpath("./input", &path_buffer); - - const file = try std.fs.openFileAbsolute(path, .{}); - defer file.close(); - - const file_size = (try file.stat()).size; - - const file_buffer = try file.readToEndAlloc(allocator, file_size); + const file_buffer = try slurp(allocator, "./input"); defer allocator.free(file_buffer); var iter = std.mem.split(u8, file_buffer, "\n"); diff --git a/day-1/main_1.zig b/day-1/main_1.zig index 1a36e26..5e502d3 100644 --- a/day-1/main_1.zig +++ b/day-1/main_1.zig @@ -1,18 +1,12 @@ const std = @import("std"); +const slurp = @import("util/file.zig").slurp; +const min_idx = @import("util/mem.zig").min_idx; var gpa = std.heap.GeneralPurposeAllocator(.{}){}; const allocator = gpa.allocator(); pub fn main() !void { - var path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined; - const path = try std.fs.realpath("./input", &path_buffer); - - const file = try std.fs.openFileAbsolute(path, .{}); - defer file.close(); - - const file_size = (try file.stat()).size; - - const file_buffer = try file.readToEndAlloc(allocator, file_size); + const file_buffer = try slurp(allocator, "./input"); defer allocator.free(file_buffer); var iter = std.mem.split(u8, file_buffer, "\n"); @@ -21,7 +15,7 @@ pub fn main() !void { while (iter.next()) |line| { if (line.len == 0) { - const lowest_u = lowest(i32, &max); + const lowest_u = min_idx(i32, &max); if (count > max[lowest_u]) { max[lowest_u] = count; } @@ -38,18 +32,3 @@ pub fn main() !void { std.debug.print("{d}\n", .{count}); } - -fn lowest(comptime T: type, items: []const T) usize { - var lowest_u: usize = 0; - var previous: T = items[0]; - - for (items) |item, idx| { - if (item < previous) { - lowest_u = idx; - } - - previous = item; - } - - return lowest_u; -} diff --git a/day-1/util b/day-1/util new file mode 120000 index 0000000..40c3fc5 --- /dev/null +++ b/day-1/util @@ -0,0 +1 @@ +../util
\ No newline at end of file diff --git a/util/file.zig b/util/file.zig new file mode 100644 index 0000000..90849e9 --- /dev/null +++ b/util/file.zig @@ -0,0 +1,15 @@ +const std = @import("std"); + +/// Reads an entire file into memory, caller owns the returned slice. +pub fn slurp(allocator: std.mem.Allocator, file_path: []const u8) ![]u8 { + var path_buffer: [std.fs.MAX_PATH_BYTES]u8 = undefined; + const path = try std.fs.realpath(file_path, &path_buffer); + + const file = try std.fs.openFileAbsolute(path, .{}); + defer file.close(); + + return try file.readToEndAlloc( + allocator, + (try file.stat()).size, + ); +} diff --git a/util/mem.zig b/util/mem.zig new file mode 100644 index 0000000..89ba67b --- /dev/null +++ b/util/mem.zig @@ -0,0 +1,17 @@ +const std = @import("std"); +const math = std.math; + +/// Returns the position of the smallest number in a slice. +pub fn min_idx(comptime T: type, slice: []const T) usize { + var best = slice[0]; + var idx: usize = 0; + + for (slice[1..]) |item, i| { + const possible_best = math.min(best, item); + if (best > possible_best) { + best = possible_best; + idx = i + 1; + } + } + return idx; +} |