i don't know why i did that so stupidly this is like 25x faster

This commit is contained in:
2024-12-06 01:48:26 -06:00
parent 613edfd80b
commit dabcdfc2e1

View File

@@ -101,38 +101,36 @@ fn run2(s: []const u8) !u64 {
var map2: [256][256]u8 = undefined; var map2: [256][256]u8 = undefined;
@memcpy(&map2, &omap); @memcpy(&map2, &omap);
var prev_states = std.ArrayList(u32).init(alloc);
y = 0; y = 0;
while (y < 256) : (y += 1) { while (y < 256) : (y += 1) {
if (map[y][2] == 0) continue; if (map[y][2] == 0) continue;
x = 0; x = 0;
while (x < 256) : (x += 1) { while (x < 256) : (x += 1) {
// attempt new obstacle... this is ugly and painfully slow // attempt new obstacle... this is ugly
// but i'm lazy // but i'm lazy
if (y == sy and x == sx) continue; if (y == sy and x == sx) continue;
if (map[y][x] == 'X') { if (map[y][x] == 'X') {
prev_states.clearRetainingCapacity();
map2[y][x] = '#'; map2[y][x] = '#';
var iy = sy; var iy = sy;
var ix = sx; var ix = sx;
dy = -1; dy = -1;
dx = 0; dx = 0;
var dir: u8 = 0;
var prev_states: [256][256][4]bool = .{.{.{false}**4}**256}**256;
while (map2[iy][ix] != 0) { while (map2[iy][ix] != 0) {
const state: u32 = @intCast((@as(usize,@bitCast(dy))<<24)|(@as(usize,@bitCast(dx))<<16)|(iy<<8)|(ix)); if (prev_states[iy][ix][dir]) {
const idx = std.mem.indexOfScalar(u32, prev_states.items, state);
if (idx) |_| {
total += 1; total += 1;
break; break;
} }
try prev_states.append(state); prev_states[iy][ix][dir] = true;
ny = @intCast(@as(isize,@intCast(iy)) + dy); ny = @intCast(@as(isize,@intCast(iy)) + dy);
nx = @intCast(@as(isize,@intCast(ix)) + dx); nx = @intCast(@as(isize,@intCast(ix)) + dx);
while (map2[ny][nx] == '#') { while (map2[ny][nx] == '#') {
if (dy == -1 and dx == 0) { dy = 0; dx = 1; } if (dir == 0) { dy = 0; dx = 1; dir = 1; }
else if (dy == 0 and dx == 1) { dy = 1; dx = 0; } else if (dir == 1) { dy = 1; dx = 0; dir = 2; }
else if (dy == 1 and dx == 0) { dy = 0; dx = -1; } else if (dir == 2) { dy = 0; dx = -1; dir = 3; }
else { dy = -1; dx = 0; } else { dy = -1; dx = 0; dir = 0; }
ny = @intCast(@as(isize,@intCast(iy)) + dy); ny = @intCast(@as(isize,@intCast(iy)) + dy);
nx = @intCast(@as(isize,@intCast(ix)) + dx); nx = @intCast(@as(isize,@intCast(ix)) + dx);
} }