Skip to content

Commit 8064192

Browse files
committed
[2024] Added Day 22: Monkey Market
1 parent d602230 commit 8064192

File tree

12 files changed

+2207
-0
lines changed

12 files changed

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

2024/22/monkey_market/build.zig

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 monkey_market = b.addModule("monkey_market", .{
9+
.root_source_file = b.path("src/monkey_market.zig"),
10+
});
11+
12+
// -------------------------- Main executable --------------------------- \\
13+
const monkey_market_exe = b.addExecutable(.{
14+
.name = "monkey_market",
15+
.root_source_file = b.path("src/main.zig"),
16+
.target = target,
17+
.optimize = optimize,
18+
});
19+
20+
const yazap = b.dependency("yazap", .{});
21+
monkey_market_exe.root_module.addImport("yazap", yazap.module("yazap"));
22+
monkey_market_exe.root_module.addImport("monkey_market", monkey_market);
23+
b.installArtifact(monkey_market_exe);
24+
25+
const run_cmd = b.addRunArtifact(monkey_market_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 Monkey Market (day 22) app");
32+
run_step.dependOn(&run_cmd.step);
33+
34+
// --------------------------- Example tests ---------------------------- \\
35+
const monkey_market_tests = b.addTest(.{
36+
.name = "monkey_market_tests",
37+
.root_source_file = b.path("tests/example_tests.zig"),
38+
.target = target,
39+
.optimize = optimize,
40+
});
41+
42+
monkey_market_tests.root_module.addImport("monkey_market", monkey_market);
43+
inline for (1..4) |i| {
44+
const num_str = std.fmt.comptimePrint("{!}", .{i});
45+
monkey_market_tests.root_module.addAnonymousImport(
46+
"example_input_" ++ num_str,
47+
.{
48+
.root_source_file = b.path("input/example_input_" ++ num_str ++ ".txt"),
49+
},
50+
);
51+
}
52+
b.installArtifact(monkey_market_tests);
53+
54+
const test_step = b.step("test", "Run Monkey Market (day 22) tests");
55+
test_step.dependOn(&b.addRunArtifact(monkey_market_tests).step);
56+
57+
// ------------------------- Puzzle benchmarks -------------------------- \\
58+
const monkey_market_benchmarks = b.addExecutable(.{
59+
.name = "monkey_market_benchmarks",
60+
.root_source_file = b.path("benchmarks/puzzle_benchmarks.zig"),
61+
.target = target,
62+
.optimize = optimize,
63+
});
64+
65+
const zbench = b.dependency("zbench", .{
66+
.target = target,
67+
.optimize = optimize,
68+
});
69+
monkey_market_benchmarks.root_module.addImport("zbench", zbench.module("zbench"));
70+
monkey_market_benchmarks.root_module.addImport("monkey_market", monkey_market);
71+
monkey_market_benchmarks.root_module.addAnonymousImport("puzzle_input", .{
72+
.root_source_file = b.path("input/puzzle_input.txt"),
73+
});
74+
b.installArtifact(monkey_market_benchmarks);
75+
76+
const benchmark_step = b.step("benchmark", "Run Monkey Market (day 22) benchmarks");
77+
benchmark_step.dependOn(&b.addRunArtifact(monkey_market_benchmarks).step);
78+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.{
2+
.name = "monkey_market",
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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
123
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1
2+
10
3+
100
4+
2024
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1
2+
2
3+
3
4+
2024

0 commit comments

Comments
 (0)