aboutsummaryrefslogtreecommitdiff
path: root/2022/04-camp-cleanup/second.zig
diff options
context:
space:
mode:
authorJulien Dessaux2022-12-04 06:28:59 +0100
committerJulien Dessaux2022-12-04 06:28:59 +0100
commitc912b3347da6da6062b8ce56eac1711d6fb54ed9 (patch)
tree7f4004f911b5ce2eb8ccf5df5e18ffbeb1903811 /2022/04-camp-cleanup/second.zig
parent2022-03 in zig (diff)
downloadadvent-of-code-c912b3347da6da6062b8ce56eac1711d6fb54ed9.tar.gz
advent-of-code-c912b3347da6da6062b8ce56eac1711d6fb54ed9.tar.bz2
advent-of-code-c912b3347da6da6062b8ce56eac1711d6fb54ed9.zip
2022-04 in zig
Diffstat (limited to '')
-rw-r--r--2022/04-camp-cleanup/second.zig26
1 files changed, 26 insertions, 0 deletions
diff --git a/2022/04-camp-cleanup/second.zig b/2022/04-camp-cleanup/second.zig
new file mode 100644
index 0000000..283169e
--- /dev/null
+++ b/2022/04-camp-cleanup/second.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), 4);
+ 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 or a <= c and b >= c or a <= d and b >= d) {
+ tot += 1;
+ }
+ }
+ return tot;
+}