aoc2015/day2_p1.zig
2024-12-07 23:51:58 +01:00

32 lines
1,018 B
Zig

const std = @import("std");
pub fn main() !void {
const stdin = std.io.getStdIn();
var buf_io = std.io.bufferedReader(stdin.reader());
var reader = buf_io.reader();
var buf = [_]u8{0} ** 1024;
var total: usize = 0;
while (try reader.readUntilDelimiterOrEof(&buf, '\n')) |line| {
const trimmed = std.mem.trim(u8, line, "\n\r");
var it = std.mem.splitScalar(u8, trimmed, 'x');
const l = try std.fmt.parseInt(u32, it.first(), 10);
const w = try std.fmt.parseInt(u32, it.next().?, 10);
const h = try std.fmt.parseInt(u32, it.next().?, 10);
total += calculate_sqft(l, w, h);
}
std.debug.print("{}", .{total});
}
fn calculate_sqft(l: u32, w: u32, h: u32) u32 {
const coverage = 2 * l * w + 2 * w * h + 2 * h * l;
const paper = @min(l * w, w * h, h * l);
return coverage + paper;
}
test "day-test" {
try std.testing.expectEqual(calculate_sqft(2, 3, 4), 58);
try std.testing.expectEqual(calculate_sqft(1, 1, 10), 43);
}