From 8af3372cf204861f8541f91e47543f1b33337c5b Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Mon, 4 May 2026 17:15:30 +0200 Subject: [PATCH] fixed removed array multiplication --- build.zig | 2 +- exercises/005_arrays2.zig | 13 +++++-------- exercises/006_strings.zig | 6 +----- exercises/058_quiz7.zig | 11 +++++------ exercises/067_comptime2.zig | 8 ++++---- exercises/074_comptime9.zig | 2 +- exercises/075_quiz8.zig | 8 +++----- patches/patches/005_arrays2.patch | 12 ++++++------ patches/patches/006_strings.patch | 13 ++++--------- patches/patches/058_quiz7.patch | 8 ++++---- patches/patches/067_comptime2.patch | 6 +++--- patches/patches/074_comptime9.patch | 6 +++--- patches/patches/075_quiz8.patch | 14 ++++++++------ patches/patches/110_files2.patch | 6 +++--- 14 files changed, 51 insertions(+), 64 deletions(-) diff --git a/build.zig b/build.zig index 07fe5dd..e8f05a2 100644 --- a/build.zig +++ b/build.zig @@ -777,7 +777,7 @@ const exercises = [_]Exercise{ }, .{ .main_file = "006_strings.zig", - .output = "d=d ha ha ha Major Tom", + .output = "d=d Major Tom", .hint = "Each '???' needs something filled in.", }, .{ diff --git a/exercises/005_arrays2.zig b/exercises/005_arrays2.zig index 497d400..fdbb505 100644 --- a/exercises/005_arrays2.zig +++ b/exercises/005_arrays2.zig @@ -1,5 +1,5 @@ // -// Zig has some fun array operators. +// Zig has one array operators. // // You can use '++' to concatenate two arrays: // @@ -7,12 +7,8 @@ // const b = [_]u8{ 3,4 }; // const c = a ++ b ++ [_]u8{ 5 }; // equals 1 2 3 4 5 // -// You can use '**' to repeat an array: -// -// const d = [_]u8{ 1,2,3 } ** 2; // equals 1 2 3 1 2 3 -// -// Note that both '++' and '**' only operate on arrays while your -// program is _being compiled_. This special time is known in Zig +// Note that '++'' only operate on arrays while your program is +// _being compiled_. This special time is known in Zig // parlance as "comptime" and we'll learn plenty more about that // later. // @@ -30,7 +26,8 @@ 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 = [_]u8{ ??? } ** 3; + 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))); // Okay, that's all of the problems. Let's see the results. // diff --git a/exercises/006_strings.zig b/exercises/006_strings.zig index 5a7172c..08977de 100644 --- a/exercises/006_strings.zig +++ b/exercises/006_strings.zig @@ -27,10 +27,6 @@ pub fn main() void { const d: u8 = ziggy[???]; // (Problem 2) - // Use the array repeat '**' operator to make "ha ha ha ". - const laugh = "ha " ???; - - // (Problem 3) // Use the array concatenation '++' operator to make "Major Tom". // (You'll need to add a space as well!) const major = "Major"; @@ -38,7 +34,7 @@ pub fn main() void { const major_tom = major ??? tom; // That's all the problems. Let's see our results: - std.debug.print("d={u} {s}{s}\n", .{ d, laugh, major_tom }); + std.debug.print("d={u} {s}\n", .{ d, major_tom }); // Keen eyes will notice that we've put 'u' and 's' inside the '{}' // placeholders in the format string above. This tells the // print() function to format the values as a UTF-8 character and diff --git a/exercises/058_quiz7.zig b/exercises/058_quiz7.zig index fda83fc..d3b677b 100644 --- a/exercises/058_quiz7.zig +++ b/exercises/058_quiz7.zig @@ -224,11 +224,10 @@ const NotebookEntry = struct { // +---+----------------+----------------+----------+ // const HermitsNotebook = struct { - // Remember the array repetition operator `**`? It is no mere - // novelty, it's also a great way to assign multiple items in an - // array without having to list them one by one. Here we use it to - // initialize an array with null values. - entries: [place_count]?NotebookEntry = .{null} ** place_count, + // Remember the array repetition function @splat()? It is a great way + // to assign multiple items in an array without having to list them + // one by one. Here we use it to initialize an array with null values. + entries: [place_count]?NotebookEntry = @splat(null), // The next entry keeps track of where we are in our "todo" list. next_entry: u8 = 0, @@ -409,7 +408,7 @@ pub fn main() void { // aside memory for the trip and have the hermit's notebook fill // in the trip from the destination back to the path. Note that // this is the first time we've actually used the destination! - var trip = [_]?TripItem{null} ** (place_count * 2); + var trip: [place_count * 2]?TripItem = @splat(null); notebook.getTripTo(trip[0..], destination) catch |err| { print("Oh no! {}\n", .{err}); diff --git a/exercises/067_comptime2.zig b/exercises/067_comptime2.zig index bdbc3da..e212263 100644 --- a/exercises/067_comptime2.zig +++ b/exercises/067_comptime2.zig @@ -39,16 +39,16 @@ pub fn main() void { var count = 0; count += 1; - const a1: [count]u8 = .{'A'} ** count; + const a1: [count]u8 = @splat('A'); count += 1; - const a2: [count]u8 = .{'B'} ** count; + const a2: [count]u8 = @splat('B'); count += 1; - const a3: [count]u8 = .{'C'} ** count; + const a3: [count]u8 = @splat('C'); count += 1; - const a4: [count]u8 = .{'D'} ** count; + const a4: [count]u8 = @splat('D'); print("{s} {s} {s} {s}\n", .{ a1, a2, a3, a4 }); diff --git a/exercises/074_comptime9.zig b/exercises/074_comptime9.zig index 1fc8e31..dfdc588 100644 --- a/exercises/074_comptime9.zig +++ b/exercises/074_comptime9.zig @@ -32,7 +32,7 @@ fn makeCreature(comptime count: usize, comptime fmt: []const u8) [count]Animal { // We return an array of animals representing the creature. (This is why we // really needed the 'count' parameter. Arrays need a size.) - var animals: [count]Animal = .{undefined} ** count; + var animals: [count]Animal = undefined; var next_animal: usize = 0; inline for (fmt) |char| { diff --git a/exercises/075_quiz8.zig b/exercises/075_quiz8.zig index 63d208b..08270ee 100644 --- a/exercises/075_quiz8.zig +++ b/exercises/075_quiz8.zig @@ -48,9 +48,7 @@ const Path = struct { // instead. // // Please fill in the body of this function! -fn makePath(from: *Place, to: *Place, dist: u8) Path { - -} +fn makePath(from: *Place, to: *Place, dist: u8) Path {} // Using our new function, these path definitions take up considerably less // space in our program now! @@ -97,7 +95,7 @@ const NotebookEntry = struct { }; const HermitsNotebook = struct { - entries: [place_count]?NotebookEntry = .{null} ** place_count, + entries: [place_count]?NotebookEntry = @splat(null), next_entry: u8 = 0, end_of_entries: u8 = 0, @@ -193,7 +191,7 @@ pub fn main() void { } } - var trip = [_]?TripItem{null} ** (place_count * 2); + var trip: [place_count * 2]?TripItem = @splat(null); notebook.getTripTo(trip[0..], destination) catch |err| { print("Oh no! {}\n", .{err}); diff --git a/patches/patches/005_arrays2.patch b/patches/patches/005_arrays2.patch index c576150..d1abb36 100644 --- a/patches/patches/005_arrays2.patch +++ b/patches/patches/005_arrays2.patch @@ -1,6 +1,6 @@ ---- exercises/005_arrays2.zig 2023-10-03 22:15:22.122241138 +0200 -+++ answers/005_arrays2.zig 2023-10-05 20:04:06.862763262 +0200 -@@ -25,12 +25,12 @@ +--- 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 @@ // (Problem 1) // Please set this array concatenating the two arrays above. // It should result in: 1 3 3 7 @@ -10,8 +10,8 @@ // (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 = [_]u8{ ??? } ** 3; -+ const bit_pattern = [_]u8{ 1, 0, 0, 1 } ** 3; +- const bit_pattern_unit = [_]u8{ ??? }; ++ 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. - // diff --git a/patches/patches/006_strings.patch b/patches/patches/006_strings.patch index bd29ecc..e63ab24 100644 --- a/patches/patches/006_strings.patch +++ b/patches/patches/006_strings.patch @@ -1,6 +1,6 @@ ---- exercises/006_strings.zig 2023-10-03 22:15:22.122241138 +0200 -+++ answers/006_strings.zig 2023-10-05 20:04:06.869430053 +0200 -@@ -24,18 +24,18 @@ +--- exercises/006_strings.zig 2026-05-04 17:04:31.763821070 +0200 ++++ answers/006_strings.zig 2026-05-04 17:02:11.672866263 +0200 +@@ -24,14 +24,14 @@ // (Problem 1) // Use array square bracket syntax to get the letter 'd' from // the string "stardust" above. @@ -8,11 +8,6 @@ + const d: u8 = ziggy[4]; // (Problem 2) - // Use the array repeat '**' operator to make "ha ha ha ". -- const laugh = "ha " ???; -+ const laugh = "ha " ** 3; - - // (Problem 3) // Use the array concatenation '++' operator to make "Major Tom". // (You'll need to add a space as well!) const major = "Major"; @@ -21,4 +16,4 @@ + const major_tom = major ++ " " ++ tom; // That's all the problems. Let's see our results: - std.debug.print("d={u} {s}{s}\n", .{ d, laugh, major_tom }); + std.debug.print("d={u} {s}\n", .{ d, major_tom }); diff --git a/patches/patches/058_quiz7.patch b/patches/patches/058_quiz7.patch index 978fbb1..8a4a53c 100644 --- a/patches/patches/058_quiz7.patch +++ b/patches/patches/058_quiz7.patch @@ -1,5 +1,5 @@ ---- exercises/058_quiz7.zig 2024-10-28 09:06:49.448505460 +0100 -+++ answers/058_quiz7.zig 2024-10-28 09:35:14.631932322 +0100 +--- exercises/058_quiz7.zig 2026-05-04 16:34:31.692458399 +0200 ++++ answers/058_quiz7.zig 2026-05-04 16:34:29.239406323 +0200 @@ -192,8 +192,8 @@ // Oops! The hermit forgot how to capture the union values // in a switch statement. Please capture each value as @@ -11,7 +11,7 @@ } } }; -@@ -255,7 +255,7 @@ +@@ -254,7 +254,7 @@ // dereference and optional value "unwrapping" look // together. Remember that you return the address with the // "&" operator. @@ -20,7 +20,7 @@ // Try to make your answer this long:__________; } return null; -@@ -309,7 +309,7 @@ +@@ -308,7 +308,7 @@ // // Looks like the hermit forgot something in the return value of // this function. What could that be? diff --git a/patches/patches/067_comptime2.patch b/patches/patches/067_comptime2.patch index 8e10813..7c880ff 100644 --- a/patches/patches/067_comptime2.patch +++ b/patches/patches/067_comptime2.patch @@ -1,5 +1,5 @@ ---- exercises/067_comptime2.zig 2025-12-07 22:51:24.396031248 +0100 -+++ answers/067_comptime2.zig 2025-12-07 22:50:18.721616293 +0100 +--- exercises/067_comptime2.zig 2026-05-04 15:38:52.565144012 +0200 ++++ answers/067_comptime2.zig 2026-05-04 15:37:20.257213463 +0200 @@ -36,7 +36,7 @@ // In this contrived example, we've decided to allocate some // arrays using a variable count! But something's missing... @@ -8,4 +8,4 @@ + comptime var count = 0; count += 1; - const a1: [count]u8 = .{'A'} ** count; + const a1: [count]u8 = @splat('A'); diff --git a/patches/patches/074_comptime9.patch b/patches/patches/074_comptime9.patch index bc3522d..ef054b9 100644 --- a/patches/patches/074_comptime9.patch +++ b/patches/patches/074_comptime9.patch @@ -1,5 +1,5 @@ ---- exercises/074_comptime9.zig 2026-04-30 19:28:08.990995153 +0200 -+++ answers/074_comptime9.zig 2026-04-30 19:27:09.642728692 +0200 +--- exercises/074_comptime9.zig 2026-05-04 17:11:05.144118157 +0200 ++++ answers/074_comptime9.zig 2026-05-04 17:10:36.778519877 +0200 @@ -28,12 +28,12 @@ start, // Ready to start a new animal. l, // This means we've seen an "l", so if we see an "m", we know it's a Llama. @@ -9,7 +9,7 @@ // We return an array of animals representing the creature. (This is why we // really needed the 'count' parameter. Arrays need a size.) - var animals: [count]Animal = .{undefined} ** count; + var animals: [count]Animal = undefined; - var next_animal: usize = 0; + comptime var next_animal: usize = 0; diff --git a/patches/patches/075_quiz8.patch b/patches/patches/075_quiz8.patch index 1ff5016..97457c0 100644 --- a/patches/patches/075_quiz8.patch +++ b/patches/patches/075_quiz8.patch @@ -1,15 +1,17 @@ ---- exercises/075_quiz8.zig 2023-11-21 14:48:15.440702720 +0100 -+++ answers/075_quiz8.zig 2023-11-21 14:50:23.453311616 +0100 -@@ -49,7 +49,11 @@ +--- exercises/075_quiz8.zig 2026-05-04 15:51:48.254371574 +0200 ++++ answers/075_quiz8.zig 2026-05-04 15:49:28.426445382 +0200 +@@ -48,7 +48,13 @@ + // instead. // // Please fill in the body of this function! - fn makePath(from: *Place, to: *Place, dist: u8) Path { -- +-fn makePath(from: *Place, to: *Place, dist: u8) Path {} ++fn makePath(from: *Place, to: *Place, dist: u8) Path { + return Path{ + .from = from, + .to = to, + .dist = dist, + }; - } ++} // Using our new function, these path definitions take up considerably less + // space in our program now! diff --git a/patches/patches/110_files2.patch b/patches/patches/110_files2.patch index 64dc193..8d65ef8 100644 --- a/patches/patches/110_files2.patch +++ b/patches/patches/110_files2.patch @@ -1,11 +1,11 @@ ---- exercises/110_files2.zig 2026-03-20 19:23:48.874150121 +0100 -+++ answers/110_files2.zig 2026-04-02 10:51:15.815831734 +0200 +--- exercises/110_files2.zig 2026-05-04 17:08:38.913033915 +0200 ++++ answers/110_files2.zig 2026-05-04 17:08:37.112995948 +0200 @@ -39,7 +39,7 @@ // initialize an array of u8 with all letter 'A' // we need to pick the size of the array, 64 seems like a good number // fix the initialization below - var content = ['A']*64; -+ var content = [_]u8{'A'} ** 64; ++ var content: [64]u8 = @splat('A'); // this should print out : `AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA` std.debug.print("{s}\n", .{content});