This commit is contained in:
Hamcha 2024-12-07 23:51:58 +01:00
parent 1d8c17bb7a
commit d7a5477430
Signed by: hamcha
GPG key ID: 1669C533B8CF6D89
2 changed files with 65 additions and 0 deletions

32
day2_p1.zig Normal file
View file

@ -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);
}

33
day2_p2.zig Normal file
View file

@ -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);
}