Skip to content

Instantly share code, notes, and snippets.

@benjamingr
Created December 1, 2022 20:23
Show Gist options
  • Save benjamingr/cf3213449fc68d88af4ab39a8c290dfd to your computer and use it in GitHub Desktop.
Save benjamingr/cf3213449fc68d88af4ab39a8c290dfd to your computer and use it in GitHub Desktop.

Revisions

  1. benjamingr created this gist Dec 1, 2022.
    36 changes: 36 additions & 0 deletions isuck.zig
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    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});
    }