From 5f524ace81c24a639e026bf09d56fdcdcf3f5b24 Mon Sep 17 00:00:00 2001 From: "Pascal P." Date: Tue, 6 Jan 2026 15:56:27 +0100 Subject: [PATCH] zig - Implemented day01 --- zig/src/days/day01.zig | 79 +++++++++++++++++++++++++++++++++++++++--- zig/src/main.zig | 12 +++++-- zig/src/utils/day.zig | 2 +- 3 files changed, 85 insertions(+), 8 deletions(-) diff --git a/zig/src/days/day01.zig b/zig/src/days/day01.zig index 1c64655..41cbf0a 100644 --- a/zig/src/days/day01.zig +++ b/zig/src/days/day01.zig @@ -1,13 +1,84 @@ const std = @import("std"); const debug = std.debug; +const expectEqual = std.testing.expectEqual; + const day = @import("../utils/day.zig"); pub const Day = struct { pub const nb = 1; - pub fn part1(_: []const u8) void { - debug.print("hello from day01 - part 1", .{}); + pub fn part1(data: []const u8) i32 { + 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); + } + + if (current == 0) { + count += 1; + } + + //debug.print("{s} -> {d} (count: {d})\n", .{ line, current, count }); + } + + return count; } - pub fn part2(_: []const u8) void { - debug.print("hello from day01 - part 2", .{}); + 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); +} diff --git a/zig/src/main.zig b/zig/src/main.zig index 854189a..28f9e86 100644 --- a/zig/src/main.zig +++ b/zig/src/main.zig @@ -7,6 +7,10 @@ const import_days = struct { pub const day01 = @import("days/day01.zig"); }; +test { + std.testing.refAllDecls(@This()); +} + pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; defer _ = gpa.deinit(); @@ -20,21 +24,23 @@ pub fn main() !void { var days = Map.init(alloc); defer days.deinit(); - debug.print("Running day {d}\n", .{arg_day_nb}); inline for (comptime std.meta.declarations(import_days)) |decl| { const day_str = decl.name[3..]; // Skip "day" prefix const day_number = try std.fmt.parseInt(usize, day_str, 10); 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 day = Day{ .nb = imported_day.nb, .part1 = imported_day.part1, .part2 = imported_day.part2 }; // TODO: get the correct input values const buffer = "coucou"; - day.part1(buffer); - day.part2(buffer); + _ = day.part1(buffer); + _ = day.part2(buffer); break; } + } else { + debug.print("Could not find implementation for day {d}\n", .{arg_day_nb}); } } diff --git a/zig/src/utils/day.zig b/zig/src/utils/day.zig index 0c021fa..efbe9e4 100644 --- a/zig/src/utils/day.zig +++ b/zig/src/utils/day.zig @@ -1,6 +1,6 @@ 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 { nb: u32 = 0, part1: fn_part_run,