Skip to content

Instantly share code, notes, and snippets.

@akhildevelops
Created November 14, 2025 02:44
Show Gist options
  • Select an option

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

Select an option

Save akhildevelops/ff59d13953690a14d79ba7e605289b5f to your computer and use it in GitHub Desktop.
const std = @import("std");
const buffer1 = "";
const buffer2 = "";
const buffer3 = "";
fn part1(buffer: []const u8) [3]u16 {
var mentors: [3]u16 = .{ 0, 0, 0 };
var pairs: [3]u16 = .{ 0, 0, 0 };
for (0..buffer.len) |index| {
switch (buffer[index]) {
'A' => mentors[0] += 1,
'B' => mentors[1] += 1,
'C' => mentors[2] += 1,
'a' => pairs[0] += mentors[0],
'b' => pairs[1] += mentors[1],
'c' => pairs[2] += mentors[2],
else => {
std.debug.print("{d}", .{buffer1[index]});
unreachable;
},
}
}
return pairs;
}
fn part2(buffer: []const u8) u16 {
const pairs = part1(buffer);
var sum: u16 = 0;
inline for (0..3) |index| {
sum += pairs[index];
}
return sum;
}
fn part3(buffer: []const u8) usize {
// var current_pointers: [3]usize = .{ 0, 0, 0 };
var tail_pointers: [3]usize = .{ 0, 0, 0 };
var forward_pointers: [3]usize = .{ 0, 0, 0 };
var index: ?usize = null;
var tail_index: ?usize = null;
var forward_index: usize = 0;
const repeats: usize = 1000;
const distance: usize = 1000;
var _pairs: usize = 0;
while (index == null or index.? < (repeats * buffer.len)) {
if (forward_index < (repeats * buffer.len)) {
switch (buffer[forward_index % buffer.len]) {
'A' => {
forward_pointers[0] += 1;
},
'B' => {
forward_pointers[1] += 1;
},
'C' => {
forward_pointers[2] += 1;
},
else => {},
}
forward_index += 1;
}
if (forward_index > distance) {
if (index) |_| {
index.? += 1;
} else {
index = 0;
}
if (index != null and index.? > distance) {
if (tail_index) |_| {
tail_index.? += 1;
} else {
tail_index = 0;
}
switch (buffer[tail_index.? % buffer.len]) {
'A' => {
tail_pointers[0] += 1;
},
'B' => {
tail_pointers[1] += 1;
},
'C' => {
tail_pointers[2] += 1;
},
else => {},
}
}
switch (buffer[index.? % buffer.len]) {
'a' => {
_pairs += (forward_pointers[0] - tail_pointers[0]);
},
'b' => {
_pairs += (forward_pointers[1] - tail_pointers[1]);
},
'c' => {
_pairs += (forward_pointers[2] - tail_pointers[2]);
},
else => {},
}
}
}
return _pairs;
}
pub fn main() !void {
const pairs = part1(buffer1);
std.debug.print("{d}\n", .{pairs[0]});
const _pairs = part2(buffer2);
std.debug.print("{d}\n", .{_pairs});
const _pairs2 = part3(buffer3);
std.debug.print("{d}\n", .{_pairs2});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment