Skip to content

Commit

Permalink
Pass main allocator to all solution functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
ACSimon33 committed Dec 17, 2024
1 parent b6eb657 commit bb42f4a
Show file tree
Hide file tree
Showing 65 changed files with 385 additions and 197 deletions.
8 changes: 4 additions & 4 deletions 2024/00/zig_template/benchmarks/puzzle_benchmarks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const zig_template = @import("zig_template");
const puzzle_input = @embedFile("puzzle_input");

// Benchmark of part 1
fn task_1(_: std.mem.Allocator) void {
_ = zig_template.solution_1(puzzle_input) catch {};
fn task_1(allocator: std.mem.Allocator) void {
_ = zig_template.solution_1(puzzle_input, allocator) catch {};
}

// Benchmark of part 2
fn task_2(_: std.mem.Allocator) void {
_ = zig_template.solution_2(puzzle_input) catch {};
fn task_2(allocator: std.mem.Allocator) void {
_ = zig_template.solution_2(puzzle_input, allocator) catch {};
}

pub fn main() !void {
Expand Down
10 changes: 8 additions & 2 deletions 2024/00/zig_template/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ pub fn main() !void {
return;
}

const result_1 = zig_template.solution_1(file_content);
const result_1 = zig_template.solution_1(
file_content,
allocator,
);
try stdout.print("1. Solution: {!}\n", .{result_1});
try bw.flush();

const result_2 = zig_template.solution_2(file_content);
const result_2 = zig_template.solution_2(
file_content,
allocator,
);
try stdout.print("2. Solution: {!}\n", .{result_2});
try bw.flush();
}
17 changes: 12 additions & 5 deletions 2024/00/zig_template/src/zig_template.zig
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const ArrayList = std.ArrayList;
const string = []const u8;

/// Task 1 -
///
/// Arguments:
/// - `contents`: Input file contents.
/// - `main_allocator`: Base allocator for everything.
///
/// Returns:
/// - Solution for task 1.
pub fn solution_1(contents: string) !i32 {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
pub fn solution_1(contents: string, main_allocator: Allocator) !i32 {
var arena = std.heap.ArenaAllocator.init(main_allocator);
defer arena.deinit();

const allocator = arena.allocator();
Expand All @@ -23,12 +25,16 @@ pub fn solution_1(contents: string) !i32 {
///
/// Arguments:
/// - `contents`: Input file contents.
/// - `main_allocator`: Base allocator for everything.
///
/// Returns:
/// - Solution for task 2.
pub fn solution_2(contents: string) !i32 {
const lines = std.mem.split(u8, contents, "\n");
_ = lines;
pub fn solution_2(contents: string, main_allocator: Allocator) !i32 {
var arena = std.heap.ArenaAllocator.init(main_allocator);
defer arena.deinit();

const allocator = arena.allocator();
_ = try parse(contents, allocator);

return 1;
}
Expand All @@ -39,6 +45,7 @@ pub fn solution_2(contents: string) !i32 {
///
/// Arguments:
/// - `contents`: Input file contents.
/// - `main_allocator`: Base allocator for everything.
/// - `allocator`: Allocator for the containers.
///
/// Returns:
Expand Down
4 changes: 2 additions & 2 deletions 2024/00/zig_template/tests/example_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test "task_1" {
const example_input = @embedFile("example_input");
try testing.expectEqual(
0,
zig_template.solution_1(example_input),
zig_template.solution_1(example_input, std.testing.allocator),
);
}

Expand All @@ -16,6 +16,6 @@ test "task_2" {
const example_input = @embedFile("example_input");
try testing.expectEqual(
1,
zig_template.solution_2(example_input),
zig_template.solution_2(example_input, std.testing.allocator),
);
}
8 changes: 4 additions & 4 deletions 2024/01/historian_hysteria/benchmarks/puzzle_benchmarks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const historian_hysteria = @import("historian_hysteria");
const puzzle_input = @embedFile("puzzle_input");

// Benchmark of part 1
fn task_1(_: std.mem.Allocator) void {
_ = historian_hysteria.list_distance(puzzle_input) catch {};
fn task_1(allocator: std.mem.Allocator) void {
_ = historian_hysteria.list_distance(puzzle_input, allocator) catch {};
}

// Benchmark of part 2
fn task_2(_: std.mem.Allocator) void {
_ = historian_hysteria.list_similarity(puzzle_input) catch {};
fn task_2(allocator: std.mem.Allocator) void {
_ = historian_hysteria.list_similarity(puzzle_input, allocator) catch {};
}

pub fn main() !void {
Expand Down
11 changes: 7 additions & 4 deletions 2024/01/historian_hysteria/src/historian_hysteria.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const string = []const u8;
///
/// Arguments:
/// - `contents`: Input file contents.
/// - `main_allocator`: Base allocator for everything.
///
/// Returns:
/// - Distance of the two lists.
pub fn list_distance(contents: string) !u32 {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
pub fn list_distance(contents: string, main_allocator: Allocator) !u32 {
var arena = std.heap.ArenaAllocator.init(main_allocator);
defer arena.deinit();

const allocator = arena.allocator();
Expand All @@ -32,11 +33,12 @@ pub fn list_distance(contents: string) !u32 {
///
/// Arguments:
/// - `contents`: Input file contents.
/// - `main_allocator`: Base allocator for everything.
///
/// Returns:
/// - Similarity of the two lists.
pub fn list_similarity(contents: string) !i32 {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
pub fn list_similarity(contents: string, main_allocator: Allocator) !i32 {
var arena = std.heap.ArenaAllocator.init(main_allocator);
defer arena.deinit();

const allocator = arena.allocator();
Expand All @@ -57,6 +59,7 @@ pub fn list_similarity(contents: string) !i32 {
///
/// Arguments:
/// - `contents`: Input file contents.
/// - `main_allocator`: Base allocator for everything.
/// - `allocator`: Allocator for the containers.
///
/// Returns:
Expand Down
10 changes: 8 additions & 2 deletions 2024/01/historian_hysteria/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ pub fn main() !void {
return;
}

const result_1 = historian_hysteria.list_distance(file_content);
const result_1 = historian_hysteria.list_distance(
file_content,
allocator,
);
try stdout.print("List distance: {!}\n", .{result_1});
try bw.flush();

const result_2 = historian_hysteria.list_similarity(file_content);
const result_2 = historian_hysteria.list_similarity(
file_content,
allocator,
);
try stdout.print("List similarity: {!}\n", .{result_2});
try bw.flush();
}
4 changes: 2 additions & 2 deletions 2024/01/historian_hysteria/tests/example_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test "task_1" {
const example_input = @embedFile("example_input");
try testing.expectEqual(
11,
historian_hysteria.list_distance(example_input),
historian_hysteria.list_distance(example_input, std.testing.allocator),
);
}

Expand All @@ -16,6 +16,6 @@ test "task_2" {
const example_input = @embedFile("example_input");
try testing.expectEqual(
31,
historian_hysteria.list_similarity(example_input),
historian_hysteria.list_similarity(example_input, std.testing.allocator),
);
}
8 changes: 4 additions & 4 deletions 2024/02/red_nosed_reports/benchmarks/puzzle_benchmarks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const red_nosed_reports = @import("red_nosed_reports");
const puzzle_input = @embedFile("puzzle_input");

// Benchmark of part 1
fn task_1(_: std.mem.Allocator) void {
_ = red_nosed_reports.number_of_safe_reports(puzzle_input) catch {};
fn task_1(allocator: std.mem.Allocator) void {
_ = red_nosed_reports.number_of_safe_reports(puzzle_input, allocator) catch {};
}

// Benchmark of part 2
fn task_2(_: std.mem.Allocator) void {
_ = red_nosed_reports.number_of_partially_safe_reports(puzzle_input) catch {};
fn task_2(allocator: std.mem.Allocator) void {
_ = red_nosed_reports.number_of_partially_safe_reports(puzzle_input, allocator) catch {};
}

pub fn main() !void {
Expand Down
10 changes: 8 additions & 2 deletions 2024/02/red_nosed_reports/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ pub fn main() !void {
return;
}

const safe_reports = red_nosed_reports.number_of_safe_reports(file_content);
const safe_reports = red_nosed_reports.number_of_safe_reports(
file_content,
allocator,
);
try stdout.print("Number of safe reports: {!}\n", .{safe_reports});
try bw.flush();

const partially_safe_reports = red_nosed_reports.number_of_partially_safe_reports(file_content);
const partially_safe_reports = red_nosed_reports.number_of_partially_safe_reports(
file_content,
allocator,
);
try stdout.print("Number of partially safe reports: {!}\n", .{partially_safe_reports});
try bw.flush();
}
11 changes: 7 additions & 4 deletions 2024/02/red_nosed_reports/src/red_nosed_reports.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const string = []const u8;
///
/// Arguments:
/// - `contents`: Input file contents.
/// - `main_allocator`: Base allocator for everything.
///
/// Returns:
/// - Number of fully safe reports.
pub fn number_of_safe_reports(contents: string) !i32 {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
pub fn number_of_safe_reports(contents: string, main_allocator: Allocator) !i32 {
var arena = std.heap.ArenaAllocator.init(main_allocator);
defer arena.deinit();

const allocator = arena.allocator();
Expand All @@ -32,11 +33,12 @@ pub fn number_of_safe_reports(contents: string) !i32 {
///
/// Arguments:
/// - `contents`: Input file contents.
/// - `main_allocator`: Base allocator for everything.
///
/// Returns:
/// - Number of fully safe reports.
pub fn number_of_partially_safe_reports(contents: string) !i32 {
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
pub fn number_of_partially_safe_reports(contents: string, main_allocator: Allocator) !i32 {
var arena = std.heap.ArenaAllocator.init(main_allocator);
defer arena.deinit();

const allocator = arena.allocator();
Expand Down Expand Up @@ -67,6 +69,7 @@ const Report = struct {
///
/// Arguments:
/// - `contents`: Input file contents.
/// - `main_allocator`: Base allocator for everything.
/// - `allocator`: Allocator for the containers.
///
/// Returns:
Expand Down
4 changes: 2 additions & 2 deletions 2024/02/red_nosed_reports/tests/example_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test "task_1" {
const example_input = @embedFile("example_input");
try testing.expectEqual(
2,
red_nosed_reports.number_of_safe_reports(example_input),
red_nosed_reports.number_of_safe_reports(example_input, std.testing.allocator),
);
}

Expand All @@ -16,6 +16,6 @@ test "task_2" {
const example_input = @embedFile("example_input");
try testing.expectEqual(
4,
red_nosed_reports.number_of_partially_safe_reports(example_input),
red_nosed_reports.number_of_partially_safe_reports(example_input, std.testing.allocator),
);
}
8 changes: 4 additions & 4 deletions 2024/03/mull_it_over/benchmarks/puzzle_benchmarks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const mull_it_over = @import("mull_it_over");
const puzzle_input = @embedFile("puzzle_input");

// Benchmark of part 1
fn task_1(_: std.mem.Allocator) void {
_ = mull_it_over.sum_of_multiplication_instructions(puzzle_input) catch {};
fn task_1(allocator: std.mem.Allocator) void {
_ = mull_it_over.sum_of_multiplication_instructions(puzzle_input, allocator) catch {};
}

// Benchmark of part 2
fn task_2(_: std.mem.Allocator) void {
_ = mull_it_over.conditional_sum_of_multiplication_instructions(puzzle_input) catch {};
fn task_2(allocator: std.mem.Allocator) void {
_ = mull_it_over.conditional_sum_of_multiplication_instructions(puzzle_input, allocator) catch {};
}

pub fn main() !void {
Expand Down
10 changes: 8 additions & 2 deletions 2024/03/mull_it_over/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,17 @@ pub fn main() !void {
return;
}

const result_1 = mull_it_over.sum_of_multiplication_instructions(file_content);
const result_1 = mull_it_over.sum_of_multiplication_instructions(
file_content,
allocator,
);
try stdout.print("Sum of all multiplication instructions: {!}\n", .{result_1});
try bw.flush();

const result_2 = mull_it_over.conditional_sum_of_multiplication_instructions(file_content);
const result_2 = mull_it_over.conditional_sum_of_multiplication_instructions(
file_content,
allocator,
);
try stdout.print("Conditional sum of multiplication instructions: {!}\n", .{result_2});
try bw.flush();
}
6 changes: 4 additions & 2 deletions 2024/03/mull_it_over/src/mull_it_over.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ const string = []const u8;
///
/// Arguments:
/// - `contents`: Input file contents.
/// - `main_allocator`: Base allocator for everything.
///
/// Returns:
/// - Sum of all multiplications.
pub fn sum_of_multiplication_instructions(contents: string) !u32 {
pub fn sum_of_multiplication_instructions(contents: string, _: Allocator) !u32 {
const min_instruction_len: usize = 8;

var product_sum: u32 = 0;
Expand All @@ -28,10 +29,11 @@ pub fn sum_of_multiplication_instructions(contents: string) !u32 {
///
/// Arguments:
/// - `contents`: Input file contents.
/// - `main_allocator`: Base allocator for everything.
///
/// Returns:
/// - Sum of all active multiplications.
pub fn conditional_sum_of_multiplication_instructions(contents: string) !u32 {
pub fn conditional_sum_of_multiplication_instructions(contents: string, _: Allocator) !u32 {
const min_instruction_len: usize = 8;

var product_sum: u32 = 0;
Expand Down
6 changes: 3 additions & 3 deletions 2024/03/mull_it_over/tests/example_tests.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ test "task_1_input_1" {
const example_input = @embedFile("example_input_1");
try testing.expectEqual(
161,
mull_it_over.sum_of_multiplication_instructions(example_input),
mull_it_over.sum_of_multiplication_instructions(example_input, std.testing.allocator),
);
}

Expand All @@ -16,14 +16,14 @@ test "task_2_input_1" {
const example_input = @embedFile("example_input_1");
try testing.expectEqual(
161,
mull_it_over.conditional_sum_of_multiplication_instructions(example_input),
mull_it_over.conditional_sum_of_multiplication_instructions(example_input, std.testing.allocator),
);
}

test "task_2_input_2" {
const example_input = @embedFile("example_input_2");
try testing.expectEqual(
48,
mull_it_over.conditional_sum_of_multiplication_instructions(example_input),
mull_it_over.conditional_sum_of_multiplication_instructions(example_input, std.testing.allocator),
);
}
8 changes: 4 additions & 4 deletions 2024/04/ceres_search/benchmarks/puzzle_benchmarks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const ceres_search = @import("ceres_search");
const puzzle_input = @embedFile("puzzle_input");

// Benchmark of part 1
fn task_1(_: std.mem.Allocator) void {
_ = ceres_search.xmas_count(puzzle_input) catch {};
fn task_1(allocator: std.mem.Allocator) void {
_ = ceres_search.xmas_count(puzzle_input, allocator) catch {};
}

// Benchmark of part 2
fn task_2(_: std.mem.Allocator) void {
_ = ceres_search.x_mas_count(puzzle_input) catch {};
fn task_2(allocator: std.mem.Allocator) void {
_ = ceres_search.x_mas_count(puzzle_input, allocator) catch {};
}

pub fn main() !void {
Expand Down
Loading

0 comments on commit bb42f4a

Please sign in to comment.