const std = @import("std");
const path = "data/day10/input.txt";
const Pairs = [4][2]u8{
.{ '(', ')' },
.{ '[', ']' },
.{ '{', '}' },
.{ '<', '>' },
};
const RetType = u20;
fn parseInput() anyerror!RetType {
const input = @embedFile(path);
var lines = std.mem.split(u8, input, "\n");
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
var pairs = std.ArrayList(u8).init(allocator);
defer pairs.deinit();
var ret: RetType = 0;
lines: while (lines.next()) |line| {
pairs.clearRetainingCapacity();
chars: for (line) |ch| {
for (Pairs) |p| {
if (ch == p[1]) { // closing pair
const last = pairs.popOrNull();
// XXX: zig compiler error!
if (last == null) {
ret += errorValue(ch);
continue :lines;
} else if (last != p[0]) {
ret += errorValue(ch);
continue :lines;
}
continue :chars;
}
}
try pairs.append(ch);
}
}
return ret;
}
fn errorValue(ch: u8) RetType {
const ret: RetType = switch (ch) {
')' => 3,
']' => 57,
'}' => 1197,
'>' => 25137,
else => 0,
};
return ret;
}
pub fn first(allocator: ?std.mem.Allocator) anyerror!RetType {
_ = allocator;
return try parseInput();
}
pub fn main() anyerror!void {
var timer = try std.time.Timer.start();
const ret = try first(null);
const f = timer.lap() / 1000;
try std.testing.expectEqual(ret, @as(RetType, 411471));
std.debug.print("Day 10a result: {d} \t\ttime: {d}us\n", .{ ret, f });
}
test "day10a" {
try std.testing.expectEqual(@as(RetType, 411471), try first(std.testing.allocator));
}