Skip to content

Commit 5f4c018

Browse files
committed
Improve root build system; added string typedefs
1 parent 546b5bd commit 5f4c018

File tree

11 files changed

+80
-53
lines changed

11 files changed

+80
-53
lines changed

2024/00/zig_template/build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.{
22
.name = "zig_template",
33
.version = "0.1.0",
4-
.minimum_zig_version = "0.11.0",
4+
.minimum_zig_version = "0.13.0",
55
.dependencies = .{
66
.yazap = .{
77
.url = "git+https://github.com/prajwalch/yazap#c2e3122d5dd6192513ba590f229dbc535110efb8",

2024/00/zig_template/src/main.zig

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@ const allocator = std.heap.page_allocator;
66
const log = std.log;
77
const App = yazap.App;
88
const Arg = yazap.Arg;
9+
const string = []const u8;
910

1011
pub fn main() !void {
1112
var app = App.init(allocator, "Day 00", "Day 00: Zig Template");
1213
defer app.deinit();
1314

1415
var cmd = app.rootCommand();
1516
cmd.setProperty(.help_on_empty_args);
16-
try cmd.addArg(Arg.singleValueOption("filename", 'f', "Input file (e.g. input/puzzle_input.txt)"));
17+
try cmd.addArg(Arg.singleValueOption(
18+
"filename",
19+
'f',
20+
"Input file (e.g. input/puzzle_input.txt)",
21+
));
1722

1823
const matches = try app.parseProcess();
1924

2025
const stdout_file = std.io.getStdOut().writer();
2126
var bw = std.io.bufferedWriter(stdout_file);
2227
const stdout = bw.writer();
2328

24-
var file_content: []u8 = &[_]u8{};
25-
29+
var file_content: string = undefined;
2630
if (matches.getSingleValue("filename")) |filename| {
2731
const file = try std.fs.cwd().openFile(filename, .{});
2832
defer file.close();

2024/00/zig_template/src/zig_template.zig

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
const std = @import("std");
2+
const string = []const u8;
23

34
/// Task 1
4-
pub fn solution_1(contents: []const u8) !i32 {
5+
pub fn solution_1(contents: string) !i32 {
56
const lines = std.mem.split(u8, contents, "\n");
67
_ = lines;
78

89
return 0;
910
}
1011

1112
/// Task 2
12-
pub fn solution_2(contents: []const u8) !i32 {
13+
pub fn solution_2(contents: string) !i32 {
1314
const lines = std.mem.split(u8, contents, "\n");
1415
_ = lines;
1516

2024/02/red_nosed_reports/build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.{
22
.name = "red_nosed_reports",
33
.version = "0.1.0",
4-
.minimum_zig_version = "0.11.0",
4+
.minimum_zig_version = "0.13.0",
55
.dependencies = .{
66
.yazap = .{
77
.url = "git+https://github.com/prajwalch/yazap#c2e3122d5dd6192513ba590f229dbc535110efb8",

2024/02/red_nosed_reports/src/main.zig

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@ const allocator = std.heap.page_allocator;
66
const log = std.log;
77
const App = yazap.App;
88
const Arg = yazap.Arg;
9+
const string = []const u8;
910

1011
pub fn main() !void {
1112
var app = App.init(allocator, "Day 02", "Day 02: red_nosed Reports");
1213
defer app.deinit();
1314

1415
var cmd = app.rootCommand();
1516
cmd.setProperty(.help_on_empty_args);
16-
try cmd.addArg(Arg.singleValueOption("filename", 'f', "Input file (e.g. input/puzzle_input.txt)"));
17+
try cmd.addArg(Arg.singleValueOption(
18+
"filename",
19+
'f',
20+
"Input file (e.g. input/puzzle_input.txt)",
21+
));
1722

1823
const matches = try app.parseProcess();
1924

2025
const stdout_file = std.io.getStdOut().writer();
2126
var bw = std.io.bufferedWriter(stdout_file);
2227
const stdout = bw.writer();
2328

24-
var file_content: []u8 = &[_]u8{};
25-
29+
var file_content: string = undefined;
2630
if (matches.getSingleValue("filename")) |filename| {
2731
const file = try std.fs.cwd().openFile(filename, .{});
2832
defer file.close();

2024/02/red_nosed_reports/src/red_nosed_reports.zig

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

56
const Report = struct {
67
levels: ArrayList(i32),
@@ -13,7 +14,7 @@ const Report = struct {
1314
///
1415
/// Returns:
1516
/// - Number of fully safe reports.
16-
pub fn number_of_safe_reports(contents: []const u8) !i32 {
17+
pub fn number_of_safe_reports(contents: string) !i32 {
1718
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
1819
defer arena.deinit();
1920

@@ -38,7 +39,7 @@ pub fn number_of_safe_reports(contents: []const u8) !i32 {
3839
///
3940
/// Returns:
4041
/// - Number of fully safe reports.
41-
pub fn number_of_partially_safe_reports(contents: []const u8) !i32 {
42+
pub fn number_of_partially_safe_reports(contents: string) !i32 {
4243
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
4344
defer arena.deinit();
4445

@@ -67,7 +68,7 @@ pub fn number_of_partially_safe_reports(contents: []const u8) !i32 {
6768
///
6869
/// Returns:
6970
/// - Array list of report objects.
70-
fn parse_reports(contents: []const u8, allocator: Allocator) !ArrayList(Report) {
71+
fn parse_reports(contents: string, allocator: Allocator) !ArrayList(Report) {
7172
var reports = ArrayList(Report).init(allocator);
7273

7374
var lines = std.mem.split(u8, contents, "\n");

2024/06/guard_gallivant/build.zig.zon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
.{
22
.name = "guard_gallivant",
33
.version = "0.1.0",
4-
.minimum_zig_version = "0.11.0",
4+
.minimum_zig_version = "0.13.0",
55
.dependencies = .{
66
.yazap = .{
77
.url = "git+https://github.com/prajwalch/yazap#c2e3122d5dd6192513ba590f229dbc535110efb8",

2024/06/guard_gallivant/src/guard_gallivant.zig

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
const std = @import("std");
22
const HashMap = std.AutoArrayHashMap;
33
const Allocator = std.mem.Allocator;
4+
const string = []const u8;
45

56
/// Task 1
6-
pub fn solution_1(contents: []const u8) !usize {
7+
pub fn solution_1(contents: string) !usize {
78
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
89
defer arena.deinit();
910

1011
const allocator = arena.allocator();
11-
const setup = try parse_map(contents, allocator);
12-
const map = setup[0];
13-
var guard = setup[1];
12+
const map, var guard = try parse_map(contents, allocator);
1413

14+
// Move the guard until it is outside the map and record all positions
1515
var visited = HashMap(Position, void).init(allocator);
1616
while (is_guard_inside(map, guard)) : (guard = move(map, guard)) {
1717
try visited.put(guard.pos, {});
@@ -21,36 +21,35 @@ pub fn solution_1(contents: []const u8) !usize {
2121
}
2222

2323
/// Task 2
24-
pub fn solution_2(contents: []const u8) !usize {
24+
pub fn solution_2(contents: string) !usize {
2525
var arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
2626
defer arena.deinit();
2727

2828
const allocator = arena.allocator();
29-
const setup = try parse_map(contents, allocator);
30-
var map = setup[0];
31-
var guard = setup[1];
29+
var map, var guard = try parse_map(contents, allocator);
3230

31+
// Move the guard until it is outside the map and record all states
3332
var visited = HashMap(Guard, void).init(allocator);
3433
while (is_guard_inside(map, guard)) : (guard = move(map, guard)) {
3534
try visited.put(guard, {});
3635
}
3736

38-
var tried_obstacle = HashMap(Position, void).init(allocator);
39-
try tried_obstacle.put(visited.keys()[0].pos, {});
37+
var new_obstacles = HashMap(Position, void).init(allocator);
38+
try new_obstacles.put(visited.keys()[0].pos, {});
4039
var loop_count: usize = 0;
40+
41+
// Put an obstacle into each unique position in the guards path and check
42+
// wether the guard runs in a loop or exits the map
4143
for (0..visited.count()) |i| {
42-
if (tried_obstacle.contains(visited.keys()[i].pos)) {
44+
if (new_obstacles.contains(visited.keys()[i].pos)) {
4345
continue;
4446
}
45-
4647
try map.obstacles.put(visited.keys()[i].pos, {});
4748

49+
// Let the guard start right in front of the new obstacle to save time
4850
guard = visited.keys()[i - 1];
49-
var states = HashMap(Guard, void).init(
50-
std.heap.page_allocator,
51-
);
52-
defer states.deinit();
5351

52+
var states = HashMap(Guard, void).init(allocator);
5453
while (is_guard_inside(map, guard)) : (guard = move(map, guard)) {
5554
if (states.contains(guard)) {
5655
loop_count += 1;
@@ -60,7 +59,7 @@ pub fn solution_2(contents: []const u8) !usize {
6059
}
6160

6261
_ = map.obstacles.pop();
63-
try tried_obstacle.put(visited.keys()[i].pos, {});
62+
try new_obstacles.put(visited.keys()[i].pos, {});
6463
}
6564

6665
return loop_count;
@@ -131,7 +130,7 @@ pub fn is_guard_inside(map: GuardMap, guard: Guard) bool {
131130
///
132131
/// Returns:
133132
/// - Array list of report objects.
134-
fn parse_map(contents: []const u8, allocator: Allocator) !struct { GuardMap, Guard } {
133+
fn parse_map(contents: string, allocator: Allocator) !struct { GuardMap, Guard } {
135134
var obstacles = HashMap(Position, void).init(allocator);
136135
var dir: ?Direction = null;
137136
var pos: ?Position = null;

2024/06/guard_gallivant/src/main.zig

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,27 @@ const allocator = std.heap.page_allocator;
66
const log = std.log;
77
const App = yazap.App;
88
const Arg = yazap.Arg;
9+
const string = []const u8;
910

1011
pub fn main() !void {
1112
var app = App.init(allocator, "Day 06", "Day 06: Guard Gallivant");
1213
defer app.deinit();
1314

1415
var cmd = app.rootCommand();
1516
cmd.setProperty(.help_on_empty_args);
16-
try cmd.addArg(Arg.singleValueOption("filename", 'f', "Input file (e.g. input/puzzle_input.txt)"));
17+
try cmd.addArg(Arg.singleValueOption(
18+
"filename",
19+
'f',
20+
"Input file (e.g. input/puzzle_input.txt)",
21+
));
1722

1823
const matches = try app.parseProcess();
1924

2025
const stdout_file = std.io.getStdOut().writer();
2126
var bw = std.io.bufferedWriter(stdout_file);
2227
const stdout = bw.writer();
2328

24-
var file_content: []u8 = &[_]u8{};
25-
29+
var file_content: string = undefined;
2630
if (matches.getSingleValue("filename")) |filename| {
2731
const file = try std.fs.cwd().openFile(filename, .{});
2832
defer file.close();

2024/build.zig

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
11
const std = @import("std");
2+
const string = []const u8;
3+
const ResolvedTarget = std.Build.ResolvedTarget;
4+
const OptimizeMode = std.builtin.OptimizeMode;
5+
const Step = std.Build.Step;
26

3-
pub fn build(b: *std.Build) void {
4-
const target = b.standardTargetOptions(.{});
5-
const optimize = b.standardOptimizeOption(.{});
6-
7-
// ---------------------------- Subprojects ----------------------------- \\
8-
const day_00 = b.dependency("day_00_zig_template", .{
7+
/// Add a dependency for each day
8+
fn add_subproject(
9+
b: *std.Build,
10+
target: ResolvedTarget,
11+
optimize: OptimizeMode,
12+
test_step: *Step,
13+
benchmark_step: *Step,
14+
comptime day: string,
15+
comptime name: string,
16+
) void {
17+
const subproject = b.dependency("day_" ++ day ++ "_" ++ name, .{
918
.target = target,
1019
.optimize = optimize,
1120
});
1221

13-
const day_02 = b.dependency("day_02_red_nosed_reports", .{
14-
.target = target,
15-
.optimize = optimize,
16-
});
22+
test_step.dependOn(&b.addRunArtifact(subproject.artifact(name ++ "_tests")).step);
23+
benchmark_step.dependOn(&b.addRunArtifact(subproject.artifact(name ++ "_benchmarks")).step);
24+
}
1725

18-
// --------------------------- Example tests ---------------------------- \\
19-
const test_step = b.step("test", "Run example tests");
20-
test_step.dependOn(&b.addRunArtifact(day_00.artifact("zig_template_tests")).step);
21-
test_step.dependOn(&b.addRunArtifact(day_02.artifact("red_nosed_reports_tests")).step);
26+
pub fn build(b: *std.Build) void {
27+
const target = b.standardTargetOptions(.{});
28+
const optimize = b.standardOptimizeOption(.{});
2229

23-
// ------------------------- Puzzle benchmarks -------------------------- \\
30+
const test_step = b.step("test", "Run example tests");
2431
const benchmark_step = b.step("benchmark", "Run puzzle benchmarks");
25-
benchmark_step.dependOn(&b.addRunArtifact(day_00.artifact("zig_template_benchmarks")).step);
26-
benchmark_step.dependOn(&b.addRunArtifact(day_02.artifact("red_nosed_reports_benchmarks")).step);
32+
33+
// ---------------------------- Subprojects ----------------------------- \\
34+
add_subproject(b, target, optimize, test_step, benchmark_step, "00", "zig_template");
35+
add_subproject(b, target, optimize, test_step, benchmark_step, "02", "red_nosed_reports");
36+
add_subproject(b, target, optimize, test_step, benchmark_step, "06", "guard_gallivant");
2737
}

0 commit comments

Comments
 (0)