Compare commits
No commits in common. "b13d5bb5f9928a270c76f64aee75f109ce52522b" and "fa7a9aab14320494b0c4f577e3c007b3be9b770c" have entirely different histories.
b13d5bb5f9
...
fa7a9aab14
136
src/day13.zig
136
src/day13.zig
@ -1,136 +0,0 @@
|
||||
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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user