ZTWBVEQZV4CS6752OOYBUFTLFXKA6CV43UQCI6KFYN7E662U2LIQC
const std = @import("std");
const Str = []const u8;
const DAYS = 1;
const aoc2015 = struct {
const day01 = @import("day01.zig");
};
test {
_ = @import("day01.zig");
}
pub fn main() anyerror!void {
var timer = try std.time.Timer.start();
var time_sum: usize = 0;
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer arena.deinit();
var buffer = std.io.bufferedWriter(std.io.getStdOut().writer());
const stdout = buffer.writer();
try stdout.print("{s:<8}{s:>45}{s:>20}\n", .{ "Day", "Result", "Time" });
comptime var day: u5 = 0;
inline while (day < DAYS) : (day += 1) {
comptime var buf: [5]u8 = undefined;
const day_str = comptime try std.fmt.bufPrint(&buf, "day{d:0>2}", .{day + 1});
timer.reset();
const first_res = @field(aoc2015, day_str).first(arena.allocator()) catch unreachable;
const first_time = timer.read() / std.time.ns_per_us;
time_sum += first_time;
if (@TypeOf(first_res) == Str) {
try stdout.print("{s}a: {s:>45}{d:>20} us\n", .{ day_str, first_res, first_time });
} else {
try stdout.print("{s}a: {d:>45}{d:>20} us\n", .{ day_str, first_res, first_time });
}
if (day < 24) {
timer.reset();
const second_res = @field(aoc2015, day_str).second(arena.allocator()) catch unreachable;
const second_time = timer.read() / std.time.ns_per_us;
time_sum += second_time;
if (@TypeOf(second_res) == Str) {
try stdout.print("{s}b: {s:>45}{d:>20} us\n", .{ day_str, second_res, second_time });
} else {
try stdout.print("{s}b: {d:>45}{d:>20} us\n", .{ day_str, second_res, second_time });
}
}
}
try stdout.print("Total time: {d} ms\n", .{time_sum / std.time.us_per_ms});
try buffer.flush();
}
const std = @import("std");
const PATH = "input/day01.txt";
pub fn first(allocator: ?std.mem.Allocator) !isize {
_ = allocator;
const input = @embedFile(PATH);
var ret: isize = 0;
for (input) |ch| {
switch (ch) {
'(' => ret += 1,
')' => ret -= 1,
else => {},
}
}
return ret;
}
pub fn second(allocator: ?std.mem.Allocator) !usize {
_ = allocator;
const input = @embedFile(PATH);
var floor: usize = 0;
for (input) |ch, idx| {
switch (ch) {
'(' => floor += 1,
')' => {
if (floor == 0) return idx + 1;
floor -= 1;
},
else => unreachable,
}
}
unreachable;
}
test "day01a" {
try std.testing.expectEqual(@as(isize, 74), try first(std.testing.allocator));
}
test "day01b" {
try std.testing.expectEqual(@as(usize, 1795), try second(std.testing.allocator));
}
const std = @import("std");
pub fn build(b: *std.build.Builder) void {
// Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options
// for restricting supported target set are available.
const target = b.standardTargetOptions(.{});
// Standard release options allow the person running `zig build` to select
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall.
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("aoc2015", "src/main.zig");
exe.setTarget(target);
exe.setBuildMode(mode);
exe.install();
const run_cmd = exe.run();
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run all solver");
run_step.dependOn(&run_cmd.step);
const exe_tests = b.addTest("src/main.zig");
exe_tests.setTarget(target);
exe_tests.setBuildMode(mode);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&exe_tests.step);
}
zig-cache
zig-out