Skip to content

Commit

Permalink
[2024] Added Day 08: Resonant Collinearity
Browse files Browse the repository at this point in the history
  • Loading branch information
ACSimon33 committed Dec 27, 2024
1 parent 597fdac commit a9aa0e5
Show file tree
Hide file tree
Showing 14 changed files with 565 additions and 0 deletions.
27 changes: 27 additions & 0 deletions 2024/08/resonant_collinearity/benchmarks/puzzle_benchmarks.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const std = @import("std");
const zbench = @import("zbench");
const resonant_collinearity = @import("resonant_collinearity");

const puzzle_input = @embedFile("puzzle_input");

// Benchmark of part 1
fn task_1(allocator: std.mem.Allocator) void {
_ = resonant_collinearity.number_of_outer_antinodes(puzzle_input, allocator) catch {};
}

// Benchmark of part 2
fn task_2(allocator: std.mem.Allocator) void {
_ = resonant_collinearity.number_of_all_antinodes(puzzle_input, allocator) catch {};
}

pub fn main() !void {
const stdout = std.io.getStdOut().writer();
var bench = zbench.Benchmark.init(std.heap.page_allocator, .{});
defer bench.deinit();

try bench.add("Day 08 - Task 1", task_1, .{});
try bench.add("Day 08 - Task 2", task_2, .{});

try stdout.writeAll("\n");
try bench.run(stdout);
}
78 changes: 78 additions & 0 deletions 2024/08/resonant_collinearity/build.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

// -------------------------- Solution module --------------------------- \\
const resonant_collinearity = b.addModule("resonant_collinearity", .{
.root_source_file = b.path("src/resonant_collinearity.zig"),
});

// -------------------------- Main executable --------------------------- \\
const resonant_collinearity_exe = b.addExecutable(.{
.name = "resonant_collinearity",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});

const yazap = b.dependency("yazap", .{});
resonant_collinearity_exe.root_module.addImport("yazap", yazap.module("yazap"));
resonant_collinearity_exe.root_module.addImport("resonant_collinearity", resonant_collinearity);
b.installArtifact(resonant_collinearity_exe);

const run_cmd = b.addRunArtifact(resonant_collinearity_exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}

const run_step = b.step("run", "Run Resonant Collinearity (day 08) app");
run_step.dependOn(&run_cmd.step);

// --------------------------- Example tests ---------------------------- \\
const resonant_collinearity_tests = b.addTest(.{
.name = "resonant_collinearity_tests",
.root_source_file = b.path("tests/example_tests.zig"),
.target = target,
.optimize = optimize,
});

resonant_collinearity_tests.root_module.addImport("resonant_collinearity", resonant_collinearity);
inline for (1..6) |i| {
const num_str = std.fmt.comptimePrint("{!}", .{i});
resonant_collinearity_tests.root_module.addAnonymousImport(
"example_input_" ++ num_str,
.{
.root_source_file = b.path("input/example_input_" ++ num_str ++ ".txt"),
},
);
}
b.installArtifact(resonant_collinearity_tests);

const test_step = b.step("test", "Run Resonant Collinearity (day 08) tests");
test_step.dependOn(&b.addRunArtifact(resonant_collinearity_tests).step);

// ------------------------- Puzzle benchmarks -------------------------- \\
const resonant_collinearity_benchmarks = b.addExecutable(.{
.name = "resonant_collinearity_benchmarks",
.root_source_file = b.path("benchmarks/puzzle_benchmarks.zig"),
.target = target,
.optimize = optimize,
});

const zbench = b.dependency("zbench", .{
.target = target,
.optimize = optimize,
});
resonant_collinearity_benchmarks.root_module.addImport("zbench", zbench.module("zbench"));
resonant_collinearity_benchmarks.root_module.addImport("resonant_collinearity", resonant_collinearity);
resonant_collinearity_benchmarks.root_module.addAnonymousImport("puzzle_input", .{
.root_source_file = b.path("input/puzzle_input.txt"),
});
b.installArtifact(resonant_collinearity_benchmarks);

const benchmark_step = b.step("benchmark", "Run Resonant Collinearity (day 08) benchmarks");
benchmark_step.dependOn(&b.addRunArtifact(resonant_collinearity_benchmarks).step);
}
23 changes: 23 additions & 0 deletions 2024/08/resonant_collinearity/build.zig.zon
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.{
.name = "resonant_collinearity",
.version = "0.1.0",
.minimum_zig_version = "0.13.0",
.dependencies = .{
.yazap = .{
.url = "git+https://github.com/prajwalch/yazap#c2e3122d5dd6192513ba590f229dbc535110efb8",
.hash = "122054439ec36ac10987c87ae69f3b041b40b2e451af3fe3ef1fc578b3bad756a800",
},
.zbench = .{
.url = "git+https://github.com/hendriknielaender/zBench#fb3ecae5d035091fd2392a2ec21970c06fc375fa",
.hash = "122095b73930ff5d627429295c669905d85bb9b54394ddc185ad2d61295cc65819e5",
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
"input",
"tests",
"benchmarks",
},
}
10 changes: 10 additions & 0 deletions 2024/08/resonant_collinearity/input/example_input_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
..........
..........
..........
....a.....
..........
.....a....
..........
..........
..........
..........
10 changes: 10 additions & 0 deletions 2024/08/resonant_collinearity/input/example_input_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
..........
..........
..........
....a.....
........a.
.....a....
..........
..........
..........
..........
10 changes: 10 additions & 0 deletions 2024/08/resonant_collinearity/input/example_input_3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
..........
..........
..........
....a.....
........a.
.....a....
..........
......A...
..........
..........
12 changes: 12 additions & 0 deletions 2024/08/resonant_collinearity/input/example_input_4.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............
10 changes: 10 additions & 0 deletions 2024/08/resonant_collinearity/input/example_input_5.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
T.........
...T......
.T........
..........
..........
..........
..........
..........
..........
..........
Binary file not shown.
66 changes: 66 additions & 0 deletions 2024/08/resonant_collinearity/src/main.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const std = @import("std");
const yazap = @import("yazap");
const resonant_collinearity = @import("resonant_collinearity");

const allocator = std.heap.page_allocator;
const log = std.log;
const App = yazap.App;
const Arg = yazap.Arg;
const string = []const u8;

pub fn main() !void {
var app = App.init(
allocator,
"resonant_collinearity",
"Day 08: Resonant Collinearity",
);
defer app.deinit();

var cmd = app.rootCommand();
cmd.setProperty(.help_on_empty_args);
try cmd.addArg(Arg.singleValueOption(
"filename",
'f',
"Input file (e.g. input/puzzle_input.txt)",
));

const matches = try app.parseProcess();

const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();

var file_content: string = undefined;
if (matches.getSingleValue("filename")) |filename| {
const file = try std.fs.cwd().openFile(filename, .{});
defer file.close();

const file_size = try file.getEndPos();
const buffer: []u8 = try allocator.alloc(u8, file_size);
defer allocator.free(buffer);

_ = try file.readAll(buffer);
file_content = std.mem.Allocator.dupe(
allocator,
u8,
std.mem.trim(u8, buffer, "\n"),
) catch unreachable;
} else {
try app.displayHelp();
return;
}

const result_1 = resonant_collinearity.number_of_outer_antinodes(
file_content,
allocator,
);
try stdout.print("Number of outer antinodes: {!}\n", .{result_1});
try bw.flush();

const result_2 = resonant_collinearity.number_of_all_antinodes(
file_content,
allocator,
);
try stdout.print("Number of all antinodes: {!}\n", .{result_2});
try bw.flush();
}
Loading

0 comments on commit a9aa0e5

Please sign in to comment.