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

56 lines
1.6 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 vowels: u16 = 0;
var has_consecutive: bool = false;
var last_letter: u8 = '_';
for (s) |c| {
if (c == last_letter) {
has_consecutive = true;
}
switch (c) {
'a', 'e', 'i', 'o', 'u' => {
vowels += 1;
},
else => {
// Check for forbidden sequences
if ((last_letter == 'a' and c == 'b') or
(last_letter == 'c' and c == 'd') or
(last_letter == 'p' and c == 'q') or
(last_letter == 'x' and c == 'y'))
{
return false;
}
},
}
last_letter = c;
}
return vowels > 2 and has_consecutive;
}
test "day-tests" {
try std.testing.expectEqual(is_nice("ugknbfddgicrmopn"), true);
try std.testing.expectEqual(is_nice("aaa"), true);
try std.testing.expectEqual(is_nice("jchzalrnumimnmhp"), false);
try std.testing.expectEqual(is_nice("haegwjzuvuyypxyu"), false);
try std.testing.expectEqual(is_nice("dvszwmarrgswjxmb"), false);
}