summaryrefslogtreecommitdiff
path: root/src/day-9/part-2.zig
diff options
context:
space:
mode:
authorame <[email protected]>2025-12-09 04:49:02 -0600
committerame <[email protected]>2025-12-09 04:49:02 -0600
commit8190c46ee0b8684e7457b7891b8dfd352b3967e3 (patch)
treef6b1cf725fe7ae095327f74349f406e285945521 /src/day-9/part-2.zig
parent7ecb12636f40d143a87db5966f53e5adecd57319 (diff)
Diffstat (limited to 'src/day-9/part-2.zig')
-rw-r--r--src/day-9/part-2.zig68
1 files changed, 68 insertions, 0 deletions
diff --git a/src/day-9/part-2.zig b/src/day-9/part-2.zig
new file mode 100644
index 0000000..28898e2
--- /dev/null
+++ b/src/day-9/part-2.zig
@@ -0,0 +1,68 @@
+const std = @import("std");
+
+const point = struct { x: i64, y: i64 };
+
+pub fn main() !void {
+ var gpa = std.heap.GeneralPurposeAllocator(.{}){};
+ defer _ = gpa.deinit();
+ const alloc = gpa.allocator();
+
+ const path = "input.txt";
+ const fp = try std.fs.cwd().openFile(path, .{.mode = std.fs.File.OpenMode.read_only});
+ defer fp.close();
+ var reader = fp.reader();
+
+ var buffer: [256]u8 = undefined;
+
+ var points = std.ArrayList(point).init(alloc);
+ defer points.deinit();
+
+ while(try reader.readUntilDelimiterOrEof(&buffer, '\n')) |line| {
+ const i = std.mem.indexOf(u8, line, ",").?;
+
+ const p = point{
+ .x = try std.fmt.parseInt(i64, line[0..i], 10),
+ .y = try std.fmt.parseInt(i64, line[i+1..], 10)
+ };
+
+ try points.append(p);
+ }
+
+ var largest: u64 = 0;
+
+ for(0..points.items.len) |i| {
+ for(i..points.items.len) |z| {
+ if(i == z) continue;
+
+ const vol = (1 + @abs(points.items[i].x - points.items[z].x)) * (1 + @abs(points.items[i].y - points.items[z].y));
+ if(vol > largest) {
+ const len: i64 = @intCast(@abs(points.items[i].x - points.items[z].x));
+ const high: i64 = @intCast(@abs(points.items[i].y - points.items[z].y));
+
+ const a: i64 = @min(points.items[i].x, points.items[z].x);
+ const b: i64 = a + len;
+ const c: i64 = @min(points.items[i].y, points.items[z].y);
+ const d: i64 = c + high;
+
+ var pass: bool = true;
+ //check if any line intersects with this box, so glad this worked
+ for(0..points.items.len, 1..points.items.len + 1) |o,s| {
+ const item1 = points.items[o];
+ const item2 = if(points.items.len > s) points.items[s] else points.items[0];
+ const verticle = item1.x == item2.x;
+
+ if(verticle and (a < item1.x and item1.x < b) and !(item1.y >= d and item2.y >= d or item1.y <= c and item2.y <= c)){
+ pass = false;
+ break;
+ } else if(!verticle and (c < item1.y and item1.y < d) and !(item1.x >= b and item2.x >= b or item1.x <= a and item2.x <= a)){
+ pass = false;
+ break;
+ }
+ }
+ if(pass) largest = vol;
+ }
+ }
+ }
+
+ std.debug.print("{}\n", .{largest});
+}