From 98b831aec4ea4babe4a71b7bd5d90f2ac392ce81 Mon Sep 17 00:00:00 2001 From: it3x Date: Sat, 27 Jun 2026 09:04:32 +0000 Subject: [PATCH] Refresh 005_arrays2 exercise Updated the exercise to use SIMD. This allows us to cross-reference the concept from 112_vectors back to 005_arrays2, introducing a handy feature of vectors early on. See #430 --- exercises/005_arrays2.zig | 18 ++++++++++++++++-- exercises/112_vectors.zig | 4 ++++ patches/patches/005_arrays2.patch | 16 ++++++++++------ rivendell/elrond.zig | 2 -- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/exercises/005_arrays2.zig b/exercises/005_arrays2.zig index 07b9abe..1de9a36 100644 --- a/exercises/005_arrays2.zig +++ b/exercises/005_arrays2.zig @@ -26,8 +26,13 @@ pub fn main() void { // (Problem 2) // Please set this array using repetition. // It should result in: 1 0 0 1 1 0 0 1 1 0 0 1 - const bit_pattern_unit = [_]u8{ ??? }; - const bit_pattern: [3 * bit_pattern_unit.len]u8 = @bitCast(@as([3][bit_pattern_unit.len]u8, @splat(bit_pattern_unit))); + const bit_pattern_unit = ???; + + // How long should the bit pattern be? + const len = ???; + + // For now, don't worry about the use of SIMD. + const bit_pattern: [len]u8 = std.simd.repeat(len, bit_pattern_unit); // Okay, that's all of the problems. Let's see the results. // @@ -53,3 +58,12 @@ pub fn main() void { std.debug.print("\n", .{}); } + +// For the curious: +// +// The `std.simd.repeat` function takes a target length and a pattern, +// and returns a vector filled with that pattern repeated to the +// desired length. +// +// For example, `repeat(5, [_]u8{1, 2})` will return a vector +// equivalent to `.{1, 2, 1, 2, 1}`. diff --git a/exercises/112_vectors.zig b/exercises/112_vectors.zig index 748c086..0c43f9c 100644 --- a/exercises/112_vectors.zig +++ b/exercises/112_vectors.zig @@ -145,3 +145,7 @@ pub fn main() void { print("Max difference (old fn): {d: >5.3}\n", .{mpd_old}); print("Max difference (new fn): {d: >5.3}\n", .{mpd_new}); } + +// Another cool feature of Vectors is repeating patterns. +// Remember the arrays exercise from earlier where we created an array +// by repeating a pattern? See `005_arrays2.zig`. diff --git a/patches/patches/005_arrays2.patch b/patches/patches/005_arrays2.patch index d1abb36..0c72bd7 100644 --- a/patches/patches/005_arrays2.patch +++ b/patches/patches/005_arrays2.patch @@ -1,6 +1,6 @@ ---- exercises/005_arrays2.zig 2026-05-04 16:26:32.778330847 +0200 -+++ answers/005_arrays2.zig 2026-05-04 16:26:13.082917974 +0200 -@@ -21,12 +21,12 @@ +--- exercises/005_arrays2.zig 2026-06-27 09:03:29.918124920 +0000 ++++ answers/005_arrays2.zig 2026-06-27 09:01:13.162138431 +0000 +@@ -21,15 +21,15 @@ // (Problem 1) // Please set this array concatenating the two arrays above. // It should result in: 1 3 3 7 @@ -10,8 +10,12 @@ // (Problem 2) // Please set this array using repetition. // It should result in: 1 0 0 1 1 0 0 1 1 0 0 1 -- const bit_pattern_unit = [_]u8{ ??? }; +- const bit_pattern_unit = ???; + const bit_pattern_unit = [_]u8{ 1, 0, 0, 1 }; - const bit_pattern: [3 * bit_pattern_unit.len]u8 = @bitCast(@as([3][bit_pattern_unit.len]u8, @splat(bit_pattern_unit))); - // Okay, that's all of the problems. Let's see the results. + // How long should the bit pattern be? +- const len = ???; ++ const len = 3 * bit_pattern_unit.len; + + // For now, don't worry about the use of SIMD. + const bit_pattern: [len]u8 = std.simd.repeat(len, bit_pattern_unit); diff --git a/rivendell/elrond.zig b/rivendell/elrond.zig index 371c16f..2d26ce9 100644 --- a/rivendell/elrond.zig +++ b/rivendell/elrond.zig @@ -599,8 +599,6 @@ const exercises = [_]Exercise{ .main_file = "005_arrays2.zig", .output = "LEET: 1337, Bits: 100110011001", .hint = "Fill in the two arrays.", - .skip = true, - .skip_hint = "Better solution is needed.", }, .{ .main_file = "006_strings.zig",