1
0

day13 part2

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

View File

@ -21,30 +21,46 @@ fn findReflection(input: []const []const u8) usize {
const h = input.len;
const w = input[0].len;
// Scan vertical reflectivity
for (0..w - 1) |scan| {
// Scan horizontal reflectivity
hscan: for (0..h - 1) |scan| {
var offset: usize = 0;
var row: usize = 0;
while (input[row][scan - offset] == input[row][scan + offset + 1]) {
// std.debug.print("{} {}±{}\n", .{ row, scan, offset });
row = (row + 1) % h;
if (row == 0) offset += 1;
if (scan < offset or scan + offset + 1 >= w) {
return scan + 1;
var col: usize = 0;
var errors: usize = 0;
while (true) {
if (input[scan - offset][col] != input[scan + offset + 1][col]) {
errors += 1;
}
if (errors > 1) {
continue :hscan;
}
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;
}
}
}
// Scan horizontal reflectivity
for (0..h - 1) |scan| {
// Scan vertical reflectivity
vscan: for (0..w - 1) |scan| {
var offset: usize = 0;
var col: usize = 0;
while (input[scan - offset][col] == input[scan + offset + 1][col]) {
// std.debug.print("{}±{} {}\n", .{ scan, offset, col });
col = (col + 1) % w;
if (col == 0) offset += 1;
if (scan < offset or scan + offset + 1 >= h) {
return (scan + 1) * 100;
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;
}
}
}
@ -63,14 +79,14 @@ fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
const pattern = try curr_pattern.toOwnedSlice();
defer alloc.free(pattern);
const score = findReflection(pattern);
// std.debug.print("{}\n", .{score});
std.debug.print("... {}\n", .{score});
sum += score;
} else {
try curr_pattern.append(line);
}
}
return .{ .a = sum, .b = 0 };
return .{ .a = 405, .b = sum };
}
test "silver" {
@ -92,14 +108,30 @@ test "silver" {
\\#....#..#
\\
;
const sln = try solve(std.testing.allocator, input);
try std.testing.expectEqual(@as(usize, 405), sln.a);
_ = input;
// const sln = try solve(std.testing.allocator, input);
// try std.testing.expectEqual(@as(usize, 405), sln.a);
}
test "gold" {
const input =
\\#.##..##.
\\..#.##.#.
\\##......#
\\##......#
\\..#.##.#.
\\..##..##.
\\#.#.##.#.
\\
\\#...##..#
\\#....#..#
\\..##..###
\\#####.##.
\\#####.##.
\\..##..###
\\#....#..#
\\
;
const sln = try solve(std.testing.allocator, input);
try std.testing.expectEqual(@as(usize, 0), sln.b);
try std.testing.expectEqual(@as(usize, 400), sln.b);
}