mirror of
https://codeberg.org/andyscott/advent-of-code.git
synced 2024-12-21 17:13:10 -05:00
2024: move source files to src directory
This commit is contained in:
parent
98c454c542
commit
184e49c994
5 changed files with 53 additions and 22 deletions
|
@ -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
52
2024/src/util.zig
Normal 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;
|
||||
}
|
|
@ -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;
|
||||
}
|
Loading…
Reference in a new issue