Skip to content

Commit 5e3cf44

Browse files
committed
[2024] Add Day 12: Garden Groups
1 parent c93641e commit 5e3cf44

File tree

14 files changed

+755
-0
lines changed

14 files changed

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

2024/12/garden_groups/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 garden_groups = b.addModule("garden_groups", .{
9+
.root_source_file = b.path("src/garden_groups.zig"),
10+
});
11+
12+
// -------------------------- Main executable --------------------------- \\
13+
const garden_groups_exe = b.addExecutable(.{
14+
.name = "garden_groups",
15+
.root_source_file = b.path("src/main.zig"),
16+
.target = target,
17+
.optimize = optimize,
18+
});
19+
20+
const yazap = b.dependency("yazap", .{});
21+
garden_groups_exe.root_module.addImport("yazap", yazap.module("yazap"));
22+
garden_groups_exe.root_module.addImport("garden_groups", garden_groups);
23+
b.installArtifact(garden_groups_exe);
24+
25+
const run_cmd = b.addRunArtifact(garden_groups_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 the garden_groups (day 12) app");
32+
run_step.dependOn(&run_cmd.step);
33+
34+
// --------------------------- Example tests ---------------------------- \\
35+
const garden_groups_tests = b.addTest(.{
36+
.name = "garden_groups_tests",
37+
.root_source_file = b.path("tests/example_tests.zig"),
38+
.target = target,
39+
.optimize = optimize,
40+
});
41+
42+
garden_groups_tests.root_module.addImport("garden_groups", garden_groups);
43+
inline for (1..6) |i| {
44+
const num_str = std.fmt.comptimePrint("{!}", .{i});
45+
garden_groups_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(garden_groups_tests);
53+
54+
const test_step = b.step("test", "Run garden_groups (day 12) tests");
55+
test_step.dependOn(&b.addRunArtifact(garden_groups_tests).step);
56+
57+
// ------------------------- Puzzle benchmarks -------------------------- \\
58+
const garden_groups_benchmarks = b.addExecutable(.{
59+
.name = "garden_groups_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+
garden_groups_benchmarks.root_module.addImport("zbench", zbench.module("zbench"));
70+
garden_groups_benchmarks.root_module.addImport("garden_groups", garden_groups);
71+
garden_groups_benchmarks.root_module.addAnonymousImport("puzzle_input", .{
72+
.root_source_file = b.path("input/puzzle_input.txt"),
73+
});
74+
b.installArtifact(garden_groups_benchmarks);
75+
76+
const benchmark_step = b.step("benchmark", "Run garden_groups (day 12) benchmarks");
77+
benchmark_step.dependOn(&b.addRunArtifact(garden_groups_benchmarks).step);
78+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.{
2+
.name = "garden_groups",
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
AAAA
2+
BBCD
3+
BBCC
4+
EEEC
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
OOOOO
2+
OXOXO
3+
OOOOO
4+
OXOXO
5+
OOOOO
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
RRRRIICCFF
2+
RRRRIICCCF
3+
VVRRRCCFFF
4+
VVRCCCJFFF
5+
VVVVCJJCFE
6+
VVIVCCJJEE
7+
VVIIICJJEE
8+
MIIIIIJJEE
9+
MIIISIJEEE
10+
MMMISSJEEE
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
EEEEE
2+
EXXXX
3+
EEEEE
4+
EXXXX
5+
EEEEE
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
AAAAAA
2+
AAABBA
3+
AAABBA
4+
ABBAAA
5+
ABBAAA
6+
AAAAAA

0 commit comments

Comments
 (0)