diff options
author | Christian Segundo | 2022-12-03 19:28:32 +0100 |
---|---|---|
committer | Christian Segundo | 2022-12-03 19:28:32 +0100 |
commit | ee6c6e975dabc6c14f7e8e62e2de1551a431cc7a (patch) | |
tree | 834829061b796f3e6168ec1d6b7e1e8ff4469934 /day-1/main.zig | |
parent | 78fa87cd43cd18f2f9ec4a04c45bcb8036143fd8 (diff) | |
download | advent-of-zig-2022-ee6c6e975dabc6c14f7e8e62e2de1551a431cc7a.tar.gz |
refactor to use a meta test
Diffstat (limited to 'day-1/main.zig')
-rw-r--r-- | day-1/main.zig | 39 |
1 files changed, 29 insertions, 10 deletions
diff --git a/day-1/main.zig b/day-1/main.zig index fa89e3f..0af8162 100644 --- a/day-1/main.zig +++ b/day-1/main.zig @@ -1,14 +1,8 @@ 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 { - const file_buffer = try slurp(allocator, "./input"); - defer allocator.free(file_buffer); - - var iter = std.mem.split(u8, file_buffer, "\n"); +pub fn puzzle_1(input: []u8) !i32 { + var iter = std.mem.split(u8, input, "\n"); var count: i32 = 0; var max: i32 = 0; @@ -23,5 +17,30 @@ pub fn main() !void { } } - std.debug.print("{d}\n", .{max}); + return max; +} + +pub fn puzzle_2(input: []u8) !i32 { + var iter = std.mem.split(u8, input, "\n"); + var count: i32 = 0; + var max: [3]i32 = std.mem.zeroes([3]i32); + + while (iter.next()) |line| { + if (line.len == 0) { + const lowest_u = min_idx(i32, &max); + if (count > max[lowest_u]) { + max[lowest_u] = count; + } + count = 0; + } else { + count += try std.fmt.parseInt(i32, line, 0); + } + } + + count = 0; + for (max) |v| { + count += v; + } + + return count; } |