Files
aoc2024/12/src/main.zig
2024-12-12 00:28:39 -06:00

270 lines
7.2 KiB
Zig

// wow! i am bad at code. shitty day i just wanted it to Work
// awful way to do it especially in part 2 what was i even doing
const std = @import("std");
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
const alloc = arena.allocator();
const test_data = @embedFile("test");
const input_data = @embedFile("input");
pub fn main() !void {
const stdout = std.io.getStdOut().writer();
const value1 = try run1(input_data);
try stdout.print("{d}\n", .{value1});
const value2 = try run2(input_data);
try stdout.print("{d}\n", .{value2});
}
test "test input part 1" {
std.debug.print("\n", .{});
const value = try run1(test_data);
std.debug.print("{d}\n", .{value});
try std.testing.expectEqual(1930, value);
}
test "test input part 2" {
std.debug.print("\n",.{});
const value = try run2(test_data);
std.debug.print("{d}\n", .{value});
try std.testing.expectEqual(1206, value);
}
fn parseIntFromReader(reader: anytype) anyerror!u64 {
const f = try reader.readByte();
if (f < '0' or f > '9') return error.BadCharacter;
var v: u64 = f - '0';
while (true) {
const c = try reader.readByte();
if (c < '0' or c > '9') {
break;
} else {
v = (v * 10) + (c - '0');
}
}
return v;
}
var r2map: [256][256]u8 = [_][256]u8{[_]u8{0}**256}**256;
var r2done: [256][256]u8 = [_][256]u8{[_]u8{0}**256}**256;
var r2edges: [256][256]u8 = [_][256]u8{[_]u8{0}**256}**256;
var r2dim: usize = undefined;
fn run2_r(y: usize, x: usize) void {
const c = r2map[y][x];
r2done[y][x] = 1;
if (y > 0 and r2map[y-1][x] == c) {
if (r2done[y-1][x] == 0) run2_r(y-1, x);
} else {
r2edges[y][x] |= 0b1;
}
if (x > 0 and r2map[y][x-1] == c) {
if (r2done[y][x-1] == 0) run2_r(y, x-1);
} else {
r2edges[y][x] |= 0b10;
}
if (y < r2dim-1 and r2map[y+1][x] == c) {
if (r2done[y+1][x] == 0) run2_r(y+1, x);
} else {
r2edges[y][x] |= 0b100;
}
if (x < r2dim-1 and r2map[y][x+1] == c) {
if (r2done[y][x+1] == 0) run2_r(y, x+1);
} else {
r2edges[y][x] |= 0b1000;
}
}
fn run2_r2(y: usize, x: usize, area: *u64, perimeter: *u64) void {
const c = r2map[y][x];
r2done[y][x] = 2;
area.* += 1;
// top edge
if (r2edges[y][x] & 0b1 != 0) {
perimeter.* += 1;
var ix = x;
while (ix >= 0) : (ix -= 1) {
if (r2map[y][ix] != c) break;
if (r2edges[y][ix] & 0b1 == 0) break;
r2edges[y][ix] &= ~@as(u8,0b1);
if (ix == 0) break;
}
ix = x+1;
while (ix < r2dim) : (ix += 1) {
if (r2map[y][ix] != c) break;
if (r2edges[y][ix] & 0b1 == 0) break;
r2edges[y][ix] &= ~@as(u8,0b1);
}
}
// bottom edge
if (r2edges[y][x] & 0b100 != 0) {
perimeter.* += 1;
var ix = x;
while (ix >= 0) : (ix -= 1) {
if (r2map[y][ix] != c) break;
if (r2edges[y][ix] & 0b100 == 0) break;
r2edges[y][ix] &= ~@as(u8,0b100);
if (ix == 0) break;
}
ix = x+1;
while (ix < r2dim) : (ix += 1) {
if (r2map[y][ix] != c) break;
if (r2edges[y][ix] & 0b100 == 0) break;
r2edges[y][ix] &= ~@as(u8,0b100);
}
}
// left edge
if (r2edges[y][x] & 0b10 != 0) {
perimeter.* += 1;
var iy = y;
while (iy >= 0) : (iy -= 1) {
if (r2map[iy][x] != c) break;
if (r2edges[iy][x] & 0b10 == 0) break;
r2edges[iy][x] &= ~@as(u8,0b10);
if (iy == 0) break;
}
iy = y+1;
while (iy < r2dim) : (iy += 1) {
if (r2map[iy][x] != c) break;
if (r2edges[iy][x] & 0b10 == 0) break;
r2edges[iy][x] &= ~@as(u8,0b10);
}
}
// right edge
if (r2edges[y][x] & 0b1000 != 0) {
perimeter.* += 1;
var iy = y;
while (iy >= 0) : (iy -= 1) {
if (r2map[iy][x] != c) break;
if (r2edges[iy][x] & 0b1000 == 0) break;
r2edges[iy][x] &= ~@as(u8,0b1000);
if (iy == 0) break;
}
iy = y+1;
while (iy < r2dim) : (iy += 1) {
if (r2map[iy][x] != c) break;
if (r2edges[iy][x] & 0b1000 == 0) break;
r2edges[iy][x] &= ~@as(u8,0b1000);
}
}
if (y > 0 and r2map[y-1][x] == c) {
if (r2done[y-1][x] == 1) run2_r2(y-1, x, area, perimeter);
}
if (x > 0 and r2map[y][x-1] == c) {
if (r2done[y][x-1] == 1) run2_r2(y, x-1, area, perimeter);
}
if (y < r2dim-1 and r2map[y+1][x] == c) {
if (r2done[y+1][x] == 1) run2_r2(y+1, x, area, perimeter);
}
if (x < r2dim-1 and r2map[y][x+1] == c) {
if (r2done[y][x+1] == 1) run2_r2(y, x+1, area, perimeter);
}
}
fn run2(input: []const u8) !u64 {
var lines = std.mem.splitScalar(u8, input, '\n');
{
var y: usize = 0;
while (lines.next()) |line| {
if (std.mem.eql(u8, line, "")) continue;
std.mem.copyForwards(u8, &r2map[y], line);
std.debug.print("{s}\n",.{line});
r2dim = line.len;
y += 1;
}
}
std.debug.print("map max {}\n", .{r2dim});
var total: u64 = 0;
for (0..r2dim) |y| {
for (0..r2dim) |x| {
if (r2done[y][x] > 0) continue;
run2_r(y, x);
var area: u64 = 0;
var perimeter: u64 = 0;
run2_r2(y, x, &area, &perimeter);
total += area * perimeter;
}
}
return total;
}
var r1map: [256][256]u8 = [_][256]u8{[_]u8{0}**256}**256;
var r1done: [256][256]u8 = [_][256]u8{[_]u8{0}**256}**256;
var r1dim: usize = undefined;
fn run1_r(y: usize, x: usize, area: *u64, perimeter: *u64) void {
const c = r1map[y][x];
area.* += 1;
r1done[y][x] = 1;
if (y > 0 and r1map[y-1][x] == c) {
if (r1done[y-1][x] == 0) run1_r(y-1, x, area, perimeter);
} else {
perimeter.* += 1;
}
if (x > 0 and r1map[y][x-1] == c) {
if (r1done[y][x-1] == 0) run1_r(y, x-1, area, perimeter);
} else {
perimeter.* += 1;
}
if (y < r1dim-1 and r1map[y+1][x] == c) {
if (r1done[y+1][x] == 0) run1_r(y+1, x, area, perimeter);
} else {
perimeter.* += 1;
}
if (x < r1dim-1 and r1map[y][x+1] == c) {
if (r1done[y][x+1] == 0) run1_r(y, x+1, area, perimeter);
} else {
perimeter.* += 1;
}
}
fn run1(input: []const u8) !u64 {
var lines = std.mem.splitScalar(u8, input, '\n');
{
var y: usize = 0;
while (lines.next()) |line| {
if (std.mem.eql(u8, line, "")) continue;
std.mem.copyForwards(u8, &r1map[y], line);
std.debug.print("{s}\n",.{line});
r1dim = line.len;
y += 1;
}
}
std.debug.print("map max {}\n", .{r1dim});
var total: u64 = 0;
for (0..r1dim) |y| {
for (0..r1dim) |x| {
if (r1done[y][x] == 1) continue;
var area: u64 = 0;
var perimeter: u64 = 0;
run1_r(y, x, &area, &perimeter);
total += area * perimeter;
}
}
return total;
}