diff --git a/src/days/day09.zig b/src/days/day09.zig index e85cb36..0a0044b 100644 --- a/src/days/day09.zig +++ b/src/days/day09.zig @@ -30,7 +30,7 @@ pub fn run(allocator: std.mem.Allocator) !void { const s = coordinates.next() orelse continue; break :blk try std.fmt.parseUnsigned(u64, s, 10); }; - try tiles.append(allocator, .{ .x = x, .y = y }); + try tiles.append(allocator, .{ .pos = .{ x, y } }); } var largest_area: usize = 0; @@ -58,13 +58,12 @@ pub fn run(allocator: std.mem.Allocator) !void { } const Tile = struct { - x: u64, - y: u64, + pos: @Vector(2, u64), const Self = @This(); pub fn format(self: Self, w: *std.io.Writer) std.io.Writer.Error!void { - try w.print("{d},{d}", .{self.x, self.y}); + try w.print("{d},{d}", .{self.pos[0], self.pos[1]}); } pub fn rectangle(self: Self, other_corner: Self) Rectangle { @@ -83,9 +82,12 @@ const Rectangle = struct { } pub fn area(self: Self) u64 { - const width = @max(self.a.x, self.b.x) - @min(self.a.x, self.b.x) + 1; - const height = @max(self.a.y, self.b.y) - @min(self.a.y, self.b.y) + 1; - return width * height; + const cond = self.a.pos > self.b.pos; + const high = @select(u64, cond, self.a.pos, self.b.pos); + const low = @select(u64, !cond, self.a.pos, self.b.pos); + const diff = high - low; + const one: @Vector(2, u64) = @splat(1); + return @reduce(.Mul, diff + one); } };