From b1fa8186c18037335693033f413760453e8e4f9f Mon Sep 17 00:00:00 2001 From: Kai Norman Clasen <46302524+kai-tub@users.noreply.github.com> Date: Thu, 17 Aug 2023 19:43:13 +0200 Subject: [PATCH 1/7] Add link to DoD talk in 101_for5.zig --- exercises/101_for5.zig | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/exercises/101_for5.zig b/exercises/101_for5.zig index 234cc4f..200e71d 100644 --- a/exercises/101_for5.zig +++ b/exercises/101_for5.zig @@ -84,7 +84,7 @@ pub fn main() void { // use a marble scoop, spoon magnet, and feather tongs to grab // each type of object. // -// Now, would you rather the magic bag: +// Now, would you rather use the magic bag: // // A. Grouped the items in clusters so you have to pick up one // marble, then one spoon, then one feather? @@ -95,7 +95,7 @@ pub fn main() void { // marbles at once, then all the spoons, then all of the // feathers? // -// If this metaphor is working, hopefully it's clear that the 'B' +// If this metaphor is working, hopefully, it's clear that the 'B' // option would be much more efficient. // // Well, it probably comes as little surprise that storing and @@ -120,3 +120,7 @@ pub fn main() void { // three arrays of one data type each, like those in the exercise // above (SoA). // +// For a more practical application of "data-oriented design" +// watch the following talk from Andrew Kelley, the creator of Zig: +// https://vimeo.com/649009599 +// From 9fce9bd57f6ccdf0e74f798db3d5ae6219ad9d69 Mon Sep 17 00:00:00 2001 From: Juergen Hoetzel Date: Sat, 26 Aug 2023 13:26:24 +0200 Subject: [PATCH 2/7] Add example for Unicode Code point literal > 255 Only Basic Latin and Latin-1 Supplement code points fit into a single byte. --- exercises/059_integers.zig | 3 ++- patches/patches/059_integers.patch | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/exercises/059_integers.zig b/exercises/059_integers.zig index 4410079..39e077c 100644 --- a/exercises/059_integers.zig +++ b/exercises/059_integers.zig @@ -6,7 +6,8 @@ // const a2: u8 = 0x41; // hexadecimal // const a3: u8 = 0o101; // octal // const a4: u8 = 0b1000001; // binary -// const a5: u8 = 'A'; // UTF-8 code point literal +// const a5: u8 = 'A'; // ASCII code point literal +// const a6: u16 = 'Ȁ'; // Unicode code points can take up to 21 bits // // You can also place underscores in numbers to aid readability: // diff --git a/patches/patches/059_integers.patch b/patches/patches/059_integers.patch index 7075ebe..c75cf8a 100644 --- a/patches/patches/059_integers.patch +++ b/patches/patches/059_integers.patch @@ -1,4 +1,4 @@ -22,24c22,24 +23,25c23,25 < 0o131, // octal < 0b1101000, // binary < 0x66, // hex From 6f34f7ad0e00513c20f4a22dafaa92b2344974b6 Mon Sep 17 00:00:00 2001 From: Chris Boesch <48591413+chrboesch@users.noreply.github.com> Date: Sun, 27 Aug 2023 18:55:54 +0200 Subject: [PATCH 3/7] Update README.md Advanced usage line 'zig build -Dn=x start' deleted, because of https://github.com/ratfactor/ziglings/issues/355 --- README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index e185ef6..fb9d777 100644 --- a/README.md +++ b/README.md @@ -114,12 +114,10 @@ Version-0.11.0-dev.4246+71dfce31b ## Advanced Usage -It can be handy to check just a single exercise or _start_ from a -single exercise: +It can be handy to check just a single exercise: ``` zig build -Dn=19 -zig build -Dn=19 start ``` You can also run without checking for correctness: From fca59c258b50ef31566621b44c156e942c00ca53 Mon Sep 17 00:00:00 2001 From: Chris Boesch <48591413+chrboesch@users.noreply.github.com> Date: Sun, 27 Aug 2023 19:11:21 +0200 Subject: [PATCH 4/7] Fixed some broken patches --- patches/patches/047_methods.patch | 2 +- patches/patches/060_floats.patch | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/patches/patches/047_methods.patch b/patches/patches/047_methods.patch index edd9db1..c64f657 100644 --- a/patches/patches/047_methods.patch +++ b/patches/patches/047_methods.patch @@ -1,4 +1,4 @@ -92c92 +91c91 < ???.zap(???); --- > heat_ray.zap(alien); diff --git a/patches/patches/060_floats.patch b/patches/patches/060_floats.patch index 38ebb41..0cfa18e 100644 --- a/patches/patches/060_floats.patch +++ b/patches/patches/060_floats.patch @@ -1,4 +1,4 @@ -43c43 +46c46 < const shuttle_weight: f16 = 907.18 * 2200; --- > const shuttle_weight: f32 = 907.18 * 2200.0; From d699fc1824b07dedb44e3cf2423812008d8047c7 Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Tue, 19 Sep 2023 21:18:27 -0400 Subject: [PATCH 5/7] Announce move to Codeberg in README --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index fb9d777..478d893 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,13 @@ # Ziglings +## ⚠️ Attention! Ziglings has moved to Codeberg! + +Check out our handy new URL: https://ziglings.org + +Or visit the repo directly at: https://codeberg.org/ziglings/exercises + +*** + Welcome to Ziglings! This project contains a series of tiny broken programs (and one nasty surprise). By fixing them, you'll learn how to read and write [Zig](https://ziglang.org/) code. From 7417f01d5d8fa8bed84849684501c71b821dff68 Mon Sep 17 00:00:00 2001 From: Dave Gauer Date: Tue, 19 Sep 2023 20:51:05 -0400 Subject: [PATCH 6/7] Update README.md with new clone instructions --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 478d893..02de52a 100644 --- a/README.md +++ b/README.md @@ -59,8 +59,8 @@ $ zig version Clone this repository with Git: ``` -$ git clone https://github.com/ratfactor/ziglings -$ cd ziglings +$ git clone https://ziglings.org +$ cd ziglings.org ``` Then run `zig build` and follow the instructions to begin! From b68855fc2de2898dc90af1e5e56235324ecbd5eb Mon Sep 17 00:00:00 2001 From: Laurens Miers Date: Wed, 25 Sep 2024 12:20:34 +0200 Subject: [PATCH 7/7] WIP: comptime experiments --- exercises/066_comptime.zig | 4 ++-- exercises/067_comptime2.zig | 2 +- exercises/068_comptime3.zig | 5 +++-- exercises/069_comptime4.zig | 4 ++-- exercises/070_comptime5.zig | 4 ++-- exercises/071_comptime6.zig | 2 +- exercises/072_comptime7.zig | 2 +- exercises/073_comptime8.zig | 4 ++-- exercises/074_comptime9.zig | 2 +- exercises/075_quiz8.zig | 43 ++++++++++++++++++++++++++++++++++++- 10 files changed, 57 insertions(+), 15 deletions(-) diff --git a/exercises/066_comptime.zig b/exercises/066_comptime.zig index 9b07a2d..124b472 100644 --- a/exercises/066_comptime.zig +++ b/exercises/066_comptime.zig @@ -62,8 +62,8 @@ pub fn main() void { // types with specific sizes. The comptime numbers will be // coerced (if they'll fit!) into your chosen runtime types. // For this it is necessary to specify a size, e.g. 32 bit. - var var_int = 12345; - var var_float = 987.654; + var var_int: u32 = 12345; + var var_float: f32 = 987.654; // We can change what is stored at the areas set aside for // "var_int" and "var_float" in the running compiled program. diff --git a/exercises/067_comptime2.zig b/exercises/067_comptime2.zig index 7d4bdb0..136bca1 100644 --- a/exercises/067_comptime2.zig +++ b/exercises/067_comptime2.zig @@ -35,7 +35,7 @@ pub fn main() void { // In this contrived example, we've decided to allocate some // arrays using a variable count! But something's missing... // - var count = 0; + comptime var count = 0; count += 1; var a1: [count]u8 = .{'A'} ** count; diff --git a/exercises/068_comptime3.zig b/exercises/068_comptime3.zig index 15b8997..4d8419a 100644 --- a/exercises/068_comptime3.zig +++ b/exercises/068_comptime3.zig @@ -43,7 +43,8 @@ const Schooner = struct { // // Please change this so that it sets a 0 scale to 1 // instead. - if (my_scale == 0) @compileError("Scale 1:0 is not valid!"); + // if (my_scale == 0) @compileError("Scale 1:0 is not valid!"); + if (my_scale == 0) my_scale = 1; self.scale = my_scale; self.hull_length /= my_scale; @@ -69,7 +70,7 @@ pub fn main() void { // Hey, we can't just pass this runtime variable as an // argument to the scaleMe() method. What would let us do // that? - var scale: u32 = undefined; + comptime var scale: u32 = undefined; scale = 32; // 1:32 scale diff --git a/exercises/069_comptime4.zig b/exercises/069_comptime4.zig index e090bb3..0263de0 100644 --- a/exercises/069_comptime4.zig +++ b/exercises/069_comptime4.zig @@ -42,8 +42,8 @@ pub fn main() void { // 2) Sets the size of the array of type T (which is the // sequence we're creating and returning). // -fn makeSequence(comptime T: type, ??? size: usize) [???]T { - var sequence: [???]T = undefined; +fn makeSequence(comptime T: type, comptime size: usize) [size]T { + var sequence: [size]T = undefined; var i: usize = 0; while (i < size) : (i += 1) { diff --git a/exercises/070_comptime5.zig b/exercises/070_comptime5.zig index 3b85aae..69a529b 100644 --- a/exercises/070_comptime5.zig +++ b/exercises/070_comptime5.zig @@ -123,8 +123,8 @@ fn isADuck(possible_duck: anytype) bool { // Please make sure MyType has both waddle() and quack() // methods: const MyType = @TypeOf(possible_duck); - const walks_like_duck = ???; - const quacks_like_duck = ???; + const walks_like_duck = @hasDecl(MyType, "waddle"); + const quacks_like_duck = @hasDecl(MyType, "quack"); const is_duck = walks_like_duck and quacks_like_duck; diff --git a/exercises/071_comptime6.zig b/exercises/071_comptime6.zig index 7723291..d467184 100644 --- a/exercises/071_comptime6.zig +++ b/exercises/071_comptime6.zig @@ -40,7 +40,7 @@ pub fn main() void { const fields = @typeInfo(Narcissus).Struct.fields; - ??? { + inline for (fields) |field| { if (field.type != void) { print(" {s}", .{field.name}); } diff --git a/exercises/072_comptime7.zig b/exercises/072_comptime7.zig index 9bc30fb..48bb39e 100644 --- a/exercises/072_comptime7.zig +++ b/exercises/072_comptime7.zig @@ -35,7 +35,7 @@ pub fn main() void { // at compile time. // // Please fix this to loop once per "instruction": - ??? (i < instructions.len) : (???) { + inline while (i < instructions.len) : (i += 3) { // This gets the digit from the "instruction". Can you // figure out why we subtract '0' from it? diff --git a/exercises/073_comptime8.zig b/exercises/073_comptime8.zig index fe1f8ea..2e69566 100644 --- a/exercises/073_comptime8.zig +++ b/exercises/073_comptime8.zig @@ -32,12 +32,12 @@ const llamas = [llama_count]u32{ 5, 10, 15, 20, 25 }; pub fn main() void { // We meant to fetch the last llama. Please fix this simple // mistake so the assertion no longer fails. - const my_llama = getLlama(5); + const my_llama = getLlama(4); print("My llama value is {}.\n", .{my_llama}); } -fn getLlama(i: usize) u32 { +fn getLlama(comptime i: usize) u32 { // We've put a guard assert() at the top of this function to // prevent mistakes. The 'comptime' keyword here means that // the mistake will be caught when we compile! diff --git a/exercises/074_comptime9.zig b/exercises/074_comptime9.zig index 6db8ad2..d5e6e67 100644 --- a/exercises/074_comptime9.zig +++ b/exercises/074_comptime9.zig @@ -39,7 +39,7 @@ const llamas = makeLlamas(llama_count); // And here's the function. Note that the return value type // depends on one of the input arguments! -fn makeLlamas(count: usize) [count]u8 { +fn makeLlamas(comptime count: usize) [count]u8 { var temp: [count]u8 = undefined; var i = 0; diff --git a/exercises/075_quiz8.zig b/exercises/075_quiz8.zig index 12b460c..8eee967 100644 --- a/exercises/075_quiz8.zig +++ b/exercises/075_quiz8.zig @@ -8,6 +8,8 @@ // of it. // const print = @import("std").debug.print; +const mem = @import("std").mem; +const fmt = @import("std").fmt; const TripError = error{ Unreachable, EatenByAGrue }; @@ -49,12 +51,47 @@ const Path = struct { // // Please fill in the body of this function! fn makePath(from: *Place, to: *Place, dist: u8) Path { + return Path{ + .from = from, + .to = to, + .dist = dist, + }; +} +fn makePath2(comptime path: []const u8) Path { + var it = mem.split(u8, path, "->"); + var from: []const u8 = it.first(); + it = mem.split(u8, it.next().?, ":"); + var to: []const u8 = it.first(); + var dist: []const u8 = it.next().?; + return Path{ + .from = &@field(@This(), from), + .to = &@field(@This(), to), + .dist = try fmt.parseInt(u8, dist, 10), + }; +} + +fn makePath3(comptime path: []const u8) Path { + var it = mem.split(u8, path, "->"); + var from: []const u8 = it.first(); + var list = it.next().?; + var connections_it = mem.split(u8, list[1..(path.len - from.len - 3)], " "); + + while (connections_it.next()) |connect| { + var connection_it = mem.split(u8, connect, ":"); + return Path{ + .from = &@field(@This(), from), + .to = &@field(@This(), connection_it.first()), + .dist = try fmt.parseInt(u8, connection_it.next().?, 10), + }; + } } // Using our new function, these path definitions take up considerably less // space in our program now! -const a_paths = [_]Path{makePath(&a, &b, 2)}; +// const a_paths = [_]Path{makePath(&a, &b, 2)}; +const a_paths = [_]Path{makePath2("a->b:2")}; +// const a_paths = makePath3("a->(b:2)"); const b_paths = [_]Path{ makePath(&b, &a, 2), makePath(&b, &d, 1) }; const c_paths = [_]Path{ makePath(&c, &d, 3), makePath(&c, &e, 2) }; const d_paths = [_]Path{ makePath(&d, &b, 1), makePath(&d, &c, 3), makePath(&d, &f, 7) }; @@ -154,6 +191,10 @@ pub fn main() void { const start = &a; // Archer's Point const destination = &f; // Fox Pond + // makePath3("a->(b:2 d:3)"); + var temp: Path = makePath3("a->(b:2)"); + _ = temp; + // We could either have this: // // a.paths = a_paths[0..];