21 Commits

Author SHA1 Message Date
Chris Boesch f39b462659 Merge pull request 'fixed formatting error' (#496) from format_error into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/496
2026-07-21 21:06:57 +02:00
Chris Boesch a2eee9d52f fixed formatting error 2026-07-21 20:54:27 +02:00
Chris Boesch 248093a2de Merge pull request '026 thier to their' (#489) from geoffreygordonashbrook/ziglings_exercises_fork:main into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/489
2026-07-05 22:51:52 +02:00
lineality 004ea3b3ae 026 thier to their 2026-07-05 13:32:37 -04:00
Chris Boesch c85daab1a1 Merge pull request 'rivendell: Remove the resetLine function' (#488) from manliop/exercises:remove-resetLine into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/488
2026-07-02 23:56:15 +02:00
Manlio Perillo f6c3e4edc4 rivendell: Remove the resetLine function
This was necessary in the past (commit 30db9d, 2023-04-17).

The new Progress.zig and, probably, the fact that exercises run on a
separate executable solved the problem.
2026-07-02 15:07:51 +02:00
Chris Boesch a5ed7b636b Merge pull request 'added explanation about stdout' (#487) from i481-stdout-explanation into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/487
2026-07-01 16:32:38 +02:00
Chris Boesch ba99eea43b created also new patch file 2026-07-01 16:28:04 +02:00
Chris Boesch 708436c63d added explanation about stdout 2026-07-01 16:20:41 +02:00
Chris Boesch e65b359055 Merge pull request 'Added comment to help run exercize 108_threading2.zig with the ReleaseFast flag.' (#482) from atici/ziglings-exercises:main into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/482
2026-07-01 16:11:19 +02:00
atici 719f7f0740 removed weird characters 2026-07-01 16:29:20 +03:00
atici 9a1ff49206 Added comment to help run exercize 108 with the ReleaseFast flag. 2026-06-30 11:33:03 +02:00
Chris Boesch 89fe65c158 Merge pull request 'Fix count to actually _be_ a count' (#458) from strega-nil/ziglings-exercises:make-threading-mathier into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/458
2026-06-30 00:25:04 +02:00
Chris Boesch 6e34e835e0 Merge branch 'main' into make-threading-mathier 2026-06-30 00:09:19 +02:00
Nicole Patricia Mazzuca a03b676bed Fix count to actually _be_ a count
Also, start from 1 in the positive direction, instead of 5, since that
is more how the thing works (sorry, mathematician brain).

Additionally, adds some extra information in the comments:
information about how long the code would take if one actually sets
count to 2,5 * 10^12, and adding a plus in front of the 4/1 to show that
that is in fact the first element of the "plus" side.

Finally, add an (floating point) error to the debug print.
2026-06-29 23:30:22 +02:00
Chris Boesch 151db7bcd4 Merge pull request 'Remove prefilled solution' (#485) from it3x/remove-solution into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/485
2026-06-29 20:30:13 +02:00
it3x b39f223b8c Fix patch for 109_files 2026-06-29 17:14:05 +00:00
it3x e932615712 Remove prefilled solution
See #478
2026-06-29 17:09:23 +00:00
it3x 98b831aec4 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
2026-06-29 18:48:05 +02:00
Chris Boesch f69c79bcb6 Merge pull request 'added absolute path to exercises, also for tests' (#484) from root_path into main
Reviewed-on: https://codeberg.org/ziglings/exercises/pulls/484
2026-06-29 18:38:40 +02:00
Chris Boesch 951351aa2e added absolute path to exercises, also for tests 2026-06-29 18:30:49 +02:00
11 changed files with 87 additions and 47 deletions
+16 -2
View File
@@ -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}`.
+8
View File
@@ -30,3 +30,11 @@ pub fn main(init: std.process.Init) !void {
// We just learned of a single statement which can accomplish this.
stdout.print("Hello world!\n", .{});
}
// Now you must be thinking "Why would I need to do this instead of good
// old 'std.debug.print()'?", but here's what you need to understand:
// 'std.debug.print()' prints its output to stderr, while the stdout_writer
// approach prints to stdout.
// A common practice is to pipe output of a command to other commands,
// and in order for the piping to work, those commands expect their input
// to come from stdout, not stderr.
+19 -12
View File
@@ -28,9 +28,9 @@
// We will not go into the formula and its derivation in detail, but
// will deal with the series straight away:
//
// 4 4 4 4 4
// PI = --- - --- + --- - --- + --- ...
// 1 3 5 7 9
// 4 4 4 4 4
// PI = + --- - --- + --- - --- + --- ...
// 1 3 5 7 9
//
// As you can clearly see, the series starts with the whole number 4 and
// approaches the circle number by subtracting and adding smaller and
@@ -71,27 +71,28 @@
const std = @import("std");
pub fn main() !void {
const count = 1_000_000_000;
const count = 500_000_000;
var pi_plus: f64 = 0;
var pi_minus: f64 = 0;
{
// First thread to calculate the plus numbers.
const handle1 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_plus, 5, count });
const handle1 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_plus, 1, count });
defer handle1.join();
// Second thread to calculate the minus numbers.
???
}
// Here we add up the results.
std.debug.print("PI ≈ {d:.8}\n", .{4 + pi_plus - pi_minus});
std.debug.print("PI ≈ {d:.8} (error = {e:.1})\n", .{ pi_plus - pi_minus, std.math.pi - (pi_plus - pi_minus) });
}
fn thread_pi(pi: *f64, begin: u64, end: u64) !void {
var n: u64 = begin;
while (n < end) : (n += 4) {
pi.* += 4 / @as(f64, @floatFromInt(n));
fn thread_pi(pi: *f64, begin: u64, count: u64) !void {
for (0..count) |i| {
const term = 4 / @as(f64, @floatFromInt(begin + 4 * i));
pi.* += term;
}
}
// If you wish, you can increase the number of loop passes, which
@@ -103,8 +104,14 @@ fn thread_pi(pi: *f64, begin: u64, end: u64) !void {
// is created. Otherwise the debug functions slow down the speed
// to such an extent that seconds become minutes during execution.
//
// You can use the following command to build and run this
// exercise with the "ReleaseFast" flag:
//
// zig run exercises/108_threading2.zig -O ReleaseFast
//
// And you should remove the formatting restriction in "print",
// otherwise you will not be able to see the additional digits.
//
// If count = 10_000_000_000_000 you should see the following:
// If count = 2_500_000_000_000, you should see the following:
// 3.141592653589
// (after waiting on the order of 1000 seconds, or 17 minutes)
+1 -1
View File
@@ -50,7 +50,7 @@ pub fn main(init: std.process.Init) !void {
// wait a minute...
// opening a directory might fail!
// what should we do here?
var output_dir: std.Io.Dir = try cwd.openDir(io, "output", .{});
var output_dir: std.Io.Dir = cwd.openDir(io, "output", .{});
defer output_dir.close(io);
// we try to open the file `zigling.txt`,
+4
View File
@@ -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`.
+10 -6
View File
@@ -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);
+5 -3
View File
@@ -1,9 +1,11 @@
--- exercises/026_hello2.zig 2026-01-09 22:51:45.803358789 +0100
+++ answers/026_hello2.zig 2026-01-09 22:50:46.016166527 +0100
@@ -28,5 +28,5 @@
--- exercises/026_hello2.zig 2026-07-01 16:27:18.674459266 +0200
+++ answers/026_hello2.zig 2026-07-01 16:27:08.579220702 +0200
@@ -28,7 +28,7 @@
// to be able to pass it up as a return value of main().
//
// We just learned of a single statement which can accomplish this.
- stdout.print("Hello world!\n", .{});
+ try stdout.print("Hello world!\n", .{});
}
// Now you must be thinking "Why would I need to do this instead of good
+7 -5
View File
@@ -1,5 +1,5 @@
--- exercises/036_enums2.zig 2023-10-03 22:15:22.122241138 +0200
+++ answers/036_enums2.zig 2023-10-05 20:04:07.002765884 +0200
--- exercises/036_enums2.zig 2026-07-21 20:48:50.596099259 +0200
+++ answers/036_enums2.zig 2026-07-21 20:50:14.726826859 +0200
@@ -31,7 +31,7 @@
const Color = enum(u32) {
red = 0xff0000,
@@ -18,9 +18,11 @@
\\</p>
\\
, .{
@intFromEnum(Color.red),
@intFromEnum(Color.green),
- @intFromEnum(Color.red),
- @intFromEnum(Color.green),
- @intFromEnum(???), // Oops! We're missing something!
+ @intFromEnum(Color.blue), // Oops! We're missing something!
+ @backingInt(Color.red),
+ @backingInt(Color.green),
+ @backingInt(Color.blue), // Oops! We're missing something!
});
}
+4 -5
View File
@@ -1,13 +1,12 @@
--- exercises/108_threading2.zig 2026-04-14 06:44:18.848246237 +0200
+++ answers/108_threading2.zig 2026-04-14 08:15:30.894485037 +0200
@@ -81,8 +81,8 @@
--- exercises/108_threading2.zig 2026-06-29 23:25:53
+++ answers/108_threading2.zig 2026-06-29 23:25:25
@@ -82,7 +82,8 @@
defer handle1.join();
// Second thread to calculate the minus numbers.
- ???
-
+ const handle2 = try std.Thread.spawn(.{}, thread_pi, .{ &pi_minus, 3, count });
+ defer handle2.join();
}
// Here we add up the results.
std.debug.print("PI ≈ {d:.8}\n", .{4 + pi_plus - pi_minus});
+11 -2
View File
@@ -1,5 +1,5 @@
--- exercises/109_files.zig 2026-03-20 19:23:48.874150121 +0100
+++ answers/109_files.zig 2026-04-02 10:51:15.813831695 +0200
--- exercises/109_files.zig 2026-06-29 17:08:04.124224900 +0000
+++ answers/109_files.zig 2026-06-29 17:13:20.696220533 +0000
@@ -41,7 +41,7 @@
// by doing nothing
//
@@ -9,6 +9,15 @@
// if there's any other unexpected error we just propagate it through
else => return e,
};
@@ -50,7 +50,7 @@
// wait a minute...
// opening a directory might fail!
// what should we do here?
- var output_dir: std.Io.Dir = cwd.openDir(io, "output", .{});
+ var output_dir: std.Io.Dir = try cwd.openDir(io, "output", .{});
defer output_dir.close(io);
// we try to open the file `zigling.txt`,
@@ -61,7 +61,7 @@
// but here we are not yet done writing to the file
// if only there were a keyword in Zig that
+2 -11
View File
@@ -359,7 +359,6 @@ fn runExe(ctx: Context, ex: Exercise) !void {
return err;
};
resetLine();
print("Checking {s}...\n", .{ex.main_file});
return checkOutput(io, arena, ex, result);
@@ -370,8 +369,7 @@ fn runTest(ctx: Context, ex: Exercise) !void {
const arena = ctx.arena;
print("Compiling {s}...\n", .{ex.main_file});
const path = std.fs.path.join(arena, &.{ ctx.work_path, ex.main_file }) catch
@panic("OOM");
const path = std.fs.path.join(arena, &.{ ctx.root_path, ctx.work_path, ex.main_file }) catch @panic("OOM");
var argv = std.ArrayList([]const u8).initCapacity(arena, 8) catch @panic("OOM");
argv.append(arena, ctx.zig_exe) catch @panic("OOM");
@@ -393,7 +391,6 @@ fn runTest(ctx: Context, ex: Exercise) !void {
return err;
};
resetLine();
print("Checking {s}...\n", .{ex.main_file});
return checkTest(ex, result);
@@ -487,10 +484,6 @@ fn help(ex: Exercise, mode: Mode) void {
});
}
fn resetLine() void {
if (stderr_term_mode == .escape_codes) print("{s}", .{"\x1b[2K\r"});
}
// Removes trailing whitespace per line and any trailing LF at the end.
fn trimLines(arena: std.mem.Allocator, buf: []const u8) ![]const u8 {
var list = try std.ArrayList(u8).initCapacity(arena, buf.len);
@@ -600,8 +593,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",
@@ -1176,7 +1167,7 @@ const exercises = [_]Exercise{
},
.{
.main_file = "108_threading2.zig",
.output = "PI ≈ 3.14159265",
.output = "PI ≈ 3.14159265 (error = 1.0e-9)",
},
.{
.main_file = "109_files.zig",