aoc2015/day5_p2.zig
2024-12-08 01:05:31 +01:00

50 lines
1.4 KiB
Zig

const std = @import("std");
pub fn main() !void {
const stdin = std.io.getStdIn();
var buf_io = std.io.bufferedReader(stdin.reader());
var reader = buf_io.reader();
var buf = [_]u8{0} ** 1024;
var total: usize = 0;
while (try reader.readUntilDelimiterOrEof(&buf, '\n')) |line| {
const trimmed = std.mem.trim(u8, line, " \n\r\x00");
if (is_nice(trimmed)) {
total += 1;
}
}
std.debug.print("{}", .{total});
}
fn is_nice(s: []const u8) bool {
var has_repeating = false;
var has_semi_consecutive: bool = false;
var last_letter: u8 = '_';
var letter_even_before: u8 = '_';
for (s, 0..) |c, i| {
if (c == letter_even_before) {
has_semi_consecutive = true;
}
if (i > 1 and std.mem.indexOf(u8, s, &[2]u8{ last_letter, c }).? < i - 2) {
has_repeating = true;
}
letter_even_before = last_letter;
last_letter = c;
}
return has_repeating and has_semi_consecutive;
}
test "day-tests" {
try std.testing.expectEqual(true, is_nice("qjhvhtzxzqqjkmpb"));
try std.testing.expectEqual(true, is_nice("xxyxx"));
try std.testing.expectEqual(false, is_nice("uurcxstgmygtbstg"));
try std.testing.expectEqual(false, is_nice("ieodomkazucvgmuy"));
}
test "custom-test" {
try std.testing.expectEqual(false, is_nice("aaa"));
try std.testing.expectEqual(true, is_nice("aaaa"));
}