Skip to content

Commit a9aa0e5

Browse files
committed
[2024] Added Day 08: Resonant Collinearity
1 parent 597fdac commit a9aa0e5

File tree

14 files changed

+565
-0
lines changed

14 files changed

+565
-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 resonant_collinearity = @import("resonant_collinearity");
4+
5+
const puzzle_input = @embedFile("puzzle_input");
6+
7+
// Benchmark of part 1
8+
fn task_1(allocator: std.mem.Allocator) void {
9+
_ = resonant_collinearity.number_of_outer_antinodes(puzzle_input, allocator) catch {};
10+
}
11+
12+
// Benchmark of part 2
13+
fn task_2(allocator: std.mem.Allocator) void {
14+
_ = resonant_collinearity.number_of_all_antinodes(puzzle_input, 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 08 - Task 1", task_1, .{});
23+
try bench.add("Day 08 - Task 2", task_2, .{});
24+
25+
try stdout.writeAll("\n");
26+
try bench.run(stdout);
27+
}
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 resonant_collinearity = b.addModule("resonant_collinearity", .{
9+
.root_source_file = b.path("src/resonant_collinearity.zig"),
10+
});
11+
12+
// -------------------------- Main executable --------------------------- \\
13+
const resonant_collinearity_exe = b.addExecutable(.{
14+
.name = "resonant_collinearity",
15+
.root_source_file = b.path("src/main.zig"),
16+
.target = target,
17+
.optimize = optimize,
18+
});
19+
20+
const yazap = b.dependency("yazap", .{});
21+
resonant_collinearity_exe.root_module.addImport("yazap", yazap.module("yazap"));
22+
resonant_collinearity_exe.root_module.addImport("resonant_collinearity", resonant_collinearity);
23+
b.installArtifact(resonant_collinearity_exe);
24+
25+
const run_cmd = b.addRunArtifact(resonant_collinearity_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 Resonant Collinearity (day 08) app");
32+
run_step.dependOn(&run_cmd.step);
33+
34+
// --------------------------- Example tests ---------------------------- \\
35+
const resonant_collinearity_tests = b.addTest(.{
36+
.name = "resonant_collinearity_tests",
37+
.root_source_file = b.path("tests/example_tests.zig"),
38+
.target = target,
39+
.optimize = optimize,
40+
});
41+
42+
resonant_collinearity_tests.root_module.addImport("resonant_collinearity", resonant_collinearity);
43+
inline for (1..6) |i| {
44+
const num_str = std.fmt.comptimePrint("{!}", .{i});
45+
resonant_collinearity_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(resonant_collinearity_tests);
53+
54+
const test_step = b.step("test", "Run Resonant Collinearity (day 08) tests");
55+
test_step.dependOn(&b.addRunArtifact(resonant_collinearity_tests).step);
56+
57+
// ------------------------- Puzzle benchmarks -------------------------- \\
58+
const resonant_collinearity_benchmarks = b.addExecutable(.{
59+
.name = "resonant_collinearity_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+
resonant_collinearity_benchmarks.root_module.addImport("zbench", zbench.module("zbench"));
70+
resonant_collinearity_benchmarks.root_module.addImport("resonant_collinearity", resonant_collinearity);
71+
resonant_collinearity_benchmarks.root_module.addAnonymousImport("puzzle_input", .{
72+
.root_source_file = b.path("input/puzzle_input.txt"),
73+
});
74+
b.installArtifact(resonant_collinearity_benchmarks);
75+
76+
const benchmark_step = b.step("benchmark", "Run Resonant Collinearity (day 08) benchmarks");
77+
benchmark_step.dependOn(&b.addRunArtifact(resonant_collinearity_benchmarks).step);
78+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.{
2+
.name = "resonant_collinearity",
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+
..........
2+
..........
3+
..........
4+
....a.....
5+
..........
6+
.....a....
7+
..........
8+
..........
9+
..........
10+
..........
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
..........
2+
..........
3+
..........
4+
....a.....
5+
........a.
6+
.....a....
7+
..........
8+
..........
9+
..........
10+
..........
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
..........
2+
..........
3+
..........
4+
....a.....
5+
........a.
6+
.....a....
7+
..........
8+
......A...
9+
..........
10+
..........
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
............
2+
........0...
3+
.....0......
4+
.......0....
5+
....0.......
6+
......A.....
7+
............
8+
............
9+
........A...
10+
.........A..
11+
............
12+
............
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
T.........
2+
...T......
3+
.T........
4+
..........
5+
..........
6+
..........
7+
..........
8+
..........
9+
..........
10+
..........
2.51 KB
Binary file not shown.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
const std = @import("std");
2+
const yazap = @import("yazap");
3+
const resonant_collinearity = @import("resonant_collinearity");
4+
5+
const allocator = std.heap.page_allocator;
6+
const log = std.log;
7+
const App = yazap.App;
8+
const Arg = yazap.Arg;
9+
const string = []const u8;
10+
11+
pub fn main() !void {
12+
var app = App.init(
13+
allocator,
14+
"resonant_collinearity",
15+
"Day 08: Resonant Collinearity",
16+
);
17+
defer app.deinit();
18+
19+
var cmd = app.rootCommand();
20+
cmd.setProperty(.help_on_empty_args);
21+
try cmd.addArg(Arg.singleValueOption(
22+
"filename",
23+
'f',
24+
"Input file (e.g. input/puzzle_input.txt)",
25+
));
26+
27+
const matches = try app.parseProcess();
28+
29+
const stdout_file = std.io.getStdOut().writer();
30+
var bw = std.io.bufferedWriter(stdout_file);
31+
const stdout = bw.writer();
32+
33+
var file_content: string = undefined;
34+
if (matches.getSingleValue("filename")) |filename| {
35+
const file = try std.fs.cwd().openFile(filename, .{});
36+
defer file.close();
37+
38+
const file_size = try file.getEndPos();
39+
const buffer: []u8 = try allocator.alloc(u8, file_size);
40+
defer allocator.free(buffer);
41+
42+
_ = try file.readAll(buffer);
43+
file_content = std.mem.Allocator.dupe(
44+
allocator,
45+
u8,
46+
std.mem.trim(u8, buffer, "\n"),
47+
) catch unreachable;
48+
} else {
49+
try app.displayHelp();
50+
return;
51+
}
52+
53+
const result_1 = resonant_collinearity.number_of_outer_antinodes(
54+
file_content,
55+
allocator,
56+
);
57+
try stdout.print("Number of outer antinodes: {!}\n", .{result_1});
58+
try bw.flush();
59+
60+
const result_2 = resonant_collinearity.number_of_all_antinodes(
61+
file_content,
62+
allocator,
63+
);
64+
try stdout.print("Number of all antinodes: {!}\n", .{result_2});
65+
try bw.flush();
66+
}

0 commit comments

Comments
 (0)