From f53ac491b1ec74ad9c44d20065517467c1877190 Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Fri, 10 Dec 2021 18:48:20 +0700 Subject: [aoc/2021] Finish day 10 --- aoc/2021/10/part-one.zig | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 aoc/2021/10/part-one.zig (limited to 'aoc/2021/10/part-one.zig') diff --git a/aoc/2021/10/part-one.zig b/aoc/2021/10/part-one.zig new file mode 100644 index 0000000..27ac20c --- /dev/null +++ b/aoc/2021/10/part-one.zig @@ -0,0 +1,34 @@ +const Stack = @import("stack.zig").Stack; +const page_allocator = std.heap.page_allocator; +const print = std.debug.print; +const std = @import("std"); +const tokenize = std.mem.tokenize; + +pub fn main() !void { + var input = tokenize(@embedFile("input"), "\n"); + var max_len: usize = 0; + while (input.next()) |line| { + if (line.len > max_len) + max_len = line.len; + } + var stack = try Stack(u8).alloc(page_allocator, max_len); + defer stack.free(); + + var sum: usize = 0; + defer print("{}\n", .{ sum }); + input.reset(); + while (input.next()) |line| { + for (line) |c| + switch (c) { + '(', '[', '{', '<' => stack.push(c), + else => if (stack.pop() ^ c > 6) { + sum += @as(usize, switch (c) { + ')' => 3, ']' => 57, '}' => 1197, '>' => 25137, + else => unreachable, + }); + break; + }, + }; + stack.reset(); + } +} -- cgit 1.4.1