diff --git a/src/days/day12.zig b/src/days/day12.zig index d940d18..26d586b 100644 --- a/src/days/day12.zig +++ b/src/days/day12.zig @@ -111,13 +111,26 @@ pub fn run(allocator: std.mem.Allocator) !void { } } - for (presents.items, 0..) |present, i| { - std.debug.print("{d}:\n{f}\n\n", .{i, present}); - } + var accumulator: usize = 0; for (regions.items) |region| { - std.debug.print("{f}\n", .{region}); + var area_needed: usize = 0; + for (region.required_presents, 0..) |amount, i| { + area_needed += amount * presents.items[i].area(); + } + if (area_needed < region.area()) { + std.debug.print("{f}\nfits\n", .{region}); + accumulator += 1; + } } + + var buffer: [8]u8 = undefined; + var stdout_writer = std.fs.File.stdout().writer(&buffer); + const stdout = &stdout_writer.interface; + + try stdout.print("{d}\n", .{accumulator}); + + try stdout.flush(); } const Present = struct { @@ -147,6 +160,10 @@ const Present = struct { const index = (y * Width) + x; return self.shape.isSet(index); } + + pub fn area(self: Self) usize { + return self.shape.count(); + } }; const Region = struct { @@ -169,5 +186,54 @@ const Region = struct { } } } + + pub fn createEmpty(self: Self, allocator: std.mem.Allocator) !EmptyRegion { + return try .init(allocator, self.width, self.height); + } + + pub fn area(self: Self) usize { + return self.width * self.height; + } +}; + +const EmptyRegion = struct { + grid: [][]bool, + filled_spaces: usize, + + allocator: std.mem.Allocator, + + const Self = @This(); + + pub fn init(allocator: std.mem.Allocator, width: u8, height: u8) !Self { + const h = try allocator.alloc([]bool, height); + for (0..height) |i| { + const w = try allocator.alloc(u8, width); + @memset(w, false); + h[i] = w; + } + return .{ + .grid = h, + .filled_spaces = 0, + .allocator = allocator, + }; + } + + pub fn deinit(self: Self) void { + for (self.grid) |row| { + self.allocator.free(row); + } + self.allocator.free(self.grid); + } + + pub fn fits(self: Self, present: Present) bool { + std.debug.assert(self.grid.len > 0); + const size = self.grid.len * self.grid[0].len; + if ((size - self.filled_spaces) < present.shape.count()) { + return false; + } + + // TODO: Implementation + return false; + } };