This repository has been archived on 2022-12-11. You can view files and clone it, but cannot push or open issues or pull requests.
aoc2022/6/1-2.zig

42 lines
1.2 KiB
Zig
Raw Normal View History

2022-12-06 16:47:50 +00:00
const std = @import("std");
pub fn main() void {
const input = @embedFile("input.asc");
std.debug.print("Part 1: {}\n", .{findDisctinctSubstrIdx(input, 4)});
std.debug.print("Part 2: {}\n", .{findDisctinctSubstrIdx(input, 14)});
}
fn findDisctinctSubstrIdx(input: []const u8, n: usize) usize {
// Loop through all the groups of 4
var i: usize = n;
group: while (i < input.len) : (i += 1) {
const slice = input[(i - n)..i];
// See if each one of them is unique in smaller groups.
// Ex.: is 1 in 234? -> is 2 in 34? -> is 3 in 4?
var j: usize = 0;
while (j < slice.len - 1) : (j += 1) {
const needle = slice[j];
const haystack = slice[(j + 1)..];
for (haystack) |blade| {
// We found a match, throw batch to garbage
if (needle == blade) {
// We know this character appears somewhere else in
// the haystack. As long as this letter is here, this
// n-letter work will be invalid. Skip ahead.
i += j;
continue :group;
}
}
}
// We found it, get out
break;
}
return i;
}