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

33 lines
1 KiB
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_ribbon(l, w, h);
}
std.debug.print("{}", .{total});
}
fn calculate_ribbon(l: u32, w: u32, h: u32) u32 {
const wrapL = l + l + w + w;
const wrapW = w + w + h + h;
const wrapH = l + l + h + h;
return @min(wrapL, wrapW, wrapH) + l * w * h;
}
test "day-test" {
try std.testing.expectEqual(calculate_ribbon(2, 3, 4), 34);
try std.testing.expectEqual(calculate_ribbon(1, 1, 10), 14);
}