diff --git a/2024/build.zig b/2024/build.zig index 8fd0949..101c4dc 100644 --- a/2024/build.zig +++ b/2024/build.zig @@ -11,7 +11,7 @@ pub fn build(b: *std.Build) void { var day: u8 = 1; while (day <= num_days) : (day += 1) { const name = b.fmt("day{d}", .{day}); - const file = b.fmt("{s}.zig", .{name}); + const file = b.fmt("src/{s}.zig", .{name}); const exe = b.addExecutable(.{ .name = name, diff --git a/2024/day1.zig b/2024/src/day1.zig similarity index 100% rename from 2024/day1.zig rename to 2024/src/day1.zig diff --git a/2024/day2.zig b/2024/src/day2.zig similarity index 100% rename from 2024/day2.zig rename to 2024/src/day2.zig diff --git a/2024/src/util.zig b/2024/src/util.zig new file mode 100644 index 0000000..c8288b3 --- /dev/null +++ b/2024/src/util.zig @@ -0,0 +1,52 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; +const TokenIterator = std.mem.TokenIterator; + +// Memory +var gpa = std.heap.GeneralPurposeAllocator(.{}){}; +pub const alloc = gpa.allocator(); + +// Logging +pub const log = std.log.default; + +// Functions +pub fn tokenizeAllInput(allocator: Allocator, outputType: type, day: u8) !TokenIterator { + var fileName: [16]u8 = undefined; + const inputFile = try std.fmt.bufPrint(&fileName, "day{d}.txt", .{day}); + + var dir = try std.fs.cwd().openDir("../data", .{}); + defer dir.close(); + + const file = try dir.openFile(inputFile, .{}); + defer file.close(); + + const stat = try file.stat(); + const input = try file.reader().readAllAlloc(allocator, stat.size); + return inputToArrayList(outputType, allocator, input); +} + +pub fn readAllInput(day: u8, allocator: std.mem.Allocator) ![]const u8 { + var fileName: [16]u8 = undefined; + const inputFile = try std.fmt.bufPrint(&fileName, "day{d}.txt", .{day}); + + var dir = try std.fs.cwd().openDir("../data", .{}); + defer dir.close(); + + const file = try dir.openFile(inputFile, .{}); + defer file.close(); + + const stat = try file.stat(); + return try file.reader().readAllAlloc(allocator, stat.size); +} + +pub fn inputToArrayList(comptime T: type, allocator: std.mem.Allocator, input: []const u8) !std.ArrayList(T) { + var out = std.ArrayList(T).init(allocator); + errdefer out.deinit(); + + var items = std.mem.tokenizeScalar(u8, input, ' '); + while (items.next()) |item| { + try out.append(try std.fmt.parseInt(T, item, 10)); + } + + return out; +} diff --git a/2024/util.zig b/2024/util.zig deleted file mode 100644 index e4e9d03..0000000 --- a/2024/util.zig +++ /dev/null @@ -1,21 +0,0 @@ -const std = @import("std"); -const print = std.debug.print; - -pub fn readAllInput(day: u8, allocator: std.mem.Allocator) ![]const u8 { - var fileName: [16]u8 = undefined; - const inputFile = try std.fmt.bufPrint(&fileName, "day{d}.txt", .{day}); - const file = try std.fs.cwd().openFile(inputFile, .{}); - defer file.close(); - const stat = try file.stat(); - return try file.reader().readAllAlloc(allocator, stat.size); -} - -pub fn inputToArrayList(comptime T: type, allocator: std.mem.Allocator, input: []const u8) !std.ArrayList(T) { - var out = std.ArrayList(T).init(allocator); - errdefer out.deinit(); - var items = std.mem.tokenizeScalar(u8, input, ' '); - while (items.next()) |item| { - try out.append(try std.fmt.parseInt(T, item, 10)); - } - return out; -}