From 85bd7ec1bd9cdc7ec53692fce5cae3118b7357a0 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sun, 5 Dec 2021 15:18:59 +0700 Subject: [aoc] Add first five/fifth --- aoc/2021/02/part-one.zig | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 aoc/2021/02/part-one.zig (limited to 'aoc/2021/02/part-one.zig') diff --git a/aoc/2021/02/part-one.zig b/aoc/2021/02/part-one.zig new file mode 100644 index 0000000..68335d7 --- /dev/null +++ b/aoc/2021/02/part-one.zig @@ -0,0 +1,25 @@ +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 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 + else if (eql(u8, direction, "down")) + depth += x + else if (eql(u8, direction, "up")) + depth -= x + else unreachable; + } +} -- cgit 1.4.1