221V 14 hours ago
parent
commit
8360290c25
1 changed files with 64 additions and 63 deletions
  1. 64 63
      exercises/063_labels.zig

+ 64 - 63
exercises/063_labels.zig

@@ -57,9 +57,9 @@ const print = @import("std").debug.print;
 const ingredients = 4;
 const ingredients = 4;
 const foods = 4;
 const foods = 4;
 
 
-const Food = struct {
-    name: []const u8,
-    requires: [ingredients]bool,
+const Food = struct{
+  name: []const u8,
+  requires: [ingredients]bool,
 };
 };
 
 
 //                 Chili  Macaroni  Tomato Sauce  Cheese
 //                 Chili  Macaroni  Tomato Sauce  Cheese
@@ -71,70 +71,71 @@ const Food = struct {
 // ------------------------------------------------------
 // ------------------------------------------------------
 
 
 const menu: [foods]Food = [_]Food{
 const menu: [foods]Food = [_]Food{
-    Food{
-        .name = "Mac & Cheese",
-        .requires = [ingredients]bool{ false, true, false, true },
-    },
-    Food{
-        .name = "Chili Mac",
-        .requires = [ingredients]bool{ true, true, false, false },
-    },
-    Food{
-        .name = "Pasta",
-        .requires = [ingredients]bool{ false, true, true, false },
-    },
-    Food{
-        .name = "Cheesy Chili",
-        .requires = [ingredients]bool{ true, false, false, true },
-    },
+  Food{
+    .name = "Mac & Cheese",
+    .requires = [ingredients]bool{ false, true, false, true },
+  },
+  Food{
+    .name = "Chili Mac",
+    .requires = [ingredients]bool{ true, true, false, false },
+  },
+  Food{
+    .name = "Pasta",
+    .requires = [ingredients]bool{ false, true, true, false },
+  },
+  Food{
+    .name = "Cheesy Chili",
+    .requires = [ingredients]bool{ true, false, false, true },
+  },
 };
 };
 
 
-pub fn main() void {
-    // Welcome to Cafeteria USA! Choose your favorite ingredients
-    // and we'll produce a delicious meal.
+pub fn main() void{
+  // Welcome to Cafeteria USA! Choose your favorite ingredients
+  // and we'll produce a delicious meal.
+  //
+  // Cafeteria Customer Note: Not all ingredient combinations
+  // make a meal. The default meal is macaroni and cheese.
+  //
+  // Software Developer Note: Hard-coding the ingredient
+  // numbers (based on array position) will be fine for our
+  // tiny example, but it would be downright criminal in a real
+  // application!
+  const wanted_ingredients = [_]u8{ 0, 3 }; // Chili, Cheese
+  
+  // Look at each Food on the menu...
+  const meal = food_loop: for(menu) |food|{
+    
+    // Now look at each required ingredient for the Food...
+    for(food.requires, 0..) |required, required_ingredient|{
+      
+      // This ingredient isn't required, so skip it.
+      if(!required){ continue; }
+      
+      // See if the customer wanted this ingredient.
+      // (Remember that want_it will be the index number of
+      // the ingredient based on its position in the
+      // required ingredient list for each food.)
+      const found = for(wanted_ingredients) |want_it|{
+        if(required_ingredient == want_it){ break true; }
+      }else false;
+      
+      // We did not find this required ingredient, so we
+      // can't make this Food. Continue the outer loop.
+      if(!found){ continue :food_loop; }
+    }
+    
+    // If we get this far, the required ingredients were all
+    // wanted for this Food.
     //
     //
-    // Cafeteria Customer Note: Not all ingredient combinations
-    // make a meal. The default meal is macaroni and cheese.
-    //
-    // Software Developer Note: Hard-coding the ingredient
-    // numbers (based on array position) will be fine for our
-    // tiny example, but it would be downright criminal in a real
-    // application!
-    const wanted_ingredients = [_]u8{ 0, 3 }; // Chili, Cheese
-
-    // Look at each Food on the menu...
-    const meal = food_loop: for (menu) |food| {
-
-        // Now look at each required ingredient for the Food...
-        for (food.requires, 0..) |required, required_ingredient| {
-
-            // This ingredient isn't required, so skip it.
-            if (!required) continue;
-
-            // See if the customer wanted this ingredient.
-            // (Remember that want_it will be the index number of
-            // the ingredient based on its position in the
-            // required ingredient list for each food.)
-            const found = for (wanted_ingredients) |want_it| {
-                if (required_ingredient == want_it) break true;
-            } else false;
-
-            // We did not find this required ingredient, so we
-            // can't make this Food. Continue the outer loop.
-            if (!found) continue :food_loop;
-        }
-
-        // If we get this far, the required ingredients were all
-        // wanted for this Food.
-        //
-        // Please return this Food from the loop.
-        break;
-    };
-    // ^ Oops! We forgot to return Mac & Cheese as the default
-    // Food when the requested ingredients aren't found.
-
-    print("Enjoy your {s}!\n", .{meal.name});
+    // Please return this Food from the loop.
+    break :food_loop food;
+  }else menu[0];
+  // ^ Oops! We forgot to return Mac & Cheese as the default
+  // Food when the requested ingredients aren't found.
+  
+  print("Enjoy your {s}!\n", .{meal.name});
 }
 }
 
 
 // Challenge: You can also do away with the 'found' variable in
 // Challenge: You can also do away with the 'found' variable in
 // the inner loop. See if you can figure out how to do that!
 // the inner loop. See if you can figure out how to do that!
+