From 8817203517907ef4248bde7474e6fb566515d6a7 Mon Sep 17 00:00:00 2001 From: Christian Segundo Date: Sun, 4 Dec 2022 15:57:15 +0100 Subject: remove naive intersection impl --- main.zig | 44 ++++++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 12 deletions(-) (limited to 'main.zig') diff --git a/main.zig b/main.zig index a10e87e..6609a2f 100644 --- a/main.zig +++ b/main.zig @@ -4,7 +4,7 @@ test { std.testing.refAllDecls(@This()); } -fn solve(allocator: std.mem.Allocator, puzzle: anytype, input: []u8) !i32 { +fn solve(allocator: std.mem.Allocator, puzzle: anytype, input: []const u8) !i32 { return blk: { switch (@typeInfo(@TypeOf(puzzle))) { .Fn => |f| { @@ -27,7 +27,6 @@ fn solve(allocator: std.mem.Allocator, puzzle: anytype, input: []u8) !i32 { } const testing_allocator = std.testing.allocator; -const slurp = @import("util/file.zig").slurp; // same as the real comptimePrint but inlined, see: https://github.com/ziglang/zig/issues/12635 inline fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [std.fmt.count(fmt, args):0]u8 { @@ -43,29 +42,50 @@ test "test" { const t = [_]struct { day: type, expect: []const i32, + input: []const u8, }{ - .{ .day = @import("day-1/main.zig"), .expect = &[_]i32{ 69912, 208180 } }, - .{ .day = @import("day-2/main.zig"), .expect = &[_]i32{ 12855, 13726 } }, - .{ .day = @import("day-3/main.zig"), .expect = &[_]i32{ 8039, 2510 } }, - .{ .day = @import("day-4/main.zig"), .expect = &[_]i32{ 483, 874 } }, + .{ .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", .{}); - const file_buffer = try slurp(std.testing.allocator, comptimePrint("./day-{d}/input", .{day + 1})); - defer testing_allocator.free(file_buffer); - 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; - const result = try solve(testing_allocator, puzzle, file_buffer); if (result == expected) { - std.debug.print("day={d}, puzzle_{d}={d}\n", .{ day + 1, idx + 1, result }); + 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}, expected={d}\n", .{ day + 1, idx + 1, result, expected }); + 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; } } } -- cgit v1.2.3