diff options
author | Christian Segundo | 2022-12-01 20:07:30 +0100 |
---|---|---|
committer | Christian Segundo | 2022-12-01 20:07:30 +0100 |
commit | 77623e0a5bdbfb271bc02f26cf2b377ec16b55ec (patch) | |
tree | e76a1bd90d25b931a83efefaf740f34a53534ab1 /util/mem.zig | |
parent | c316d355c3458aedec7c7cb3420ddba05888e705 (diff) | |
download | advent-of-zig-2022-77623e0a5bdbfb271bc02f26cf2b377ec16b55ec.tar.gz |
quick refactor to add utils
Diffstat (limited to 'util/mem.zig')
-rw-r--r-- | util/mem.zig | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/util/mem.zig b/util/mem.zig new file mode 100644 index 0000000..89ba67b --- /dev/null +++ b/util/mem.zig @@ -0,0 +1,17 @@ +const std = @import("std"); +const math = std.math; + +/// Returns the position of the smallest number in a slice. +pub fn min_idx(comptime T: type, slice: []const T) usize { + var best = slice[0]; + var idx: usize = 0; + + for (slice[1..]) |item, i| { + const possible_best = math.min(best, item); + if (best > possible_best) { + best = possible_best; + idx = i + 1; + } + } + return idx; +} |