1
0

Compare commits

...

3 Commits

Author SHA1 Message Date
b13d5bb5f9
light cleaning
Some checks failed
Build and test / test (push) Failing after 1m41s
2023-12-13 17:39:49 +11:00
06ce20853a
day13 part2 2023-12-13 17:16:14 +11:00
794dddf854
day13 part1 2023-12-13 16:51:23 +11:00

136
src/day13.zig Normal file
View File

@ -0,0 +1,136 @@
const std = @import("std");
const util = @import("util.zig");
const mem = std.mem;
pub fn main() !void {
const input = @embedFile("data/day13.txt");
const sln = try solve(util.gpa, input);
std.debug.print("{d}\n", .{sln.a});
std.debug.print("{d}\n", .{sln.b});
}
const Solution = struct {
a: usize,
b: 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;
const h = input.len;
const w = input[0].len;
for (0..h - 1) |scan| {
var offset: usize = 0;
var col: usize = 0;
var errors: usize = 0;
while (true) {
if (input[scan - offset][col] != input[scan + offset + 1][col]) {
errors += 1;
}
if (errors > err_tolerance + 1) {
break;
}
col = (col + 1) % w;
if (col == 0) offset += 1;
if (scan < offset or scan + offset + 1 >= h) {
if (errors == err_tolerance) return scan + 1;
break;
}
}
}
return null;
}
fn solve(alloc: mem.Allocator, input: []const u8) !Solution {
var curr_pattern = std.ArrayList([]const u8).init(alloc);
defer curr_pattern.deinit();
var it = util.splitLines(input);
var sums = [2]usize{ 0, 0 };
while (it.next()) |line| {
if (line.len == 0) {
const pattern = try curr_pattern.toOwnedSlice();
defer alloc.free(pattern);
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 = sums[0], .b = sums[1] };
}
test "silver" {
const 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, 400), sln.b);
}