fn readFileIntoArrayList(allocator: std.mem.Allocator, array_list: *std.ArrayList([]const u8), filename: []const u8) !void {
var file = try std.fs.cwd().openFile(filename, .{});
defer file.close();
var file_contents = try file.reader().readAllAlloc(allocator, 1024 * 1024);
defer allocator.free(file_contents); // without this line the code runs fine but comlpains about a memory leak
var split = std.mem.split(u8, file_contents, "\n");
while (split.next()) |line| {
try array_list.append(line);
}
}
test "day 1 - part 1" {
const allocator = std.testing.allocator;
var file_contents = std.ArrayList([]const u8).init(allocator);
defer file_contents.deinit();
try readFileIntoArrayList(allocator, &file_contents, "./aoc-input/1_1.txt");
var index: usize = 0;
var max: u32 = 0;
var currentSum: u32 = 0;
while (index < file_contents.items.len) {
var line = file_contents.items[index];
index = index + 1;
if (line.len != 0) {
var value = try std.fmt.parseInt(u32, line, 10);
currentSum = currentSum + value;
} else {
if (max < currentSum) {
max = currentSum;
}
currentSum = 0;
}
}
std.debug.print("{d}\n", .{max});
}