aboutsummaryrefslogtreecommitdiff
path: root/2021/01/first.zig
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--2021/01/first.zig26
1 files changed, 26 insertions, 0 deletions
diff --git a/2021/01/first.zig b/2021/01/first.zig
new file mode 100644
index 0000000..79dcc89
--- /dev/null
+++ b/2021/01/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), 7);
+ 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;
+ var prev: ?u64 = null;
+ while (it.next()) |value| {
+ const n = try std.fmt.parseInt(u64, value, 10);
+ if (prev) |p| {
+ if (p < n) {
+ tot += 1;
+ }
+ }
+ prev = n;
+ }
+ return tot;
+}