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
|
const Stack = @import("Stack.zig");
const desc = std.sort.desc(usize);
const indexOfScalar = std.mem.indexOfScalar;
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 i: usize = 0;
var max_len: usize = 0;
while (input.next()) |line| : (i += 1) {
if (line.len > max_len)
max_len = line.len;
}
var stack = try Stack.alloc(page_allocator, max_len);
defer stack.free();
var scores = try page_allocator.alloc(usize, i);
defer page_allocator.free(scores);
input.reset();
loop: while (input.next()) |line| : (stack.reset()) {
i -= 1;
scores[i] = 0;
for (line) |c|
switch (c) {
'(', '[', '{', '<' => stack.push(c),
else => if (stack.pop().? ^ c > 6) continue :loop,
};
while (stack.pop()) |c| {
scores[i] *= 5;
scores[i] += @as(usize, switch (c) {
'(' => 1, '[' => 2, '{' => 3, '<' => 4,
else => unreachable,
});
}
}
sort(usize, scores, {}, desc);
i = indexOfScalar(usize, scores, 0).?;
print("{}\n", .{ scores[i / 2] });
}
|