Main thread for Day 1 of the 2025 advent of code. Feel free to discuss the challenge and ask questions. If particular discussions become large, we can break them off into new threads.
Notice that Advent of Code has changed it’s schedule and there will only be 12 days of puzzles this year.
Some Rules:
Please try to keep spoilers within spoiler tags. This is done by typing [spoiler]text to be blurred[\spoiler]. If you have longer sections you can also use the [details=“Title”] tags.
As is usually the case, not much to say really about Day 1; you just have to understand what’s being asked, take advantage of the test cases and work through the problem. Don’t forget modular aritthmetic .
Hi! This is my attempt to learn Zig while doing Advent of Code. Previously I am a webdev with TS, and mobile dev with Flutter. I am continuing my study in CS in which I am learning C and C++, but Zig took my interest and I want to know more.
This is my attempt, which resulting in correct answer:
// zig 0.15.2
const std = @import("std");
pub fn main() !void {
var total_zeroes: i32 = 0;
var current_dial: i32 = 50;
const fname = "day-01.txt";
// begin code I got from consulting with AI
const f = try std.fs.cwd().openFile(fname, .{});
defer f.close();
var read_buf: [21000]u8 = undefined;
var start_index: usize = 0;
var end_index: usize = 0;
while (true) {
if (start_index == end_index) {
start_index = 0;
end_index = try f.read(&read_buf);
if (end_index == 0) break;
}
const slice = read_buf[start_index..end_index];
if (std.mem.indexOfScalar(u8, slice, '\n')) |newline_pos| {
const line = slice[0..newline_pos];
// std.debug.print("Line: {s}\n", .{line});
current_dial = nextDialState(current_dial, line);
if (current_dial == 0) total_zeroes += 1;
start_index += newline_pos + 1;
} else {
// To handle if line is longer
// std.debug.print("Partial/Long Line: {s}", .{slice});
current_dial = nextDialState(current_dial, slice);
if (current_dial == 0) total_zeroes += 1;
start_index = end_index;
}
}
// end code I got from consulting with AI
std.debug.print("total_zeroes: {d}\n", .{total_zeroes});
}
fn nextDialState(curr: i32, action: []const u8) i32 {
const direction = action[0];
const modifier_string = action[1..];
const modifier_int = std.fmt.parseInt(i32, modifier_string, 10) catch unreachable;
// return switch (direction) {
// 'L' => {
// const temp_result = curr - modifier_int;
// return if (temp_result < 0) temp_result + 100 else temp_result;
// },
// 'R' => {
// const temp_result = curr + modifier_int;
// return if (temp_result > 99) temp_result - 100 else temp_result;
// },
// else => curr,
// };
// there is possibility that the dial rotated more than one revolution in each action
var result = switch (direction) {
'L' => curr - modifier_int,
'R' => curr + modifier_int,
else => curr,
};
while (result < 0 or result > 99) {
result = if (result < 0) result + 100 else result - 100;
}
return result;
}
// pub fn main() !void {
// const test_string = "L32";
// const split_string_front = test_string[0..1];
// const split_string_back = test_string[1..];
// std.debug.print("#01: {s}\n", .{test_string});
// std.debug.print("#02: {s}\n", .{split_string_front});
// std.debug.print("#03: {s}\n", .{split_string_back});
// std.debug.print("#04: {s}\n", .{test_string});
// }
I have difficulties in taking from file and reading line-by-line, a little different from C++’s approach. So I consulted with Gemini AI for that. Though I also still having difficulties because apparently, Gemini doesn’t know Zig version 0.15.2.
Please criticize my Zig approach and I can manage brutally honest opinion
Was mainly working on refactoring my advent of code framework yesterday.
Didn’t have too much mental capacity yesterday left, so my solution is just stupidly iterating through the rotations, counting how often it sees a 0. Not the most elegant solution but it yielded the correct solution.
var row_it = std.mem.tokenizeSequence(u8, @embedFile("puzzle"), "\n");
const max = 100; // 0 - 99 = 100
var dial: i32 = 50;
var result: u32 = 0;
while (row_it.next()) |row| {
// parse 'L' as negative rotation
const sign: i32 = if (row[0] == 'L') -1 else 1;
const number = try std.fmt.parseInt(i32, row[1..], 10);
var change = sign * number;
while (change != 0) : (change += -1 * sign) {
dial += sign;
dial = @mod(dial, max);
if (dial == 0) result += 1;
}
}
std.debug.print("\nResult: {d}", .{result});