day 5
This commit is contained in:
parent
51f3a91632
commit
58a6925395
2 changed files with 106 additions and 0 deletions
56
day5_p1.zig
Normal file
56
day5_p1.zig
Normal file
|
@ -0,0 +1,56 @@
|
||||||
|
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);
|
||||||
|
}
|
50
day5_p2.zig
Normal file
50
day5_p2.zig
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
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"));
|
||||||
|
}
|
Loading…
Reference in a new issue