aoc2022/day1/puzzle2.zig

42 lines
1.1 KiB
Zig

const std = @import("std");
pub fn main() !void {
// read the input file
const input = try std.fs.cwd().openFile("input", .{});
defer input.close();
var buf_reader = std.io.bufferedReader(input.reader());
var in_stream = buf_reader.reader();
var buf: [1024]u8 = undefined;
var cal : i32 = 0;
var max_cal : [3]i32 = .{0, 0, 0};
while (try in_stream.readUntilDelimiterOrEof(&buf, '\n')) |line| {
for (line) |char| {
if (!std.ascii.isWhitespace(char)) { // Contains a number
cal += std.fmt.parseInt(i32, line, 10) catch break;
break; // else branch not evaluated
}
} else { // All characters are whitelines
for (max_cal) |*max_i| {
if (cal > max_i.*) {
const aux = max_i.*;
max_i.* = cal;
cal = aux;
}
}
cal = 0;
}
}
var max_total : i32 = 0;
for (max_cal) |max_i| {
std.debug.print("Max : {}\n", .{max_i});
max_total += max_i;
}
std.debug.print("Maximum Calories: {}\n", .{max_total});
}