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

40 lines
1.2 KiB
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;
for (instructions) |c| {
switch (c) {
0 => break,
'(' => current += 1,
')' => current -= 1,
else => continue,
}
std.debug.print("{}\n", .{current});
}
return current;
}
test "day-tests" {
try std.testing.expectEqual(calculate_floor("((("), 3);
try std.testing.expectEqual(calculate_floor("(())"), 0);
try std.testing.expectEqual(calculate_floor("()()"), 0);
try std.testing.expectEqual(calculate_floor("(()(()("), 3);
try std.testing.expectEqual(calculate_floor("))((((("), 3);
try std.testing.expectEqual(calculate_floor("())"), -1);
try std.testing.expectEqual(calculate_floor("))("), -1);
try std.testing.expectEqual(calculate_floor(")))"), -3);
try std.testing.expectEqual(calculate_floor(")())())"), -3);
}