about summary refs log tree commit diff
path: root/aoc/2021/02/part-two.zig
diff options
context:
space:
mode:
Diffstat (limited to 'aoc/2021/02/part-two.zig')
-rw-r--r--aoc/2021/02/part-two.zig27
1 files changed, 27 insertions, 0 deletions
diff --git a/aoc/2021/02/part-two.zig b/aoc/2021/02/part-two.zig
new file mode 100644
index 0000000..f597db6
--- /dev/null
+++ b/aoc/2021/02/part-two.zig
@@ -0,0 +1,27 @@
+const eql = std.mem.eql;
+const parseUnsigned = std.fmt.parseUnsigned;
+const print = std.debug.print;
+const std = @import("std");
+const tokenize = std.mem.tokenize;
+
+pub fn main() !void {
+    var position = @as(usize, 0);
+    var depth = @as(usize, 0);
+    defer print("{}\n", .{ position * depth });
+
+    var aim = @as(usize, 0);
+    var input = tokenize(@embedFile("input"), "\n");
+    while (input.next()) |line| {
+        var command = tokenize(line, " ");
+        const direction = command.next().?;
+        const x = try parseUnsigned(usize, command.next().?, 10);
+        if (eql(u8, direction, "forward")) {
+            position += x;
+            depth += aim * x;
+        } else if (eql(u8, direction, "down")) {
+            aim += x;
+        } else if (eql(u8, direction, "up")) {
+            aim -= x;
+        } else unreachable;
+    }
+}