zig - Implemented day01

This commit is contained in:
Pascal P. 2026-01-06 15:56:27 +01:00
parent fba971a58a
commit 5f524ace81
3 changed files with 85 additions and 8 deletions

View File

@ -1,13 +1,84 @@
const std = @import("std"); const std = @import("std");
const debug = std.debug; const debug = std.debug;
const expectEqual = std.testing.expectEqual;
const day = @import("../utils/day.zig"); const day = @import("../utils/day.zig");
pub const Day = struct { pub const Day = struct {
pub const nb = 1; pub const nb = 1;
pub fn part1(_: []const u8) void { pub fn part1(data: []const u8) i32 {
debug.print("hello from day01 - part 1", .{}); var iter = std.mem.splitAny(u8, data, "\n");
var count: i32 = 0;
var current: i32 = 50;
while (iter.next()) |line| {
const value = std.fmt.parseInt(i32, line[1..], 10) catch 0;
if (line[0] == 'L') {
current = @mod(current - value, 100);
} else {
current = @mod(current + value, 100);
} }
pub fn part2(_: []const u8) void {
debug.print("hello from day01 - part 2", .{}); if (current == 0) {
count += 1;
}
//debug.print("{s} -> {d} (count: {d})\n", .{ line, current, count });
}
return count;
}
pub fn part2(data: []const u8) i32 {
var iter = std.mem.splitAny(u8, data, "\n");
var count: i32 = 0;
var current: i32 = 50;
var new_current: i32 = 0;
var div: u32 = 0;
var nb_rotation: i32 = 0;
while (iter.next()) |line| {
var value = std.fmt.parseInt(i32, line[1..], 10) catch 0;
if (line[0] == 'L') {
value = -value;
}
new_current = current + value;
div = @divTrunc(@abs(new_current), 100);
nb_rotation = @as(i32, @intCast(div));
if (new_current < 0) {
if (current != 0)
nb_rotation += 1;
new_current += 100 * nb_rotation;
} else {
if (new_current > 0) {
new_current -= 100 * nb_rotation;
} else nb_rotation = 1;
}
new_current = @mod(new_current, 100);
//debug.print("{s} -> {d} (count: {d})\n", .{ line, current, count });
current = new_current;
count += nb_rotation;
}
return count;
} }
}; };
test "day 01 part 1" {
const test_input = "L68\nL30\nR48\nL5\nR60\nL55\nL1\nL99\nR14\nL82";
try expectEqual(Day.part1(test_input), 3);
}
test "day 01 part 2" {
const test_input = "L68\nL30\nR48\nL5\nR60\nL55\nL1\nL99\nR14\nL82";
try expectEqual(Day.part2(test_input), 6);
}
test "test_custom_2_1" {
return expectEqual(Day.part2("R48\nL498"), 5);
}
test "test_custom_2_2" {
try expectEqual(Day.part2("R1000"), 10);
}

View File

@ -7,6 +7,10 @@ const import_days = struct {
pub const day01 = @import("days/day01.zig"); pub const day01 = @import("days/day01.zig");
}; };
test {
std.testing.refAllDecls(@This());
}
pub fn main() !void { pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit(); defer _ = gpa.deinit();
@ -20,21 +24,23 @@ pub fn main() !void {
var days = Map.init(alloc); var days = Map.init(alloc);
defer days.deinit(); defer days.deinit();
debug.print("Running day {d}\n", .{arg_day_nb});
inline for (comptime std.meta.declarations(import_days)) |decl| { inline for (comptime std.meta.declarations(import_days)) |decl| {
const day_str = decl.name[3..]; // Skip "day" prefix const day_str = decl.name[3..]; // Skip "day" prefix
const day_number = try std.fmt.parseInt(usize, day_str, 10); const day_number = try std.fmt.parseInt(usize, day_str, 10);
if (day_number == arg_day_nb) { if (day_number == arg_day_nb) {
debug.print("Running day {d}\n", .{arg_day_nb});
const imported_day = @field(import_days, decl.name).Day; const imported_day = @field(import_days, decl.name).Day;
const day = Day{ .nb = imported_day.nb, .part1 = imported_day.part1, .part2 = imported_day.part2 }; const day = Day{ .nb = imported_day.nb, .part1 = imported_day.part1, .part2 = imported_day.part2 };
// TODO: get the correct input values // TODO: get the correct input values
const buffer = "coucou"; const buffer = "coucou";
day.part1(buffer); _ = day.part1(buffer);
day.part2(buffer); _ = day.part2(buffer);
break; break;
} }
} else {
debug.print("Could not find implementation for day {d}\n", .{arg_day_nb});
} }
} }

View File

@ -1,6 +1,6 @@
const std = @import("std"); const std = @import("std");
const fn_part_run = *const fn (str_buffer: []const u8) void; const fn_part_run = *const fn (str_buffer: []const u8) i32;
pub const Day = struct { pub const Day = struct {
nb: u32 = 0, nb: u32 = 0,
part1: fn_part_run, part1: fn_part_run,