Skip to content

Commit c8dba9f

Browse files
committed
[2024] Add Day 04: Ceres Search
1 parent b0f301d commit c8dba9f

File tree

10 files changed

+548
-0
lines changed

10 files changed

+548
-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 ceres_search = @import("ceres_search");
4+
5+
const puzzle_input = @embedFile("puzzle_input");
6+
7+
// Benchmark of part 1
8+
fn task_1(_: std.mem.Allocator) void {
9+
_ = ceres_search.xmas_count(puzzle_input) catch {};
10+
}
11+
12+
// Benchmark of part 2
13+
fn task_2(_: std.mem.Allocator) void {
14+
_ = ceres_search.x_mas_count(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 04 - Task 1", task_1, .{});
23+
try bench.add("Day 04 - Task 2", task_2, .{});
24+
25+
try stdout.writeAll("\n");
26+
try bench.run(stdout);
27+
}

2024/04/ceres_search/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 ceres_search = b.addModule("ceres_search", .{
9+
.root_source_file = b.path("src/ceres_search.zig"),
10+
});
11+
12+
// -------------------------- Main executable --------------------------- \\
13+
const ceres_search_exe = b.addExecutable(.{
14+
.name = "ceres_search",
15+
.root_source_file = b.path("src/main.zig"),
16+
.target = target,
17+
.optimize = optimize,
18+
});
19+
20+
const yazap = b.dependency("yazap", .{});
21+
ceres_search_exe.root_module.addImport("yazap", yazap.module("yazap"));
22+
ceres_search_exe.root_module.addImport("ceres_search", ceres_search);
23+
b.installArtifact(ceres_search_exe);
24+
25+
const run_cmd = b.addRunArtifact(ceres_search_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 Ceres Search (day 04) app");
32+
run_step.dependOn(&run_cmd.step);
33+
34+
// --------------------------- Example tests ---------------------------- \\
35+
const ceres_search_tests = b.addTest(.{
36+
.name = "ceres_search_tests",
37+
.root_source_file = b.path("tests/example_tests.zig"),
38+
.target = target,
39+
.optimize = optimize,
40+
});
41+
42+
ceres_search_tests.root_module.addImport("ceres_search", ceres_search);
43+
ceres_search_tests.root_module.addAnonymousImport("example_input", .{
44+
.root_source_file = b.path("input/example_input.txt"),
45+
});
46+
b.installArtifact(ceres_search_tests);
47+
48+
const test_step = b.step("test", "Run Ceres Search (day 04) tests");
49+
test_step.dependOn(&b.addRunArtifact(ceres_search_tests).step);
50+
51+
// ------------------------- Puzzle benchmarks -------------------------- \\
52+
const ceres_search_benchmarks = b.addExecutable(.{
53+
.name = "ceres_search_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+
ceres_search_benchmarks.root_module.addImport("zbench", zbench.module("zbench"));
64+
ceres_search_benchmarks.root_module.addImport("ceres_search", ceres_search);
65+
ceres_search_benchmarks.root_module.addAnonymousImport("puzzle_input", .{
66+
.root_source_file = b.path("input/puzzle_input.txt"),
67+
});
68+
b.installArtifact(ceres_search_benchmarks);
69+
70+
const benchmark_step = b.step("benchmark", "Run Ceres Search (day 04) benchmarks");
71+
benchmark_step.dependOn(&b.addRunArtifact(ceres_search_benchmarks).step);
72+
}

2024/04/ceres_search/build.zig.zon

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.{
2+
.name = "ceres_search",
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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
MMMSXXMASM
2+
MSAMXMSMSA
3+
AMXSXMAAMM
4+
MSAMASMSMX
5+
XMASAMXAMM
6+
XXAMMXXAMA
7+
SMSMSASXSS
8+
SAXAMASAAA
9+
MAMMMXMMMM
10+
MXMXAXMASX

0 commit comments

Comments
 (0)