From c316d355c3458aedec7c7cb3420ddba05888e705 Mon Sep 17 00:00:00 2001 From: Christian Segundo Date: Thu, 1 Dec 2022 10:04:05 +0100 Subject: day 1 --- day-1/main.zig | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 day-1/main.zig (limited to 'day-1/main.zig') diff --git a/day-1/main.zig b/day-1/main.zig new file mode 100644 index 0000000..b2760fb --- /dev/null +++ b/day-1/main.zig @@ -0,0 +1,34 @@ +const std = @import("std"); + +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); + defer allocator.free(file_buffer); + + var iter = std.mem.split(u8, file_buffer, "\n"); + var count: i32 = 0; + var max: i32 = 0; + + while (iter.next()) |line| { + if (line.len == 0) { + if (count > max) { + max = count; + } + count = 0; + } else { + count += try std.fmt.parseInt(i32, line, 0); + } + } + + std.debug.print("{d}\n", .{max}); +} -- cgit v1.2.3