From c912b3347da6da6062b8ce56eac1711d6fb54ed9 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 4 Dec 2022 06:28:59 +0100 Subject: 2022-04 in zig --- 2022/04-camp-cleanup/first.zig | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 2022/04-camp-cleanup/first.zig (limited to '2022/04-camp-cleanup/first.zig') diff --git a/2022/04-camp-cleanup/first.zig b/2022/04-camp-cleanup/first.zig new file mode 100644 index 0000000..6a5bf52 --- /dev/null +++ b/2022/04-camp-cleanup/first.zig @@ -0,0 +1,26 @@ +const std = @import("std"); + +const example = @embedFile("example"); +const input = @embedFile("input"); + +pub fn main() anyerror!void { + try std.testing.expectEqual(solve(example), 2); + const result = try solve(input); + try std.io.getStdOut().writer().print("{}\n", .{result}); +} + +fn solve(puzzle: []const u8) !u64 { + var it = std.mem.tokenize(u8, puzzle, "\n"); + var tot: u64 = 0; + while (it.next()) |line| { + var it2 = std.mem.tokenize(u8, line, "-,"); + const a = try std.fmt.parseInt(u64, it2.next() orelse unreachable, 10); + const b = try std.fmt.parseInt(u64, it2.next() orelse unreachable, 10); + const c = try std.fmt.parseInt(u64, it2.next() orelse unreachable, 10); + const d = try std.fmt.parseInt(u64, it2.next() orelse unreachable, 10); + if (a <= c and b >= d or a >= c and b <= d) { + tot += 1; + } + } + return tot; +} -- cgit v1.2.3