This commit is contained in:
2024-12-02 18:53:23 +01:00
parent 1f3d56dea0
commit 1318a10f15
5 changed files with 172 additions and 0 deletions

44
2024/02/p2/test.zig Normal file
View File

@@ -0,0 +1,44 @@
const std = @import("std");
pub fn main() !void {
const line = "50 29 30 31 34 35 37";
var iter = std.mem.split(u8, line, " ");
const res = check_list(&iter, 0);
std.debug.print("{any}\n", .{res});
}
pub fn sameSign(n1: i8, n2: i8) bool {
return (n1 < 0) == (n2 < 0);
}
// null if everythin alr
// idx num of failure otherwise
pub fn check_list(iter: *std.mem.SplitIterator(u8, .sequence), skip: ?usize) !?usize {
var dir: i8 = 0;
var i: usize = 0;
if (skip != null and skip.? == 0) {
_ = iter.next();
i = 1;
}
var prev = try std.fmt.parseInt(i8, iter.next().?, 10);
while (iter.next()) |strnum| {
i += 1;
if (skip != null and skip.? == i) {
continue;
}
const n = try std.fmt.parseInt(i8, strnum, 10);
std.debug.print("i={d} prev={} sk={any} n={}\n", .{ i, prev, skip, n });
const d = prev - n;
if (d == 0 or @abs(d) > 3) {
return i;
}
if (dir == 0) {
dir = d;
} else if (!sameSign(dir, d)) {
return i;
}
prev = n;
}
return null;
}