1
0

Embedded file directly into the binary

This commit is contained in:
Adrien Bouvais 2024-12-08 19:00:05 +01:00
parent d2f66dc8e9
commit 2aa4647c25
16 changed files with 33 additions and 160 deletions

View File

@ -1,18 +1,14 @@
const std = @import("std");
const print = std.debug.print;
var file_buf: [1024 * 1024]u8 = undefined; // The file do 20kB, I give it 1MB
const file = @embedFile("input");
pub fn main() !void {
var left: [1000]i32 = undefined;
var right: [1000]i32 = undefined;
const file = try std.fs.cwd().openFile("day1/input", .{});
defer file.close();
const len = try file.readAll(&file_buf);
var i: u32 = 0;
var iter = std.mem.split(u8, file_buf[0..len], "\n");
var iter = std.mem.split(u8, file, "\n");
while (iter.next()) |line| {
if (i == 1000) continue;
left[i] = try std.fmt.parseInt(i32, line[0..5], 10);

View File

@ -1,18 +1,14 @@
const std = @import("std");
const print = std.debug.print;
var file_buf: [1024 * 1024]u8 = undefined; // The file do 20kB, I give it 1MB
const file = @embedFile("input");
pub fn main() !void {
var left: [1000]i32 = undefined;
var right: [1000]i32 = undefined;
const file = try std.fs.cwd().openFile("day1/input", .{});
defer file.close();
const len = try file.readAll(&file_buf);
var i: u32 = 0;
var iter = std.mem.split(u8, file_buf[0..len], "\n");
var iter = std.mem.split(u8, file, "\n");
while (iter.next()) |line| {
if (i == 1000) continue;
left[i] = try std.fmt.parseInt(i32, line[0..5], 10);

View File

@ -1,18 +1,14 @@
const std = @import("std");
const print = std.debug.print;
const file = @embedFile("input");
const State = enum { First, Second, Incr, Decr }; // Increase Decrease
var file_buf: [1024 * 1024]u8 = undefined; // The file do 20kB, I give it 1MB
var second_buf: [64]u8 = undefined;
pub fn main() !void {
const file = try std.fs.cwd().openFile("day2/input", .{});
defer file.close();
const len = try file.readAll(&file_buf);
var total_safe: usize = 0;
var iter = std.mem.split(u8, file_buf[0..len], "\n");
var iter = std.mem.split(u8, file, "\n");
while (iter.next()) |line| {
if (std.mem.eql(u8, line, "")) continue;

View File

@ -1,18 +1,14 @@
const std = @import("std");
const print = std.debug.print;
const file = @embedFile("input");
const State = enum { First, Second, Incr, Decr }; // Increase Decrease
var file_buf: [1024 * 1024]u8 = undefined; // The file do 20kB, I give it 1MB
var second_buf: [64]u8 = undefined;
pub fn main() !void {
const file = try std.fs.cwd().openFile("day2/input", .{});
defer file.close();
const len = try file.readAll(&file_buf);
var total_safe: usize = 0;
var iter = std.mem.split(u8, file_buf[0..len], "\n");
var iter = std.mem.split(u8, file, "\n");
while (iter.next()) |line| {
if (std.mem.eql(u8, line, "")) continue;

View File

@ -1,5 +1,6 @@
const std = @import("std");
const print = std.debug.print;
const file = @embedFile("input");
const LookingFor = enum {
m,
@ -10,14 +11,8 @@ const LookingFor = enum {
Y,
};
var file_buf: [1024 * 1024]u8 = undefined; // The file do 20kB, I give it 1MB
pub fn main() !void {
const file = try std.fs.cwd().openFile("day3/input", .{});
defer file.close();
const len = try file.readAll(&file_buf);
try std.testing.expectEqual(190604937, try parse(file_buf[0..len]));
try std.testing.expectEqual(190604937, try parse(file));
}
fn parse(input: []const u8) !u32 {

View File

@ -1,5 +1,6 @@
const std = @import("std");
const print = std.debug.print;
const file = @embedFile("input");
const LookingFor = enum {
m_or_d,
@ -16,15 +17,8 @@ const LookingFor = enum {
r_brace_do,
};
var file_buf: [1024 * 1024]u8 = undefined; // The file do 20kB, I give it 1MB
pub fn main() !void {
const file = try std.fs.cwd().openFile("day3/input", .{});
defer file.close();
const len = try file.readAll(&file_buf);
try std.testing.expectEqual(82857512, try parse(file_buf[0..len]));
try std.testing.expectEqual(82857512, try parse(file));
}
fn parse(input: []const u8) !u32 {

View File

@ -1,7 +1,7 @@
const std = @import("std");
const print = std.debug.print;
const file = @embedFile("input");
var file_buf: [140 * 141]u8 = undefined;
var matrice: [143][143]u8 = undefined;
const masks = [_][4][4]u8{
[4][4]u8{
@ -81,12 +81,7 @@ fn setMatrice(c: u8) void {
}
fn fillMatrice() !void {
const file = try std.fs.cwd().openFile("day4/input", .{});
defer file.close();
_ = try file.readAll(&file_buf);
var iter = std.mem.split(u8, &file_buf, "\n");
var iter = std.mem.split(u8, file, "\n");
var x: usize = 1;
while (iter.next()) |line| {

View File

@ -1,7 +1,7 @@
const std = @import("std");
const print = std.debug.print;
const file = @embedFile("input");
var file_buf: [140 * 141]u8 = undefined;
var matrice: [142][142]u8 = undefined;
const masks = [_][3][3]u8{
[3][3]u8{
@ -53,12 +53,7 @@ fn setMatrice(c: u8) void {
}
fn fillMatrice() !void {
const file = try std.fs.cwd().openFile("day4/input", .{});
defer file.close();
_ = try file.readAll(&file_buf);
var iter = std.mem.split(u8, &file_buf, "\n");
var iter = std.mem.split(u8, file, "\n");
var x: usize = 1;
while (iter.next()) |line| {

View File

@ -1,5 +1,6 @@
const std = @import("std");
const print = std.debug.print;
const file = @embedFile("input");
const Rule = struct {
left: usize,
@ -51,21 +52,11 @@ pub fn main() !void {
if (deinit_status == .leak) @panic("TEST FAIL");
}
// ========= Load file ===========
const file = try std.fs.cwd().openFile("day5/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 Rules ===========
var bad_rules = std.AutoHashMap(Rule, void).init(allocator);
defer bad_rules.deinit();
var iter = std.mem.split(u8, buffer, "\n");
var iter = std.mem.split(u8, file, "\n");
while (iter.next()) |line| {
if (std.mem.eql(u8, line, "")) break;

View File

@ -1,5 +1,6 @@
const std = @import("std");
const print = std.debug.print;
const file = @embedFile("input");
const Rule = struct {
left: usize,
@ -51,21 +52,11 @@ pub fn main() !void {
if (deinit_status == .leak) @panic("TEST FAIL");
}
// ========= Load file ===========
const file = try std.fs.cwd().openFile("day5/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 Rules ===========
var bad_rules = std.AutoHashMap(Rule, void).init(allocator);
defer bad_rules.deinit();
var iter = std.mem.split(u8, buffer, "\n");
var iter = std.mem.split(u8, file, "\n");
while (iter.next()) |line| {
if (std.mem.eql(u8, line, "")) break;

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| {

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();

View File

@ -1,5 +1,6 @@
const std = @import("std");
const print = std.debug.print;
const file = @embedFile("input");
// Look like the complexity is 2 ^ (n - 1)
// with n len of a int list
@ -13,24 +14,7 @@ const Operator = struct {
pub fn main() !void {
// const test_value = "190: 10 19\n3267: 81 40 27\n83: 17 5\n156: 15 6\n7290: 6 8 6 15\n161011: 16 10 13\n192: 17 8 14\n21037: 9 7 18 13\n292: 11 6 16 20";
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer {
const deinit_status = gpa.deinit();
if (deinit_status == .leak) @panic("TEST FAIL");
}
const file = try std.fs.cwd().openFile("day7/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);
var iter = std.mem.split(u8, buffer[0..file_size], "\n");
var iter = std.mem.split(u8, file, "\n");
var total: usize = 0;
while (iter.next()) |line| {
if (std.mem.eql(u8, line, "")) continue;

View File

@ -1,10 +1,11 @@
const std = @import("std");
const print = std.debug.print;
const file = @embedFile("input");
// Look like the complexity is 2 ^ (n - 1)
// with n len of a int list
//
// So that mean I need to do
// Very happy with this solution, I juste needed to update like 5 things to make part 2 work
const Operator = struct {
value: usize,
@ -14,23 +15,7 @@ const Operator = struct {
pub fn main() !void {
// const test_value = "190: 10 19\n3267: 81 40 27\n83: 17 5\n156: 15 6\n7290: 6 8 6 15\n161011: 16 10 13\n192: 17 8 14\n21037: 9 7 18 13\n292: 11 6 16 20";
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const allocator = gpa.allocator();
defer {
const deinit_status = gpa.deinit();
if (deinit_status == .leak) @panic("TEST FAIL");
}
const file = try std.fs.cwd().openFile("day7/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);
var iter = std.mem.split(u8, buffer[0..file_size], "\n");
var iter = std.mem.split(u8, file, "\n");
var total: usize = 0;
while (iter.next()) |line| {
if (std.mem.eql(u8, line, "")) continue;

View File

@ -1,5 +1,6 @@
const std = @import("std");
const print = std.debug.print;
const file = @embedFile("input");
const MAP_SIZE = 50;
@ -110,18 +111,9 @@ pub fn main() !void {
if (deinit_status == .leak) @panic("TEST FAIL");
}
const file = try std.fs.cwd().openFile("day8/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);
var map = Map{ .unique_antenna = std.AutoHashMap(u8, usize).init(allocator) };
defer map.unique_antenna.deinit();
var iter = std.mem.split(u8, buffer, "\n");
var iter = std.mem.split(u8, file, "\n");
var x: usize = 0;
while (iter.next()) |line| {
if (std.mem.eql(u8, line, "")) continue;

View File

@ -1,5 +1,6 @@
const std = @import("std");
const print = std.debug.print;
const file = @embedFile("input");
// Agaain happy with the result, only needed to change few things
@ -120,18 +121,9 @@ pub fn main() !void {
if (deinit_status == .leak) @panic("TEST FAIL");
}
const file = try std.fs.cwd().openFile("day8/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);
var map = Map{ .unique_antenna = std.AutoHashMap(u8, usize).init(allocator) };
defer map.unique_antenna.deinit();
var iter = std.mem.split(u8, buffer, "\n");
var iter = std.mem.split(u8, file, "\n");
var x: usize = 0;
while (iter.next()) |line| {
if (std.mem.eql(u8, line, "")) continue;