Created
December 1, 2022 20:23
-
-
Save benjamingr/f1ed7fe1edb9c3c070aeff75617b26c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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