Skip to content

Instantly share code, notes, and snippets.

@benjamingr
Created December 1, 2022 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benjamingr/cf3213449fc68d88af4ab39a8c290dfd to your computer and use it in GitHub Desktop.
Save benjamingr/cf3213449fc68d88af4ab39a8c290dfd to your computer and use it in GitHub Desktop.
const std = @import("std");
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = &gpa.allocator();
var file = try std.fs.cwd().openFile("./sample.txt", .{});
const file_content = try file.readToEndAlloc(allocator.*, 1024 * 1024); // 1MB max read size
var iter = std.mem.split(u8, file_content, "\n");
defer allocator.free(file_content);
var value = iter.next();
var max:i32 = 0;
var current:i32 = 0;
while(value != null) {
if (value.?.len == 0) {
if (current > max) {
max = current;
std.debug.print("new max! {d}\n", .{max});
}
current = 0;
std.debug.print("new elf!\n", .{});
value = iter.next();
continue;
}
const num = try std.fmt.parseInt(i32, value.?, 10);
current += num;
std.debug.print("{any}\n", .{num});
value = iter.next();
}
if (current > max) {
max = current;
}
std.debug.print("Max! {d}\n", .{max});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment