Skip to content

Instantly share code, notes, and snippets.

@marvhus
Created May 21, 2023 14:16
Show Gist options
  • Save marvhus/30c25a63d0c983f892a46c52a1683f2a to your computer and use it in GitHub Desktop.
Save marvhus/30c25a63d0c983f892a46c52a1683f2a to your computer and use it in GitHub Desktop.
#!/usr/bin/jai
#import "Basic";
Rule :: struct {
num: u32;
str: string;
}
fizzbuzz :: (rules: [] Rule, max: u32) {
msg: String_Builder;
init_string_builder(*msg);
for i: 1..max {
for rule: rules {
if i % rule.num == 0 then append(*msg, rule.str);
}
output := builder_to_string(*msg);
print("%\n", ifx output.count == 0 then tprint("%", i) else output);
}
}
main :: () {
rules: [..] Rule;
array_add(*rules, .{3, "Fizz"});
array_add(*rules, .{5, "Buzz"});
if 0 { // Add the extra rules Tom Scott mentioned in his video
array_add(*rules, .{7, "Fuzz"});
array_add(*rules, .{11, "Bizz"});
array_add(*rules, .{13, "Biff"});
}
fizzbuzz(rules, 20);
}
#run main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment