2024: move source files to src directory

This commit is contained in:
Andrew Scott 2024-12-11 02:34:11 -05:00
parent 98c454c542
commit 184e49c994
Signed by: a
GPG key ID: 7CD5A5977E4931C1
5 changed files with 53 additions and 22 deletions

View file

@ -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,

52
2024/src/util.zig Normal file
View file

@ -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;
}

View file

@ -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;
}