From 8f49400aa1caa05ed120e9661287aa2e92da00d3 Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Mon, 28 Oct 2024 09:40:17 +0100 Subject: Fixed patch from quiz7 after text changes #Please enter the commit message for your changes. Lines starting --- patches/patches/058_quiz7.patch | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'patches') diff --git a/patches/patches/058_quiz7.patch b/patches/patches/058_quiz7.patch index 265b9e3..978fbb1 100644 --- a/patches/patches/058_quiz7.patch +++ b/patches/patches/058_quiz7.patch @@ -1,8 +1,8 @@ ---- exercises/058_quiz7.zig 2023-10-03 22:15:22.125574535 +0200 -+++ answers/058_quiz7.zig 2023-10-05 20:04:07.106101152 +0200 +--- exercises/058_quiz7.zig 2024-10-28 09:06:49.448505460 +0100 ++++ answers/058_quiz7.zig 2024-10-28 09:35:14.631932322 +0100 @@ -192,8 +192,8 @@ // Oops! The hermit forgot how to capture the union values - // in a switch statement. Please capture both values as + // in a switch statement. Please capture each value as // 'p' so the print statements work! - .place => print("{s}", .{p.name}), - .path => print("--{}->", .{p.dist}), -- cgit v1.2.3 From 530dcde3d413849dc2fea18eddcfb8cf85a76cb0 Mon Sep 17 00:00:00 2001 From: Roman FroĊ‚ow Date: Sat, 2 Nov 2024 14:38:53 +0100 Subject: patch --- patches/patches/065_builtins2.patch | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'patches') diff --git a/patches/patches/065_builtins2.patch b/patches/patches/065_builtins2.patch index d5da950..d6f0839 100644 --- a/patches/patches/065_builtins2.patch +++ b/patches/patches/065_builtins2.patch @@ -24,16 +24,16 @@ // (which is a zero-bit type that takes up no space at all!): - if (fields[0].??? != void) { + if (fields[0].type != void) { - print(" {s}", .{@typeInfo(Narcissus).@"struct".fields[0].name}); + print(" {s}", .{fields[0].name}); } - if (fields[1].??? != void) { + if (fields[1].type != void) { - print(" {s}", .{@typeInfo(Narcissus).@"struct".fields[1].name}); + print(" {s}", .{fields[1].name}); } - if (fields[2].??? != void) { + if (fields[2].type != void) { - print(" {s}", .{@typeInfo(Narcissus).@"struct".fields[2].name}); + print(" {s}", .{fields[2].name}); } -- cgit v1.2.3 From fb018d212c1e6d8efc42740c6b99d38c62e52d92 Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Sat, 2 Nov 2024 17:03:46 +0100 Subject: Improved maximumNarcissism --- exercises/065_builtins2.zig | 15 ++++++++------- patches/patches/065_builtins2.patch | 4 ++-- 2 files changed, 10 insertions(+), 9 deletions(-) (limited to 'patches') diff --git a/exercises/065_builtins2.zig b/exercises/065_builtins2.zig index 21a3911..6b8168c 100644 --- a/exercises/065_builtins2.zig +++ b/exercises/065_builtins2.zig @@ -110,7 +110,6 @@ pub fn main() void { // name will not be printed if the field is of type 'void' // (which is a zero-bit type that takes up no space at all!): if (fields[0].??? != void) { - print(" {s}", .{fields.name}); print(" {s}", .{fields[0].name}); } @@ -137,14 +136,16 @@ pub fn main() void { // But a change after Zig 0.10.0 added the source file name to the // type. "Narcissus" became "065_builtins2.Narcissus". // -// To fix this, I've added this function to strip the filename from -// the front of the type name in the dumbest way possible. (It returns -// a slice of the type name starting at character 14 (assuming -// single-byte characters). +// To fix this, we've added this function to strip the filename from +// the front of the type name. (It returns a slice of the type name +// starting at the index + 1 of character ".") // // We'll be seeing @typeName again in Exercise 070. For now, you can // see that it takes a Type and returns a u8 "string". fn maximumNarcissism(myType: anytype) []const u8 { - // Turn '065_builtins2.Narcissus' into 'Narcissus' - return @typeName(myType)[14..]; + const indexOf = @import("std").mem.indexOf; + + // Turn "065_builtins2.Narcissus" into "Narcissus" + const name = @typeName(myType); + return name[indexOf(u8, name, ".").? + 1 ..]; } diff --git a/patches/patches/065_builtins2.patch b/patches/patches/065_builtins2.patch index d6f0839..e5e6410 100644 --- a/patches/patches/065_builtins2.patch +++ b/patches/patches/065_builtins2.patch @@ -1,5 +1,5 @@ ---- exercises/065_builtins2.zig 2024-09-02 19:15:56.569952315 +0200 -+++ answers/065_builtins2.zig 2024-09-02 19:13:44.280600350 +0200 +--- exercises/065_builtins2.zig 2024-11-02 16:58:30.607829441 +0100 ++++ answers/065_builtins2.zig 2024-11-02 16:58:33.821220588 +0100 @@ -58,7 +58,7 @@ // Oops! We cannot leave the 'me' and 'myself' fields // undefined. Please set them here: -- cgit v1.2.3 From 8cce587d3ba60d8073a38a87a71caa5bfaa859c6 Mon Sep 17 00:00:00 2001 From: bgthompson Date: Thu, 7 Nov 2024 13:31:32 +1000 Subject: added patch file for 109_vectors --- patches/patches/109_vectors.patch | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 patches/patches/109_vectors.patch (limited to 'patches') diff --git a/patches/patches/109_vectors.patch b/patches/patches/109_vectors.patch new file mode 100644 index 0000000..ec2189e --- /dev/null +++ b/patches/patches/109_vectors.patch @@ -0,0 +1,13 @@ +--- exercises/109_vectors.zig 2024-11-03 11:17:00.928652000 +1000 ++++ answers/109_vectors.zig 2024-11-07 13:11:23.838667200 +1000 +@@ -121,8 +121,8 @@ + + const Vec4 = @Vector(4, f32); + fn calcMaxPairwiseDiffNew( a : Vec4, b : Vec4) f32 { +- const abs_diff_vec = ???; +- const max_diff = @reduce(???, abs_diff_vec); ++ const abs_diff_vec = @abs(a - b); ++ const max_diff = @reduce(.Max, abs_diff_vec); + return max_diff; + } + -- cgit v1.2.3 From 46e8fc0b614bd3187ab6b2c6b4cc83996fb8bf56 Mon Sep 17 00:00:00 2001 From: bgthompson Date: Thu, 7 Nov 2024 13:42:53 +1000 Subject: line ending format patch attempt --- patches/patches/109_vectors.patch | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'patches') diff --git a/patches/patches/109_vectors.patch b/patches/patches/109_vectors.patch index ec2189e..4b11da9 100644 --- a/patches/patches/109_vectors.patch +++ b/patches/patches/109_vectors.patch @@ -6,7 +6,7 @@ fn calcMaxPairwiseDiffNew( a : Vec4, b : Vec4) f32 { - const abs_diff_vec = ???; - const max_diff = @reduce(???, abs_diff_vec); -+ const abs_diff_vec = @abs(a - b); ++ const abs_diff_vec = @abs( a - b ); + const max_diff = @reduce(.Max, abs_diff_vec); return max_diff; } -- cgit v1.2.3 From bfed6600205504a3fd2e3d3922ca5da6995224a7 Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Thu, 7 Nov 2024 15:01:59 +0100 Subject: Fixed formating, created patch file. --- exercises/109_vectors.zig | 42 +++++++++++++++++++-------------------- patches/patches/109_vectors.patch | 8 ++++---- 2 files changed, 25 insertions(+), 25 deletions(-) (limited to 'patches') diff --git a/exercises/109_vectors.zig b/exercises/109_vectors.zig index 106937e..96892ca 100644 --- a/exercises/109_vectors.zig +++ b/exercises/109_vectors.zig @@ -10,16 +10,16 @@ // These are known as "single instruction, multiple data" (SIMD) // instructions. SIMD instructions can make code significantly // more performant. -// +// // To see why, imagine we have a program in which we take the // square root of four (changing) f32 floats. -// +// // A simple compiler would take the program and produce machine code // which calculates each square root sequentially. Most registers on // modern CPUs have 64 bits, so we could imagine that each float moves // into a 64-bit register, and the following happens four times: // -// 32 bits 32 bits +// 32 bits 32 bits // +-------------------+ // register | 0 | x | // +-------------------+ @@ -35,7 +35,7 @@ // Notice that half of the register contains blank data to which // nothing happened. What a waste! What if we were able to use // that space instead? This is the idea at the core of SIMD. -// +// // Most modern CPUs contain specialized registers with at least 128 bits // for performing SIMD instructions. On a machine with 128-bit SIMD // registers, a smart compiler would probably NOT issue four sqrt @@ -50,11 +50,11 @@ // +---------------------------------------+ // register | 4.0 | 9.0 | 25.0 | 49.0 | // +---------------------------------------+ -// +// // | // [SIMD SQRT instruction] // V -// +// // +---------------------------------------+ // register | 2.0 | 3.0 | 5.0 | 7.0 | // +---------------------------------------+ @@ -74,26 +74,26 @@ // SIMD instructions, whenever possible. // // Defining vectors in Zig is straightforwards. No library import is needed. -const v1 = @Vector(3, i32) { 1, 10, 100}; -const v2 = @Vector(3, f32) {2.0, 3.0, 5.0}; +const v1 = @Vector(3, i32){ 1, 10, 100 }; +const v2 = @Vector(3, f32){ 2.0, 3.0, 5.0 }; // Vectors support the same builtin operators as their underlying base types. const v3 = v1 + v1; // { 2, 20, 200}; const v4 = v2 * v2; // { 4.0, 9.0, 25.0}; // Intrinsics that apply to base types usually extend to vectors. -const v5 : @Vector(3, f32) = @floatFromInt(v3); // { 2.0, 20.0, 200.0} -const v6 = v4 - v5; // { 2.0, -11.0, -175.0} -const v7 = @abs(v6); // { 2.0, 11.0, 175.0} +const v5: @Vector(3, f32) = @floatFromInt(v3); // { 2.0, 20.0, 200.0} +const v6 = v4 - v5; // { 2.0, -11.0, -175.0} +const v7 = @abs(v6); // { 2.0, 11.0, 175.0} // We can make constant vectors, and reduce vectors. -const v8 : @Vector(4, u8) = @splat(2); // { 2, 2, 2, 2} -const v8_sum = @reduce(.Add, v8); // 8 -const v8_min = @reduce(.Min, v8); // 2 +const v8: @Vector(4, u8) = @splat(2); // { 2, 2, 2, 2} +const v8_sum = @reduce(.Add, v8); // 8 +const v8_min = @reduce(.Min, v8); // 2 // Fixed-length arrays can be automatically assigned to vectors (and vice-versa). -const single_digit_primes = [4] i8 {2, 3, 5, 7}; -const prime_vector : @Vector(4, i8) = single_digit_primes; +const single_digit_primes = [4]i8{ 2, 3, 5, 7 }; +const prime_vector: @Vector(4, i8) = single_digit_primes; // Now let's use vectors to simplify and optimize some code! // @@ -103,8 +103,8 @@ const prime_vector : @Vector(4, i8) = single_digit_primes; // // Ewa wrote the following function to figure this out. -fn calcMaxPairwiseDiffOld( list1 : [4] f32, list2 : [4] f32) f32 { - var max_diff : f32 = 0; +fn calcMaxPairwiseDiffOld(list1: [4]f32, list2: [4]f32) f32 { + var max_diff: f32 = 0; for (list1, list2) |n1, n2| { const abs_diff = @abs(n1 - n2); if (abs_diff > max_diff) { @@ -120,7 +120,7 @@ fn calcMaxPairwiseDiffOld( list1 : [4] f32, list2 : [4] f32) f32 { // Help Ewa finish the vector version! The examples above should help. const Vec4 = @Vector(4, f32); -fn calcMaxPairwiseDiffNew( a : Vec4, b : Vec4) f32 { +fn calcMaxPairwiseDiffNew(a: Vec4, b: Vec4) f32 { const abs_diff_vec = ???; const max_diff = @reduce(???, abs_diff_vec); return max_diff; @@ -138,8 +138,8 @@ const std = @import("std"); const print = std.debug.print; pub fn main() void { - const l1 = [4] f32 { 3.141, 2.718, 0.577, 1.000}; - const l2 = [4] f32 { 3.154, 2.707, 0.591, 0.993}; + const l1 = [4]f32{ 3.141, 2.718, 0.577, 1.000 }; + const l2 = [4]f32{ 3.154, 2.707, 0.591, 0.993 }; const mpd_old = calcMaxPairwiseDiffOld(l1, l2); const mpd_new = calcMaxPairwiseDiffNew(l1, l2); print("Max difference (old fn): {d: >5.3}\n", .{mpd_old}); diff --git a/patches/patches/109_vectors.patch b/patches/patches/109_vectors.patch index 4b11da9..bf18cc0 100644 --- a/patches/patches/109_vectors.patch +++ b/patches/patches/109_vectors.patch @@ -1,12 +1,12 @@ ---- exercises/109_vectors.zig 2024-11-03 11:17:00.928652000 +1000 -+++ answers/109_vectors.zig 2024-11-07 13:11:23.838667200 +1000 +--- exercises/109_vectors.zig 2024-11-07 14:57:09.673383618 +0100 ++++ answers/109_vectors.zig 2024-11-07 14:22:59.069150138 +0100 @@ -121,8 +121,8 @@ const Vec4 = @Vector(4, f32); - fn calcMaxPairwiseDiffNew( a : Vec4, b : Vec4) f32 { + fn calcMaxPairwiseDiffNew(a: Vec4, b: Vec4) f32 { - const abs_diff_vec = ???; - const max_diff = @reduce(???, abs_diff_vec); -+ const abs_diff_vec = @abs( a - b ); ++ const abs_diff_vec = @abs(a - b); + const max_diff = @reduce(.Max, abs_diff_vec); return max_diff; } -- cgit v1.2.3 From 37a0f4262155dc91e4d02ef593e86dd7050ff814 Mon Sep 17 00:00:00 2001 From: Chris Boesch Date: Thu, 7 Nov 2024 21:45:47 +0100 Subject: Fixed link to format source code. --- exercises/099_formatting.zig | 2 +- patches/patches/099_formatting.patch | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'patches') diff --git a/exercises/099_formatting.zig b/exercises/099_formatting.zig index 4b64209..37fab45 100644 --- a/exercises/099_formatting.zig +++ b/exercises/099_formatting.zig @@ -16,7 +16,7 @@ // Therefore, the comments for the format() function are the only // way to definitively learn how to format strings in Zig: // -// https://github.com/ziglang/zig/blob/master/lib/std/fmt.zig#L29 +// https://github.com/ziglang/zig/blob/master/lib/std/fmt.zig#L33 // // Zig already has a very nice selection of formatting options. // These can be used in different ways, but generally to convert diff --git a/patches/patches/099_formatting.patch b/patches/patches/099_formatting.patch index 384bf86..a56b556 100644 --- a/patches/patches/099_formatting.patch +++ b/patches/patches/099_formatting.patch @@ -1,5 +1,5 @@ ---- exercises/099_formatting.zig 2023-10-03 22:15:22.125574535 +0200 -+++ answers/099_formatting.zig 2023-10-05 20:04:07.292771311 +0200 +--- exercises/099_formatting.zig 2024-11-07 21:45:10.459123650 +0100 ++++ answers/099_formatting.zig 2024-11-07 21:43:55.154345991 +0100 @@ -131,7 +131,7 @@ for (0..size) |b| { // What formatting is needed here to make our columns -- cgit v1.2.3