about summary refs log tree commit diff
path: root/aoc/2021/10/part-two.zig
blob: 6e5b11df8768330d916de920fc3b78186a7325bf (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const Stack = @import("stack.zig").Stack;
const asc = std.sort.asc(usize);
const page_allocator = std.heap.page_allocator;
const print = std.debug.print;
const sort = std.sort.sort;
const std = @import("std");
const tokenize = std.mem.tokenize;

pub fn main() !void {
    var input = tokenize(@embedFile("input"), "\n");
    var incomplete: usize = 0;
    var max_len: usize = 0;
    while (input.next()) |line| : (incomplete += 1) {
        if (line.len > max_len)
            max_len = line.len;
    }

    var stack = try Stack(u8).alloc(page_allocator, max_len);
    defer stack.free();
    input.reset();
    while (input.next()) |line| {
        for (line) |c|
            switch (c) {
                '(', '[', '{', '<' => stack.push(c),
                else => if (stack.pop() ^ c > 6) {
                    incomplete -= 1;
                    break;
                },
            };
        stack.reset();
    }

    var scores = try page_allocator.alloc(usize, incomplete);
    defer page_allocator.free(scores);
    input.reset();
    loop: while (input.next()) |line| {
        stack.reset();
        for (line) |c|
            switch (c) {
                '(', '[', '{', '<' => stack.push(c),
                else => if (stack.pop() ^ c > 6) continue :loop,
            };
        incomplete -= 1;
        scores[incomplete] = 0;
        while (stack.len > 0) {
            scores[incomplete] *= 5;
            scores[incomplete] += @as(usize, switch (stack.pop()) {
                '(' => 1, '[' => 2, '{' => 3, '<' => 4,
                else => unreachable,
            });
        }
    }

    sort(usize, scores, {}, asc);
    print("{}\n", .{ scores[scores.len / 2] });
}