aoc2015/day1_p2.zig
2024-12-07 23:31:35 +01:00

37 lines
823 B
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} ** 100000;
_ = try reader.readAll(&buf);
std.debug.print("{}", .{calculate_floor(&buf)});
}
fn calculate_floor(instructions: []const u8) i32 {
var current: i32 = 0;
var i: i32 = 1;
for (instructions) |c| {
switch (c) {
0 => break,
'(' => current += 1,
')' => current -= 1,
else => continue,
}
if (current < 0) {
return i;
}
i += 1;
}
return -1;
}
test "day-tests" {
try std.testing.expectEqual(calculate_floor(")"), 1);
try std.testing.expectEqual(calculate_floor("()())"), 5);
}