Embed input files in binary
This commit is contained in:
parent
7c4b0f342b
commit
3ac598100c
@ -1,18 +1,13 @@
|
|||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
|
||||||
const MAX_BYTES = 2048 * 2048;
|
pub const input = @embedFile("input/day01.txt");
|
||||||
|
const test_input = @embedFile("input/day01_test1.txt");
|
||||||
|
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{ .safety = true }){};
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
pub fn solve_part1(filename: []const u8) !usize {
|
pub fn solve_part1(data: []const u8) !usize {
|
||||||
const input_file = try std.fs.cwd().openFile(filename, .{ .mode = std.fs.File.OpenMode.read_only });
|
var lines = std.mem.split(u8, data, "\n");
|
||||||
defer input_file.close();
|
|
||||||
|
|
||||||
const input = try input_file.readToEndAlloc(allocator, MAX_BYTES);
|
|
||||||
defer allocator.free(input);
|
|
||||||
|
|
||||||
var lines = std.mem.split(u8, input, "\n");
|
|
||||||
var largest_calories: usize = 0;
|
var largest_calories: usize = 0;
|
||||||
var calories: usize = 0;
|
var calories: usize = 0;
|
||||||
while (lines.next()) |line| {
|
while (lines.next()) |line| {
|
||||||
@ -29,14 +24,8 @@ pub fn solve_part1(filename: []const u8) !usize {
|
|||||||
return largest_calories;
|
return largest_calories;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn solve_part2(filename: []const u8) !usize {
|
pub fn solve_part2(data: []const u8) !usize {
|
||||||
const input_file = try std.fs.cwd().openFile(filename, .{ .mode = std.fs.File.OpenMode.read_only });
|
var lines = std.mem.split(u8, data, "\n");
|
||||||
defer input_file.close();
|
|
||||||
|
|
||||||
const input = try input_file.readToEndAlloc(allocator, MAX_BYTES);
|
|
||||||
defer allocator.free(input);
|
|
||||||
|
|
||||||
var lines = std.mem.split(u8, input, "\n");
|
|
||||||
var elves = std.ArrayList(usize).init(allocator);
|
var elves = std.ArrayList(usize).init(allocator);
|
||||||
defer elves.deinit();
|
defer elves.deinit();
|
||||||
var calories: usize = 0;
|
var calories: usize = 0;
|
||||||
@ -56,9 +45,9 @@ pub fn solve_part2(filename: []const u8) !usize {
|
|||||||
}
|
}
|
||||||
|
|
||||||
test "solves part1" {
|
test "solves part1" {
|
||||||
try std.testing.expectEqual(solve_part1("input/day01_test1.txt"), 24000);
|
try std.testing.expectEqual(solve_part1(test_input), 24000);
|
||||||
}
|
}
|
||||||
|
|
||||||
test "solves part2" {
|
test "solves part2" {
|
||||||
try std.testing.expectEqual(solve_part2("input/day01_test1.txt"), 45000);
|
try std.testing.expectEqual(solve_part2(test_input), 45000);
|
||||||
}
|
}
|
||||||
|
@ -8,8 +8,8 @@ pub fn main() !void {
|
|||||||
const stdout = bw.writer();
|
const stdout = bw.writer();
|
||||||
|
|
||||||
try stdout.print("Day 1\nPart 1: {}\nPart 2: {}\n\n", .{
|
try stdout.print("Day 1\nPart 1: {}\nPart 2: {}\n\n", .{
|
||||||
try day01.solve_part1("input/day01.txt"),
|
try day01.solve_part1(day01.input),
|
||||||
try day01.solve_part2("input/day01.txt"),
|
try day01.solve_part2(day01.input),
|
||||||
});
|
});
|
||||||
|
|
||||||
try bw.flush();
|
try bw.flush();
|
||||||
|
10
src/utils.zig
Normal file
10
src/utils.zig
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const MAX_BYTES = 2048 * 2048;
|
||||||
|
|
||||||
|
pub fn read_file(allocator: std.mem.Allocator, filename: []const u8) ![]const u8 {
|
||||||
|
const input_file = try std.fs.cwd().openFile(filename, .{ .mode = std.fs.File.OpenMode.read_only });
|
||||||
|
defer input_file.close();
|
||||||
|
|
||||||
|
return try input_file.readToEndAlloc(allocator, MAX_BYTES);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user