From 86ef9552b74d0410ff111fc99923d590c4a08de4 Mon Sep 17 00:00:00 2001 From: Markus Korn Date: Mon, 2 Dec 2024 08:31:33 +0100 Subject: [PATCH] Refactor day02.zig for better line handling - Refactor `part1` function in `day02.zig` to improve structure and readability using the new `Line` struct - Implement new methods for line validation and selective copying in the `Line` struct - Enhance loop operations in `part1` to integrate line validation and parsing improvements - Add unit testing for the `copy_without_item` method to verify its correct behavior --- src/day02.zig | 55 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/src/day02.zig b/src/day02.zig index 23f2c2c..42d97d3 100644 --- a/src/day02.zig +++ b/src/day02.zig @@ -1,33 +1,58 @@ const std = @import("std"); const utils = @import("./utils.zig"); -fn part1(content: []const u8) !u32 { - var safeCount: u32 = 0; - var lineIter = std.mem.tokenizeSequence(u8, content, "\n"); +const Line = struct { + items: []u32, - lines: while (lineIter.next()) |line| { + fn is_valid(self: *Line) bool { var currentValue: ?u32 = null; var isIncreasing: ?bool = null; - var cellIter = std.mem.tokenizeSequence(u8, line, " "); - while (cellIter.next()) |cell| { - const value = try utils.parseInt(u32, cell, 10); + + for (self.items) |value| { if (currentValue == null) { currentValue = value; continue; } else { - if (value == currentValue) continue :lines; + if (value == currentValue) return false; const currentIsIncreasing = value > currentValue.?; if (isIncreasing == null) { isIncreasing = currentIsIncreasing; } else { - if (isIncreasing != currentIsIncreasing) continue :lines; + if (isIncreasing != currentIsIncreasing) return false; } const diff: i64 = @as(i64, currentValue.?) - @as(i64, value); - if (@abs(diff) > 3) continue :lines; + if (@abs(diff) > 3) return false; currentValue = value; } } - safeCount += 1; + return true; + } + + fn copy_without_item(self: *Line, index: usize) !Line { + var items = utils.List(u32).init(utils.gpa); + + for (self.items, 0..) |value, idx| { + if (index == idx) { + continue; + } + try items.append(value); + } + return Line{ .items = try items.toOwnedSlice() }; + } +}; + +fn part1(content: []const u8) !u32 { + var safeCount: u32 = 0; + var lineIter = std.mem.tokenizeSequence(u8, content, "\n"); + + while (lineIter.next()) |line| { + var items = utils.List(u32).init(utils.gpa); + var cellIter = std.mem.tokenizeSequence(u8, line, " "); + while (cellIter.next()) |cell| { + try items.append(try utils.parseInt(u32, cell, 10)); + } + var ln = Line{ .items = try items.toOwnedSlice() }; + if (ln.is_valid()) safeCount += 1; } return safeCount; } @@ -52,3 +77,11 @@ test "day02 -> part1" { const result = try part1(content); try std.testing.expectEqual(@as(u32, 2), result); } + +test "day02 -> copy without item" { + var items = [_]u32{ 1, 2, 3, 4, 5 }; + var ln = Line{ .items = &items }; + const ln1 = try ln.copy_without_item(1); + + try std.testing.expectEqualSlices(u32, ln1.items, &[_]u32{ 1, 3, 4, 5 }); +}