From 4ac1d70f93eeaf6e724823329a3a25585bdf2b24 Mon Sep 17 00:00:00 2001
From: Zendril <kenneth.s.brooks@gmail.com>
Date: Tue, 4 Feb 2025 15:35:14 -0500
Subject: [PATCH 1/7] reset and completed exercise tracking wired in

---
 build.zig | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/build.zig b/build.zig
index e528552..c701efd 100644
--- a/build.zig
+++ b/build.zig
@@ -162,6 +162,8 @@ pub fn build(b: *Build) !void {
     const exno: ?usize = b.option(usize, "n", "Select exercise");
     const rand: ?bool = b.option(bool, "random", "Select random exercise");
     const start: ?usize = b.option(usize, "s", "Start at exercise");
+    // flag to reset the exercise progress
+    const reset: ?bool = b.option(bool, "reset", "Reset exercise progress");
 
     const sep = std.fs.path.sep_str;
     const healed_path = if (override_healed_path) |path|
@@ -242,13 +244,34 @@ pub fn build(b: *Build) !void {
         return;
     }
 
+    if (reset) |_| {
+
+        const progress_file = ".progress.txt";
+
+        std.fs.cwd().deleteFile(progress_file) catch |err| {
+            switch (err) {
+                std.fs.Dir.DeleteFileError.FileNotFound => {},
+                else => {
+                    print("Unable to remove progress file, Error: {}\n", .{err});
+                    return err;
+                },
+            }
+        };
+
+        print("Progress reset, .progress.txt removed.\n", .{});
+        std.process.exit(0);
+    }
+
     // Normal build mode: verifies all exercises according to the recommended
     // order.
     const ziglings_step = b.step("ziglings", "Check all ziglings");
     b.default_step = ziglings_step;
 
     var prev_step = &header_step.step;
+    // read the file to find the latest complete, use that in the for loop
+    // for (exercises[(s - 1)..]) |ex| {
     for (exercises) |ex| {
+        // print("here {s}\n", .{ex.main_file});
         const verify_stepn = ZiglingStep.create(b, ex, work_path, .normal);
         verify_stepn.step.dependOn(prev_step);
 
@@ -403,6 +426,18 @@ const ZiglingStep = struct {
             , .{ red, reset, exercise_output, red, reset, output, red, reset });
         }
 
+        const progress = try std.fmt.allocPrint(b.allocator, "{d}", .{self.exercise.number()});
+        defer b.allocator.free(progress);
+
+        const file = try std.fs.cwd().createFile(
+            ".progress.txt",
+            .{ .read = true, .truncate = true },
+        );
+        defer file.close();
+
+        try file.writeAll(progress);
+        try file.sync();
+
         print("{s}PASSED:\n{s}{s}\n\n", .{ green_text, output, reset_text });
     }
 

From e54b30f3d3a4b05d7eb35f066efd3a00c86a4b4f Mon Sep 17 00:00:00 2001
From: Zendril <kenneth.s.brooks@gmail.com>
Date: Tue, 4 Feb 2025 15:37:01 -0500
Subject: [PATCH 2/7] fmt

---
 build.zig | 1 -
 1 file changed, 1 deletion(-)

diff --git a/build.zig b/build.zig
index c701efd..ccb16ac 100644
--- a/build.zig
+++ b/build.zig
@@ -245,7 +245,6 @@ pub fn build(b: *Build) !void {
     }
 
     if (reset) |_| {
-
         const progress_file = ".progress.txt";
 
         std.fs.cwd().deleteFile(progress_file) catch |err| {

From 8e6a52d5a6c9d0de356113494390a9d888d8ee1a Mon Sep 17 00:00:00 2001
From: Zendril <kenneth.s.brooks@gmail.com>
Date: Tue, 4 Feb 2025 21:13:33 -0500
Subject: [PATCH 3/7] tracking, skipping and reset all wired in

---
 build.zig | 31 +++++++++++++++++++++++++++----
 1 file changed, 27 insertions(+), 4 deletions(-)

diff --git a/build.zig b/build.zig
index ccb16ac..2bc948b 100644
--- a/build.zig
+++ b/build.zig
@@ -267,10 +267,33 @@ pub fn build(b: *Build) !void {
     b.default_step = ziglings_step;
 
     var prev_step = &header_step.step;
-    // read the file to find the latest complete, use that in the for loop
-    // for (exercises[(s - 1)..]) |ex| {
-    for (exercises) |ex| {
-        // print("here {s}\n", .{ex.main_file});
+
+    var starting_exercise: u32 = 0;
+
+    if (std.fs.cwd().openFile(".progress.txt", .{})) |progress_file| {
+        defer progress_file.close();
+
+        const progress_file_size = try progress_file.getEndPos();
+
+        var gpa = std.heap.GeneralPurposeAllocator(.{}){};
+        defer _ = gpa.deinit();
+        const allocator = gpa.allocator();
+        const contents = try progress_file.readToEndAlloc(allocator, progress_file_size);
+        defer allocator.free(contents);
+
+        starting_exercise = try std.fmt.parseInt(u32, contents, 10);
+    } else |err| {
+        switch (err) {
+            // This is fine, may be the first time tests are run or progress have been reset
+            std.fs.File.OpenError.FileNotFound => {},
+            else => {
+                print("Unable to open progress file, Error: {}\n", .{err});
+                return err;
+            },
+        }
+    }
+
+    for (exercises[starting_exercise..]) |ex| {
         const verify_stepn = ZiglingStep.create(b, ex, work_path, .normal);
         verify_stepn.step.dependOn(prev_step);
 

From 0903c5927b58f0ca166ee86c458de44fcf4234d6 Mon Sep 17 00:00:00 2001
From: Zendril <kenneth.s.brooks@gmail.com>
Date: Tue, 4 Feb 2025 21:14:41 -0500
Subject: [PATCH 4/7] cleanup for PR

---
 build.zig | 1 -
 1 file changed, 1 deletion(-)

diff --git a/build.zig b/build.zig
index 2bc948b..40a2d47 100644
--- a/build.zig
+++ b/build.zig
@@ -162,7 +162,6 @@ pub fn build(b: *Build) !void {
     const exno: ?usize = b.option(usize, "n", "Select exercise");
     const rand: ?bool = b.option(bool, "random", "Select random exercise");
     const start: ?usize = b.option(usize, "s", "Start at exercise");
-    // flag to reset the exercise progress
     const reset: ?bool = b.option(bool, "reset", "Reset exercise progress");
 
     const sep = std.fs.path.sep_str;

From 92c723536242b31ecf222ec1f00586ebf46ea994 Mon Sep 17 00:00:00 2001
From: Zendril <kenneth.s.brooks@gmail.com>
Date: Tue, 4 Feb 2025 21:29:46 -0500
Subject: [PATCH 5/7] cleanup for PR

---
 build.zig | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/build.zig b/build.zig
index 40a2d47..335a415 100644
--- a/build.zig
+++ b/build.zig
@@ -120,6 +120,8 @@ pub const logo =
     \\
 ;
 
+const progress_filename = ".progress.txt";
+
 pub fn build(b: *Build) !void {
     if (!validate_exercises()) std.process.exit(2);
 
@@ -244,9 +246,7 @@ pub fn build(b: *Build) !void {
     }
 
     if (reset) |_| {
-        const progress_file = ".progress.txt";
-
-        std.fs.cwd().deleteFile(progress_file) catch |err| {
+        std.fs.cwd().deleteFile(progress_filename) catch |err| {
             switch (err) {
                 std.fs.Dir.DeleteFileError.FileNotFound => {},
                 else => {
@@ -256,7 +256,7 @@ pub fn build(b: *Build) !void {
             }
         };
 
-        print("Progress reset, .progress.txt removed.\n", .{});
+        print("Progress reset, {s} removed.\n", .{progress_filename});
         std.process.exit(0);
     }
 
@@ -269,7 +269,7 @@ pub fn build(b: *Build) !void {
 
     var starting_exercise: u32 = 0;
 
-    if (std.fs.cwd().openFile(".progress.txt", .{})) |progress_file| {
+    if (std.fs.cwd().openFile(progress_filename, .{})) |progress_file| {
         defer progress_file.close();
 
         const progress_file_size = try progress_file.getEndPos();
@@ -283,10 +283,12 @@ pub fn build(b: *Build) !void {
         starting_exercise = try std.fmt.parseInt(u32, contents, 10);
     } else |err| {
         switch (err) {
-            // This is fine, may be the first time tests are run or progress have been reset
-            std.fs.File.OpenError.FileNotFound => {},
+
+            std.fs.File.OpenError.FileNotFound => {
+                // This is fine, may be the first time tests are run or progress have been reset
+            },
             else => {
-                print("Unable to open progress file, Error: {}\n", .{err});
+                print("Unable to open {s}: {}\n", .{progress_filename, err});
                 return err;
             },
         }
@@ -451,7 +453,7 @@ const ZiglingStep = struct {
         defer b.allocator.free(progress);
 
         const file = try std.fs.cwd().createFile(
-            ".progress.txt",
+            progress_filename,
             .{ .read = true, .truncate = true },
         );
         defer file.close();

From e09c11a16a12dde4f88b4abbd867626008734a23 Mon Sep 17 00:00:00 2001
From: Zendril <kenneth.s.brooks@gmail.com>
Date: Tue, 4 Feb 2025 21:29:56 -0500
Subject: [PATCH 6/7] cleanup for PR

---
 README.md | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/README.md b/README.md
index f478944..c884f21 100644
--- a/README.md
+++ b/README.md
@@ -173,6 +173,11 @@ zig build -Dn=19 -l
   ...
 ```
 
+To reset the progress (have it run all the exercises that have already been completed):
+```
+zig build -Dreset
+```
+
 ## What's Covered
 
 The primary goal for Ziglings is to cover the core Zig language.

From a9487c246fc35346bc4646af28341a2bb6a83209 Mon Sep 17 00:00:00 2001
From: Zendril <kenneth.s.brooks@gmail.com>
Date: Tue, 4 Feb 2025 22:24:52 -0500
Subject: [PATCH 7/7] cleanup for PR

---
 build.zig | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/build.zig b/build.zig
index 335a415..fd72881 100644
--- a/build.zig
+++ b/build.zig
@@ -294,11 +294,13 @@ pub fn build(b: *Build) !void {
         }
     }
 
-    for (exercises[starting_exercise..]) |ex| {
-        const verify_stepn = ZiglingStep.create(b, ex, work_path, .normal);
-        verify_stepn.step.dependOn(prev_step);
+    for (exercises) |ex| {
+        if (starting_exercise < ex.number()) {
+            const verify_stepn = ZiglingStep.create(b, ex, work_path, .normal);
+            verify_stepn.step.dependOn(prev_step);
 
-        prev_step = &verify_stepn.step;
+            prev_step = &verify_stepn.step;
+        }
     }
     ziglings_step.dependOn(prev_step);