From ad976b6960a6fbe985e1b78b6187c41e68ab9c77 Mon Sep 17 00:00:00 2001 From: Anthony Debucquoy Date: Sat, 10 May 2025 20:38:28 +0200 Subject: [PATCH] d1 zig --- 2015/d1/main.zig | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 2015/d1/main.zig diff --git a/2015/d1/main.zig b/2015/d1/main.zig new file mode 100644 index 0000000..b86253d --- /dev/null +++ b/2015/d1/main.zig @@ -0,0 +1,33 @@ +const std = @import("std"); + +const input = @embedFile("input"); + +fn floorTester(in: []const u8) i32{ + var floor: i32 = 0; + for (in) |c|{ + switch (c){ + '(' => floor += 1, + ')' => floor -= 1, + else => unreachable, + } + } + return floor; +} + +fn whenBasement(in: []const u8) ?usize{ + var floor: i32 = 0; + for (in, 0..) |c, i|{ + switch (c){ + '(' => floor += 1, + ')' => floor -= 1, + else => unreachable, + } + if (floor < 0) return i+1; + } + return null; +} + +pub fn main() void{ + std.debug.print("Is on floor {d}\n", .{floorTester(input)}); + std.debug.print("Is in basement at {?}\n", .{whenBasement(input)}); +}