Skip to content

Commit 46466ff

Browse files
committed
[2024] Add Day 05: Print Queue
1 parent c8dba9f commit 46466ff

File tree

10 files changed

+1788
-0
lines changed

10 files changed

+1788
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const std = @import("std");
2+
const zbench = @import("zbench");
3+
const print_queue = @import("print_queue");
4+
5+
const puzzle_input = @embedFile("puzzle_input");
6+
7+
// Benchmark of part 1
8+
fn task_1(_: std.mem.Allocator) void {
9+
_ = print_queue.middle_page_sum_of_valid_updates(puzzle_input) catch {};
10+
}
11+
12+
// Benchmark of part 2
13+
fn task_2(_: std.mem.Allocator) void {
14+
_ = print_queue.middle_page_sum_of_fixed_updates(puzzle_input) catch {};
15+
}
16+
17+
pub fn main() !void {
18+
const stdout = std.io.getStdOut().writer();
19+
var bench = zbench.Benchmark.init(std.heap.page_allocator, .{});
20+
defer bench.deinit();
21+
22+
try bench.add("Day 05 - Task 1", task_1, .{});
23+
try bench.add("Day 05 - Task 2", task_2, .{});
24+
25+
try stdout.writeAll("\n");
26+
try bench.run(stdout);
27+
}

2024/05/print_queue/build.zig

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
const std = @import("std");
2+
3+
pub fn build(b: *std.Build) void {
4+
const target = b.standardTargetOptions(.{});
5+
const optimize = b.standardOptimizeOption(.{});
6+
7+
// -------------------------- Solution module --------------------------- \\
8+
const print_queue = b.addModule("print_queue", .{
9+
.root_source_file = b.path("src/print_queue.zig"),
10+
});
11+
12+
// -------------------------- Main executable --------------------------- \\
13+
const print_queue_exe = b.addExecutable(.{
14+
.name = "print_queue",
15+
.root_source_file = b.path("src/main.zig"),
16+
.target = target,
17+
.optimize = optimize,
18+
});
19+
20+
const yazap = b.dependency("yazap", .{});
21+
print_queue_exe.root_module.addImport("yazap", yazap.module("yazap"));
22+
print_queue_exe.root_module.addImport("print_queue", print_queue);
23+
b.installArtifact(print_queue_exe);
24+
25+
const run_cmd = b.addRunArtifact(print_queue_exe);
26+
run_cmd.step.dependOn(b.getInstallStep());
27+
if (b.args) |args| {
28+
run_cmd.addArgs(args);
29+
}
30+
31+
const run_step = b.step("run", "Run Print Queue (day 05) app");
32+
run_step.dependOn(&run_cmd.step);
33+
34+
// --------------------------- Example tests ---------------------------- \\
35+
const print_queue_tests = b.addTest(.{
36+
.name = "print_queue_tests",
37+
.root_source_file = b.path("tests/example_tests.zig"),
38+
.target = target,
39+
.optimize = optimize,
40+
});
41+
42+
print_queue_tests.root_module.addImport("print_queue", print_queue);
43+
print_queue_tests.root_module.addAnonymousImport("example_input", .{
44+
.root_source_file = b.path("input/example_input.txt"),
45+
});
46+
b.installArtifact(print_queue_tests);
47+
48+
const test_step = b.step("test", "Run Print Queue (day 05) tests");
49+
test_step.dependOn(&b.addRunArtifact(print_queue_tests).step);
50+
51+
// ------------------------- Puzzle benchmarks -------------------------- \\
52+
const print_queue_benchmarks = b.addExecutable(.{
53+
.name = "print_queue_benchmarks",
54+
.root_source_file = b.path("benchmarks/puzzle_benchmarks.zig"),
55+
.target = target,
56+
.optimize = optimize,
57+
});
58+
59+
const zbench = b.dependency("zbench", .{
60+
.target = target,
61+
.optimize = optimize,
62+
});
63+
print_queue_benchmarks.root_module.addImport("zbench", zbench.module("zbench"));
64+
print_queue_benchmarks.root_module.addImport("print_queue", print_queue);
65+
print_queue_benchmarks.root_module.addAnonymousImport("puzzle_input", .{
66+
.root_source_file = b.path("input/puzzle_input.txt"),
67+
});
68+
b.installArtifact(print_queue_benchmarks);
69+
70+
const benchmark_step = b.step("benchmark", "Run Print Queue (day 05) benchmarks");
71+
benchmark_step.dependOn(&b.addRunArtifact(print_queue_benchmarks).step);
72+
}

2024/05/print_queue/build.zig.zon

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.{
2+
.name = "print_queue",
3+
.version = "0.1.0",
4+
.minimum_zig_version = "0.13.0",
5+
.dependencies = .{
6+
.yazap = .{
7+
.url = "git+https://github.com/prajwalch/yazap#c2e3122d5dd6192513ba590f229dbc535110efb8",
8+
.hash = "122054439ec36ac10987c87ae69f3b041b40b2e451af3fe3ef1fc578b3bad756a800",
9+
},
10+
.zbench = .{
11+
.url = "git+https://github.com/hendriknielaender/zBench#fb3ecae5d035091fd2392a2ec21970c06fc375fa",
12+
.hash = "122095b73930ff5d627429295c669905d85bb9b54394ddc185ad2d61295cc65819e5",
13+
},
14+
},
15+
.paths = .{
16+
"build.zig",
17+
"build.zig.zon",
18+
"src",
19+
"input",
20+
"tests",
21+
"benchmarks",
22+
},
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
47|53
2+
97|13
3+
97|61
4+
97|47
5+
75|29
6+
61|13
7+
75|53
8+
29|13
9+
97|29
10+
53|29
11+
61|53
12+
97|53
13+
61|29
14+
47|13
15+
75|47
16+
97|75
17+
47|61
18+
75|61
19+
47|29
20+
75|13
21+
53|13
22+
23+
75,47,61,53,29
24+
97,61,53,29,13
25+
75,29,13
26+
75,97,47,61,53
27+
61,13,29
28+
97,13,75,29,47

0 commit comments

Comments
 (0)