Skip to content

Instantly share code, notes, and snippets.

@akhildevelops
Created November 9, 2025 13:08
Show Gist options
  • Select an option

  • Save akhildevelops/0611bd5087b65b730b40f3bd00078fdd to your computer and use it in GitHub Desktop.

Select an option

Save akhildevelops/0611bd5087b65b730b40f3bd00078fdd to your computer and use it in GitHub Desktop.
day2.zig
const std = @import("std");
const Complex = struct { i64, i64 };
fn mul(complex1: Complex, complex2: Complex) Complex {
// std.debug.print("{any}:{any}\n", .{ complex1, complex2 });
return .{ (complex1.@"0" * complex2.@"0") - (complex1.@"1" * complex2.@"1"), (complex1.@"0" * complex2.@"1") + (complex1.@"1" * complex2.@"0") };
}
fn add(complex1: Complex, complex2: Complex) Complex {
return .{ complex1.@"0" + complex2.@"0", complex1.@"1" + complex2.@"1" };
}
fn div(complex1: Complex, complex2: Complex) Complex {
return .{ @divTrunc(complex1.@"0", complex2.@"0"), @divTrunc(complex1.@"1", complex2.@"1") };
}
fn part1(A: struct { i64, i64 }, _div: Complex, n_counts: usize) ?Complex {
var R: Complex = .{ 0, 0 };
for (0..n_counts) |_| {
R = mul(R, R);
R = div(R, _div);
R = add(R, A);
if (@abs(R.@"0") >= 1000_000 or @abs(R.@"1") >= 1000_000) return null;
}
return R;
}
fn part2(A: struct { i64, i64 }, grid_size: usize, grid_len: usize) i64 {
var counter: i32 = 0;
const ppsize = std.math.cast(i64, grid_len / (grid_size - 1)).?;
for (0..grid_size) |_row| {
for (0..grid_size) |_col| {
const row = std.math.cast(i64, _row).?;
const col = std.math.cast(i64, _col).?;
const point: Complex = .{ A.@"0" + row * ppsize, A.@"1" + col * ppsize };
const result = part1(point, .{ 100000, 100000 }, 100);
if (result == null) continue;
counter += 1;
}
}
return counter;
}
pub fn main() !void {
const R = part1(part1_input, .{ 10, 10 }, 3);
std.debug.print("{any}\n", .{R.?});
// part2 solution
var n_points = part2(part2_input, 101, 1000);
std.debug.print("{d}\n", .{n_points});
// part3 solution
n_points = part2(part3_input, 1001, 1000);
std.debug.print("{d}\n", .{n_points});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment