Skip to content

Commit b0f301d

Browse files
committed
[2024] Add Day 13: Claw Contraption
1 parent 2f1e01f commit b0f301d

File tree

10 files changed

+1612
-0
lines changed

10 files changed

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

2024/13/claw_contraption/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 claw_contraption = b.addModule("claw_contraption", .{
9+
.root_source_file = b.path("src/claw_contraption.zig"),
10+
});
11+
12+
// -------------------------- Main executable --------------------------- \\
13+
const claw_contraption_exe = b.addExecutable(.{
14+
.name = "claw_contraption",
15+
.root_source_file = b.path("src/main.zig"),
16+
.target = target,
17+
.optimize = optimize,
18+
});
19+
20+
const yazap = b.dependency("yazap", .{});
21+
claw_contraption_exe.root_module.addImport("yazap", yazap.module("yazap"));
22+
claw_contraption_exe.root_module.addImport("claw_contraption", claw_contraption);
23+
b.installArtifact(claw_contraption_exe);
24+
25+
const run_cmd = b.addRunArtifact(claw_contraption_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 Claw Contraption (day 13) app");
32+
run_step.dependOn(&run_cmd.step);
33+
34+
// --------------------------- Example tests ---------------------------- \\
35+
const claw_contraption_tests = b.addTest(.{
36+
.name = "claw_contraption_tests",
37+
.root_source_file = b.path("tests/example_tests.zig"),
38+
.target = target,
39+
.optimize = optimize,
40+
});
41+
42+
claw_contraption_tests.root_module.addImport("claw_contraption", claw_contraption);
43+
claw_contraption_tests.root_module.addAnonymousImport("example_input", .{
44+
.root_source_file = b.path("input/example_input.txt"),
45+
});
46+
b.installArtifact(claw_contraption_tests);
47+
48+
const test_step = b.step("test", "Run Claw Contraption (day 13) tests");
49+
test_step.dependOn(&b.addRunArtifact(claw_contraption_tests).step);
50+
51+
// ------------------------- Puzzle benchmarks -------------------------- \\
52+
const claw_contraption_benchmarks = b.addExecutable(.{
53+
.name = "claw_contraption_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+
claw_contraption_benchmarks.root_module.addImport("zbench", zbench.module("zbench"));
64+
claw_contraption_benchmarks.root_module.addImport("claw_contraption", claw_contraption);
65+
claw_contraption_benchmarks.root_module.addAnonymousImport("puzzle_input", .{
66+
.root_source_file = b.path("input/puzzle_input.txt"),
67+
});
68+
b.installArtifact(claw_contraption_benchmarks);
69+
70+
const benchmark_step = b.step("benchmark", "Run Claw Contraption (day 13) benchmarks");
71+
benchmark_step.dependOn(&b.addRunArtifact(claw_contraption_benchmarks).step);
72+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.{
2+
.name = "claw_contraption",
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Button A: X+94, Y+34
2+
Button B: X+22, Y+67
3+
Prize: X=8400, Y=5400
4+
5+
Button A: X+26, Y+66
6+
Button B: X+67, Y+21
7+
Prize: X=12748, Y=12176
8+
9+
Button A: X+17, Y+86
10+
Button B: X+84, Y+37
11+
Prize: X=7870, Y=6450
12+
13+
Button A: X+69, Y+23
14+
Button B: X+27, Y+71
15+
Prize: X=18641, Y=10279

0 commit comments

Comments
 (0)