Embedded file directly into the binary

This commit is contained in:
2024-12-08 19:00:05 +01:00
parent d2f66dc8e9
commit 2aa4647c25
16 changed files with 33 additions and 160 deletions
+2 -18
View File
@@ -1,5 +1,6 @@
const std = @import("std");
const print = std.debug.print;
const file = @embedFile("input");
const UP = Position{ .x = -1, .y = 0 };
const RIGHT = Position{ .x = 0, .y = 1 };
@@ -89,25 +90,8 @@ const Map = struct {
};
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer {
const deinit_status = gpa.deinit();
if (deinit_status == .leak) @panic("TEST FAIL");
}
// ========= Load file ===========
const file = try std.fs.cwd().openFile("day6/input", .{});
defer file.close();
const file_size = (try file.stat()).size;
const buffer = try allocator.alloc(u8, file_size);
defer allocator.free(buffer);
_ = try file.readAll(buffer);
// ========= Parse map ===========
var iter = std.mem.split(u8, buffer, "\n");
var iter = std.mem.split(u8, file, "\n");
var map = Map{};
for (0..132) |x| {
+2 -11
View File
@@ -1,5 +1,6 @@
const std = @import("std");
const print = std.debug.print;
const file = @embedFile("input");
const VisitedBy = struct { up: bool = false, down: bool = false, right: bool = false, left: bool = false };
@@ -211,18 +212,8 @@ pub fn main() !void {
if (deinit_status == .leak) @panic("TEST FAIL");
}
// ========= Load file ===========
const file = try std.fs.cwd().openFile("day6/input", .{});
defer file.close();
const file_size = (try file.stat()).size;
const buffer = try allocator.alloc(u8, file_size);
defer allocator.free(buffer);
_ = try file.readAll(buffer);
// ========= Parse map ===========
var iter = std.mem.split(u8, buffer, "\n");
var iter = std.mem.split(u8, file, "\n");
var map = try Map.init(allocator);
defer map.deinit();