diff --git a/day2_p1.zig b/day2_p1.zig new file mode 100644 index 0000000..2674a89 --- /dev/null +++ b/day2_p1.zig @@ -0,0 +1,32 @@ +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); +} diff --git a/day2_p2.zig b/day2_p2.zig new file mode 100644 index 0000000..0ad0e61 --- /dev/null +++ b/day2_p2.zig @@ -0,0 +1,33 @@ +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); +}