Compare commits

..

No commits in common. "fe3b71908e110dbd758fa3cf05454ab9a5d0c09c" and "7a0c4751ebd87f9a4e1d331a2b663bd62b0bb75b" have entirely different histories.

View file

@ -1,77 +1,46 @@
const std = @import("std");
pub const title = "Day 04: Printing Department";
pub const title = "Day 04";
pub fn run(_: std.mem.Allocator) !void {
const initial_input = @embedFile("./input/day04.txt");
//const initial_input =
// \\..@@.@@@@.
// \\@@@.@.@.@@
// \\@@@@@.@.@@
// \\@.@@@@..@.
// \\@@.@@@@.@@
// \\.@@@@@@@.@
// \\.@.@.@.@@@
// \\@.@@@.@@@@
// \\.@@@@@@@@.
// \\@.@.@@@.@.
// ;
const input: []u8 = @constCast(initial_input);
const input = @embedFile("./input/day04.txt");
var lines = std.mem.tokenizeScalar(u8, input, '\n');
var accumulator: u32 = 0;
while (true) {
std.debug.print("{s}\n", .{input});
var previous_line: ?[]const u8 = null;
while (lines.next()) |line| {
const next_line = lines.peek();
var rolls_removed: u32 = 0;
if (previous_line) |p| std.debug.assert(line.len == p.len);
if (next_line) |n| std.debug.assert(line.len == n.len);
var lines = std.mem.tokenizeScalar(u8, input, '\n');
for (line, 0..) |c, i| {
if (c != '@') continue;
var previous_line: ?[]const u8 = null;
var line_index: usize = 0;
while (lines.next()) |line| {
const next_line = lines.peek();
var rolls: u8 = 0;
if (previous_line) |p| std.debug.assert(line.len == p.len);
if (next_line) |n| std.debug.assert(line.len == n.len);
for (line, 0..) |c, i| {
if (c != '@') continue;
var rolls: u8 = 0;
if (previous_line) |p| {
if (i > 0 and p[i - 1] == '@') rolls += 1;
if (p[i] == '@') rolls += 1;
if (i < p.len - 1 and p[i + 1] == '@') rolls += 1;
}
if (i > 0 and line[i - 1] == '@') rolls += 1;
if (i < line.len - 1 and line[i + 1] == '@') rolls += 1;
if (next_line) |n| {
if (i > 0 and n[i - 1] == '@') rolls += 1;
if (n[i] == '@') rolls += 1;
if (i < n.len - 1 and n[i + 1] == '@') rolls += 1;
}
if (rolls < 4) {
input[(line_index * (line.len + 1)) + i] = 'x';
rolls_removed += 1;
}
if (previous_line) |p| {
if (i > 0 and p[i - 1] == '@') rolls += 1;
if (p[i] == '@') rolls += 1;
if (i < p.len - 1 and p[i + 1] == '@') rolls += 1;
}
previous_line = line;
line_index += 1;
if (i > 0 and line[i - 1] == '@') rolls += 1;
if (i < line.len - 1 and line[i + 1] == '@') rolls += 1;
if (next_line) |n| {
if (i > 0 and n[i - 1] == '@') rolls += 1;
if (n[i] == '@') rolls += 1;
if (i < n.len - 1 and n[i + 1] == '@') rolls += 1;
}
if (rolls < 4) {
accumulator += 1;
}
}
std.debug.print("removed {d} rolls\n", .{rolls_removed});
if (rolls_removed > 0) {
accumulator += rolls_removed;
} else {
break;
}
previous_line = line;
}
var buffer: [8]u8 = undefined;