Skip to content

Commit bb42f4a

Browse files
committed
Pass main allocator to all solution functions.
1 parent b6eb657 commit bb42f4a

File tree

65 files changed

+385
-197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+385
-197
lines changed

2024/00/zig_template/benchmarks/puzzle_benchmarks.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const zig_template = @import("zig_template");
55
const puzzle_input = @embedFile("puzzle_input");
66

77
// Benchmark of part 1
8-
fn task_1(_: std.mem.Allocator) void {
9-
_ = zig_template.solution_1(puzzle_input) catch {};
8+
fn task_1(allocator: std.mem.Allocator) void {
9+
_ = zig_template.solution_1(puzzle_input, allocator) catch {};
1010
}
1111

1212
// Benchmark of part 2
13-
fn task_2(_: std.mem.Allocator) void {
14-
_ = zig_template.solution_2(puzzle_input) catch {};
13+
fn task_2(allocator: std.mem.Allocator) void {
14+
_ = zig_template.solution_2(puzzle_input, allocator) catch {};
1515
}
1616

1717
pub fn main() !void {

2024/00/zig_template/src/main.zig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@ pub fn main() !void {
5050
return;
5151
}
5252

53-
const result_1 = zig_template.solution_1(file_content);
53+
const result_1 = zig_template.solution_1(
54+
file_content,
55+
allocator,
56+
);
5457
try stdout.print("1. Solution: {!}\n", .{result_1});
5558
try bw.flush();
5659

57-
const result_2 = zig_template.solution_2(file_content);
60+
const result_2 = zig_template.solution_2(
61+
file_content,
62+
allocator,
63+
);
5864
try stdout.print("2. Solution: {!}\n", .{result_2});
5965
try bw.flush();
6066
}

2024/00/zig_template/src/zig_template.zig

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
const std = @import("std");
22
const Allocator = std.mem.Allocator;
3+
const ArrayList = std.ArrayList;
34
const string = []const u8;
45

56
/// Task 1 -
67
///
78
/// Arguments:
89
/// - `contents`: Input file contents.
10+
/// - `main_allocator`: Base allocator for everything.
911
///
1012
/// Returns:
1113
/// - Solution for task 1.
12-
pub fn solution_1(contents: string) !i32 {
13-
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
14+
pub fn solution_1(contents: string, main_allocator: Allocator) !i32 {
15+
var arena = std.heap.ArenaAllocator.init(main_allocator);
1416
defer arena.deinit();
1517

1618
const allocator = arena.allocator();
@@ -23,12 +25,16 @@ pub fn solution_1(contents: string) !i32 {
2325
///
2426
/// Arguments:
2527
/// - `contents`: Input file contents.
28+
/// - `main_allocator`: Base allocator for everything.
2629
///
2730
/// Returns:
2831
/// - Solution for task 2.
29-
pub fn solution_2(contents: string) !i32 {
30-
const lines = std.mem.split(u8, contents, "\n");
31-
_ = lines;
32+
pub fn solution_2(contents: string, main_allocator: Allocator) !i32 {
33+
var arena = std.heap.ArenaAllocator.init(main_allocator);
34+
defer arena.deinit();
35+
36+
const allocator = arena.allocator();
37+
_ = try parse(contents, allocator);
3238

3339
return 1;
3440
}
@@ -39,6 +45,7 @@ pub fn solution_2(contents: string) !i32 {
3945
///
4046
/// Arguments:
4147
/// - `contents`: Input file contents.
48+
/// - `main_allocator`: Base allocator for everything.
4249
/// - `allocator`: Allocator for the containers.
4350
///
4451
/// Returns:

2024/00/zig_template/tests/example_tests.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test "task_1" {
77
const example_input = @embedFile("example_input");
88
try testing.expectEqual(
99
0,
10-
zig_template.solution_1(example_input),
10+
zig_template.solution_1(example_input, std.testing.allocator),
1111
);
1212
}
1313

@@ -16,6 +16,6 @@ test "task_2" {
1616
const example_input = @embedFile("example_input");
1717
try testing.expectEqual(
1818
1,
19-
zig_template.solution_2(example_input),
19+
zig_template.solution_2(example_input, std.testing.allocator),
2020
);
2121
}

2024/01/historian_hysteria/benchmarks/puzzle_benchmarks.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const historian_hysteria = @import("historian_hysteria");
55
const puzzle_input = @embedFile("puzzle_input");
66

77
// Benchmark of part 1
8-
fn task_1(_: std.mem.Allocator) void {
9-
_ = historian_hysteria.list_distance(puzzle_input) catch {};
8+
fn task_1(allocator: std.mem.Allocator) void {
9+
_ = historian_hysteria.list_distance(puzzle_input, allocator) catch {};
1010
}
1111

1212
// Benchmark of part 2
13-
fn task_2(_: std.mem.Allocator) void {
14-
_ = historian_hysteria.list_similarity(puzzle_input) catch {};
13+
fn task_2(allocator: std.mem.Allocator) void {
14+
_ = historian_hysteria.list_similarity(puzzle_input, allocator) catch {};
1515
}
1616

1717
pub fn main() !void {

2024/01/historian_hysteria/src/historian_hysteria.zig

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ const string = []const u8;
77
///
88
/// Arguments:
99
/// - `contents`: Input file contents.
10+
/// - `main_allocator`: Base allocator for everything.
1011
///
1112
/// Returns:
1213
/// - Distance of the two lists.
13-
pub fn list_distance(contents: string) !u32 {
14-
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
14+
pub fn list_distance(contents: string, main_allocator: Allocator) !u32 {
15+
var arena = std.heap.ArenaAllocator.init(main_allocator);
1516
defer arena.deinit();
1617

1718
const allocator = arena.allocator();
@@ -32,11 +33,12 @@ pub fn list_distance(contents: string) !u32 {
3233
///
3334
/// Arguments:
3435
/// - `contents`: Input file contents.
36+
/// - `main_allocator`: Base allocator for everything.
3537
///
3638
/// Returns:
3739
/// - Similarity of the two lists.
38-
pub fn list_similarity(contents: string) !i32 {
39-
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
40+
pub fn list_similarity(contents: string, main_allocator: Allocator) !i32 {
41+
var arena = std.heap.ArenaAllocator.init(main_allocator);
4042
defer arena.deinit();
4143

4244
const allocator = arena.allocator();
@@ -57,6 +59,7 @@ pub fn list_similarity(contents: string) !i32 {
5759
///
5860
/// Arguments:
5961
/// - `contents`: Input file contents.
62+
/// - `main_allocator`: Base allocator for everything.
6063
/// - `allocator`: Allocator for the containers.
6164
///
6265
/// Returns:

2024/01/historian_hysteria/src/main.zig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@ pub fn main() !void {
5050
return;
5151
}
5252

53-
const result_1 = historian_hysteria.list_distance(file_content);
53+
const result_1 = historian_hysteria.list_distance(
54+
file_content,
55+
allocator,
56+
);
5457
try stdout.print("List distance: {!}\n", .{result_1});
5558
try bw.flush();
5659

57-
const result_2 = historian_hysteria.list_similarity(file_content);
60+
const result_2 = historian_hysteria.list_similarity(
61+
file_content,
62+
allocator,
63+
);
5864
try stdout.print("List similarity: {!}\n", .{result_2});
5965
try bw.flush();
6066
}

2024/01/historian_hysteria/tests/example_tests.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ test "task_1" {
77
const example_input = @embedFile("example_input");
88
try testing.expectEqual(
99
11,
10-
historian_hysteria.list_distance(example_input),
10+
historian_hysteria.list_distance(example_input, std.testing.allocator),
1111
);
1212
}
1313

@@ -16,6 +16,6 @@ test "task_2" {
1616
const example_input = @embedFile("example_input");
1717
try testing.expectEqual(
1818
31,
19-
historian_hysteria.list_similarity(example_input),
19+
historian_hysteria.list_similarity(example_input, std.testing.allocator),
2020
);
2121
}

2024/02/red_nosed_reports/benchmarks/puzzle_benchmarks.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ const red_nosed_reports = @import("red_nosed_reports");
55
const puzzle_input = @embedFile("puzzle_input");
66

77
// Benchmark of part 1
8-
fn task_1(_: std.mem.Allocator) void {
9-
_ = red_nosed_reports.number_of_safe_reports(puzzle_input) catch {};
8+
fn task_1(allocator: std.mem.Allocator) void {
9+
_ = red_nosed_reports.number_of_safe_reports(puzzle_input, allocator) catch {};
1010
}
1111

1212
// Benchmark of part 2
13-
fn task_2(_: std.mem.Allocator) void {
14-
_ = red_nosed_reports.number_of_partially_safe_reports(puzzle_input) catch {};
13+
fn task_2(allocator: std.mem.Allocator) void {
14+
_ = red_nosed_reports.number_of_partially_safe_reports(puzzle_input, allocator) catch {};
1515
}
1616

1717
pub fn main() !void {

2024/02/red_nosed_reports/src/main.zig

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,17 @@ pub fn main() !void {
5050
return;
5151
}
5252

53-
const safe_reports = red_nosed_reports.number_of_safe_reports(file_content);
53+
const safe_reports = red_nosed_reports.number_of_safe_reports(
54+
file_content,
55+
allocator,
56+
);
5457
try stdout.print("Number of safe reports: {!}\n", .{safe_reports});
5558
try bw.flush();
5659

57-
const partially_safe_reports = red_nosed_reports.number_of_partially_safe_reports(file_content);
60+
const partially_safe_reports = red_nosed_reports.number_of_partially_safe_reports(
61+
file_content,
62+
allocator,
63+
);
5864
try stdout.print("Number of partially safe reports: {!}\n", .{partially_safe_reports});
5965
try bw.flush();
6066
}

0 commit comments

Comments
 (0)