aboutsummaryrefslogtreecommitdiff
path: root/challenge-308/deadmarshal/zig/ch1/src
diff options
context:
space:
mode:
authordeadmarshal <adeadmarshal@gmail.com>2025-02-10 14:59:57 +0330
committerdeadmarshal <adeadmarshal@gmail.com>2025-02-10 14:59:57 +0330
commit9cdf26f0f3ef3cedb981c930707230ed2a7ff754 (patch)
treeb28d5231ef6904ad60a1f4373ee53a69b5699489 /challenge-308/deadmarshal/zig/ch1/src
parentaa4b8399bdb3da0a50173fcab689bfa80b9b54e1 (diff)
downloadperlweeklychallenge-club-9cdf26f0f3ef3cedb981c930707230ed2a7ff754.tar.gz
perlweeklychallenge-club-9cdf26f0f3ef3cedb981c930707230ed2a7ff754.tar.bz2
perlweeklychallenge-club-9cdf26f0f3ef3cedb981c930707230ed2a7ff754.zip
TWC308
Diffstat (limited to 'challenge-308/deadmarshal/zig/ch1/src')
-rw-r--r--challenge-308/deadmarshal/zig/ch1/src/main.zig54
-rw-r--r--challenge-308/deadmarshal/zig/ch1/src/root.zig10
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-308/deadmarshal/zig/ch1/src/main.zig b/challenge-308/deadmarshal/zig/ch1/src/main.zig
new file mode 100644
index 0000000000..249b714db7
--- /dev/null
+++ b/challenge-308/deadmarshal/zig/ch1/src/main.zig
@@ -0,0 +1,54 @@
+const std = @import("std");
+
+fn countCommon(
+ allocator: std.mem.Allocator,
+ a1: []const []const u8,
+ a2: []const []const u8,
+) !usize {
+ var count: usize = 0;
+ var h = std.StringHashMap(u32).init(allocator);
+ defer h.deinit();
+ for (a1) |s| {
+ const entry = try h.getOrPut(s);
+ if (entry.found_existing) {
+ entry.value_ptr.* += 1;
+ } else {
+ entry.value_ptr.* = 1;
+ }
+ }
+ for (a2) |s| {
+ const entry = try h.getOrPut(s);
+ if (entry.found_existing) {
+ entry.value_ptr.* += 1;
+ } else {
+ entry.value_ptr.* = 1;
+ }
+ }
+ var it = h.iterator();
+ while (it.next()) |e| {
+ if (e.value_ptr.* >= 2) {
+ count += 1;
+ }
+ }
+ return count;
+}
+
+pub fn main() !void {
+ var gpa = std.heap.GeneralPurposeAllocator(.{}){};
+ defer {
+ std.debug.assert(gpa.detectLeaks() == false);
+ std.debug.assert(gpa.deinit() == .ok);
+ }
+ const allocator = gpa.allocator();
+
+ const a1 = [_][]const u8{ "perl", "weekly", "challenge" };
+ const a2 = [_][]const u8{ "raku", "weekly", "challenge" };
+ const a3 = [_][]const u8{ "perl", "raku", "python" };
+ const a4 = [_][]const u8{ "python", "java" };
+ const a5 = [_][]const u8{ "guest", "contribution" };
+ const a6 = [_][]const u8{ "fun", "weekly", "challenge" };
+
+ std.debug.print("{}\n", .{try countCommon(allocator, &a1, &a2)});
+ std.debug.print("{}\n", .{try countCommon(allocator, &a3, &a4)});
+ std.debug.print("{}\n", .{try countCommon(allocator, &a5, &a6)});
+}
diff --git a/challenge-308/deadmarshal/zig/ch1/src/root.zig b/challenge-308/deadmarshal/zig/ch1/src/root.zig
new file mode 100644
index 0000000000..ecfeade1a3
--- /dev/null
+++ b/challenge-308/deadmarshal/zig/ch1/src/root.zig
@@ -0,0 +1,10 @@
+const std = @import("std");
+const testing = std.testing;
+
+export fn add(a: i32, b: i32) i32 {
+ return a + b;
+}
+
+test "basic add functionality" {
+ try testing.expect(add(3, 7) == 10);
+}