aoc2015/day1_p2.zig

38 lines
823 B
Zig
Raw Normal View History

2024-12-07 22:30:39 +00:00
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);
}