diff options
Diffstat (limited to 'main.zig')
-rw-r--r-- | main.zig | 115 |
1 files changed, 64 insertions, 51 deletions
@@ -1,10 +1,72 @@ const std = @import("std"); +const Result = @import("util/aoc.zig").Result; + +const t = [_]struct { + day: type, + expect: []const Result, + input: []const u8, +}{ + .{ .day = @import("day_01.zig"), .input = @embedFile("inputs/day_01"), .expect = &[_]Result{ .{ .int = 69912 }, .{ .int = 208180 } } }, + .{ .day = @import("day_02.zig"), .input = @embedFile("inputs/day_02"), .expect = &[_]Result{ .{ .int = 12855 }, .{ .int = 13726 } } }, + .{ .day = @import("day_03.zig"), .input = @embedFile("inputs/day_03"), .expect = &[_]Result{ .{ .int = 8039 }, .{ .int = 2510 } } }, + .{ .day = @import("day_04.zig"), .input = @embedFile("inputs/day_04"), .expect = &[_]Result{ .{ .int = 483 }, .{ .int = 874 } } }, + .{ .day = @import("day_05.zig"), .input = @embedFile("inputs/day_05"), .expect = &[_]Result{ .{ .string = "SHMSDGZVC" }, .{ .string = "VRZGHDFBQ" } } }, +}; + +pub fn main() !void { + var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator); + defer arena.deinit(); + const allocator = arena.allocator(); + return run_all(allocator); +} test { std.testing.refAllDecls(@This()); } -fn solve(allocator: std.mem.Allocator, puzzle: anytype, input: []const u8) !i32 { +fn run_all(allocator: std.mem.Allocator) !void { + inline for (t) |aoc_day, day| { + std.debug.print("\n", .{}); + inline for (aoc_day.expect) |expect, idx| { + const fn_name = comptimePrint("puzzle_{d}", .{idx + 1}); + const puzzle = @field(aoc_day.day, fn_name); + const start_time = try std.time.Instant.now(); + + const result = try solve( + allocator, + puzzle, + aoc_day.input[0 .. aoc_day.input.len - 1], // remove the last '^\n$' + ); + defer result.deinit(allocator); + + const end_time = try std.time.Instant.now(); + const elapsed_us = end_time.since(start_time) / std.time.ns_per_us; + + std.debug.print("day={d}, input_bytes={d}, time={d}µs, ", .{ + day + 1, + aoc_day.input.len, + elapsed_us, + }); + + switch (result) { + .string => |s| std.debug.print("puzzle_{d}={s}", .{ idx + 1, s }), + .int => |i| std.debug.print("puzzle_{d}={d}", .{ idx + 1, i }), + } + + if (!expect.cmp(result)) { + switch (expect) { + .string => |s| std.debug.print(", expected={s}\n", .{s}), + .int => |i| std.debug.print(", expected={d}\n", .{i}), + } + return error.UnexpectedValue; + } + + std.debug.print("\n", .{}); + } + } +} + +fn solve(allocator: std.mem.Allocator, puzzle: anytype, input: []const u8) !Result { return blk: { switch (@typeInfo(@TypeOf(puzzle))) { .Fn => |f| { @@ -39,56 +101,7 @@ inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [std.fmt } test "test" { - const t = [_]struct { - day: type, - expect: []const i32, - input: []const u8, - }{ - .{ .day = @import("day-1/main.zig"), .input = @embedFile("day-1/input"), .expect = &[_]i32{ 69912, 208180 } }, - .{ .day = @import("day-2/main.zig"), .input = @embedFile("day-2/input"), .expect = &[_]i32{ 12855, 13726 } }, - .{ .day = @import("day-3/main.zig"), .input = @embedFile("day-3/input"), .expect = &[_]i32{ 8039, 2510 } }, - .{ .day = @import("day-4/main.zig"), .input = @embedFile("day-4/input"), .expect = &[_]i32{ 483, 874 } }, - }; - std.debug.print("\n", .{}); - - inline for (t) |aoc_day, day| { - std.debug.print("\n", .{}); - inline for (aoc_day.expect) |expected, idx| { - const fn_name = comptimePrint("puzzle_{d}", .{idx + 1}); - const puzzle = @field(aoc_day.day, fn_name); - const start_time = try std.time.Instant.now(); - - const result = try solve( - testing_allocator, - puzzle, - aoc_day.input[0 .. aoc_day.input.len - 1], // remove the last '^\n$' - ); - - const end_time = try std.time.Instant.now(); - const elapsed_us = end_time.since(start_time) / std.time.ns_per_us; - - if (result == expected) { - std.debug.print("day={d}, puzzle_{d}={d}, input_bytes={d}, time={d}µs\n", .{ - day + 1, - idx + 1, - result, - aoc_day.input.len, - elapsed_us, - }); - } else { - std.debug.print("day={d}, puzzle_{d}={d}, input_bytes={d}, time={d}µs, expected={d}\n", .{ - day + 1, - idx + 1, - result, - aoc_day.input.len, - elapsed_us, - expected, - }); - return error.UnexpectedValue; - } - } - } - + try run_all(testing_allocator); std.debug.print("\n", .{}); } |