aboutsummaryrefslogtreecommitdiff
path: root/2021/01/second.zig
diff options
context:
space:
mode:
authorJulien Dessaux2022-02-09 00:45:04 +0100
committerJulien Dessaux2022-02-09 00:45:04 +0100
commit3587c73fea30ce6c8881be3708892fbcce044f86 (patch)
tree14ae8893f37c16c41e0370c8505eb10857132b23 /2021/01/second.zig
parentAdded solutions for 18th day: snailfish arithmetic (diff)
downloadadvent-of-code-3587c73fea30ce6c8881be3708892fbcce044f86.tar.gz
advent-of-code-3587c73fea30ce6c8881be3708892fbcce044f86.tar.bz2
advent-of-code-3587c73fea30ce6c8881be3708892fbcce044f86.zip
Added first day in zig
Diffstat (limited to '')
-rw-r--r--2021/01/second.zig30
1 files changed, 30 insertions, 0 deletions
diff --git a/2021/01/second.zig b/2021/01/second.zig
new file mode 100644
index 0000000..eb6993e
--- /dev/null
+++ b/2021/01/second.zig
@@ -0,0 +1,30 @@
+const std = @import("std");
+
+const example = @embedFile("example");
+const input = @embedFile("input");
+
+pub fn main() anyerror!void {
+ try std.testing.expectEqual(solve(example), 5);
+ 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} ** 3;
+ while (it.next()) |value| {
+ const n = try std.fmt.parseInt(u64, value, 10);
+ if (prev[0]) |p| {
+ if (p < n) {
+ tot += 1;
+ }
+ }
+ var i: usize = 0;
+ while (i < prev.len - 1) : (i += 1) {
+ prev[i] = prev[i + 1];
+ }
+ prev[2] = n;
+ }
+ return tot;
+}