1
0

light cleaning
Some checks failed
Build and test / test (push) Failing after 1m41s

This commit is contained in:
xeals 2023-12-13 17:39:49 +11:00
parent 06ce20853a
commit b13d5bb5f9
Signed by: xeals
SSH Key Fingerprint: SHA256:pRv+8swQDA+/LuZ7NHj9m006BbKexlNK62OUA01ZZBc

View File

@ -14,15 +14,27 @@ const Solution = struct {
b: usize,
};
fn findReflection(input: []const []const u8) usize {
fn transpose(alloc: mem.Allocator, grid: []const []const u8) ![]const []const u8 {
std.debug.assert(grid.len > 0 and grid[0].len > 0);
var g = try alloc.alloc([]u8, grid[0].len);
for (0..grid[0].len) |i| {
g[i] = try alloc.alloc(u8, grid.len);
}
for (0..grid.len) |i| {
for (0..grid[i].len) |j| {
g[j][i] = grid[i][j];
}
}
return g;
}
fn findReflection(input: []const []const u8, err_tolerance: usize) ?usize {
if (input.len == 0) return 0;
std.debug.print("{s}\n", .{input});
const h = input.len;
const w = input[0].len;
// Scan horizontal reflectivity
hscan: for (0..h - 1) |scan| {
for (0..h - 1) |scan| {
var offset: usize = 0;
var col: usize = 0;
var errors: usize = 0;
@ -30,42 +42,19 @@ fn findReflection(input: []const []const u8) usize {
if (input[scan - offset][col] != input[scan + offset + 1][col]) {
errors += 1;
}
if (errors > 1) {
continue :hscan;
if (errors > err_tolerance + 1) {
break;
}
std.debug.print("{}±{} {} [{}]\n", .{ scan, offset, col, errors });
col = (col + 1) % w;
if (col == 0) offset += 1;
if (scan < offset or scan + offset + 1 >= h) {
if (errors == 1) return (scan + 1) * 100;
continue :hscan;
if (errors == err_tolerance) return scan + 1;
break;
}
}
}
// Scan vertical reflectivity
vscan: for (0..w - 1) |scan| {
var offset: usize = 0;
var row: usize = 0;
var errors: usize = 0;
while (true) {
if (input[row][scan - offset] != input[row][scan + offset + 1]) {
errors += 1;
}
if (errors > 1) {
continue :vscan;
}
std.debug.print("{} {}±{} [{}]\n", .{ row, scan, offset, errors });
row = (row + 1) % h;
if (row == 0) offset += 1;
if (scan < offset or scan + offset + 1 >= w) {
if (errors == 1) return scan + 1;
continue :vscan;
}
}
}
unreachable;
return null;
}
fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
@ -73,20 +62,31 @@ fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
defer curr_pattern.deinit();
var it = util.splitLines(input);
var sum: usize = 0;
var sums = [2]usize{ 0, 0 };
while (it.next()) |line| {
if (line.len == 0) {
const pattern = try curr_pattern.toOwnedSlice();
defer alloc.free(pattern);
const score = findReflection(pattern);
std.debug.print("... {}\n", .{score});
sum += score;
for (0..2) |et| {
if (findReflection(pattern, et)) |score| {
sums[et] += 100 * score;
} else {
const t = try transpose(alloc, pattern);
sums[et] += findReflection(t, et) orelse unreachable;
// Ew.
for (t) |r| {
alloc.free(r);
}
alloc.free(t);
}
}
} else {
try curr_pattern.append(line);
}
}
return .{ .a = 405, .b = sum };
return .{ .a = sums[0], .b = sums[1] };
}
test "silver" {
@ -108,9 +108,8 @@ test "silver" {
\\#....#..#
\\
;
_ = input;
// const sln = try solve(std.testing.allocator, input);
// try std.testing.expectEqual(@as(usize, 405), sln.a);
const sln = try solve(std.testing.allocator, input);
try std.testing.expectEqual(@as(usize, 405), sln.a);
}
test "gold" {