about summary refs log tree commit diff
path: root/aoc/2021/10/part-one.zig
blob: 130fc4a2ac631d717942bc4b883f61687000f839 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const Stack = @import("Stack.zig");
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.alloc(page_allocator, max_len);
    defer stack.free();

    var sum: usize = 0;
    defer print("{}\n", .{ sum });
    input.reset();
    while (input.next()) |line| : (stack.reset())
        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;
                },
            };
}