diff options
author | Julien Dessaux | 2022-12-01 13:05:31 +0100 |
---|---|---|
committer | Julien Dessaux | 2022-12-01 13:07:37 +0100 |
commit | c0877054bdbcb4739ecc842e37b0f1d6ba7af8d0 (patch) | |
tree | 9a06fd81a80fcfda45c7686ea25c1b4e5ed5f292 /2022/01/first.zig | |
parent | Added first part of the third day in zig (diff) | |
download | advent-of-code-c0877054bdbcb4739ecc842e37b0f1d6ba7af8d0.tar.gz advent-of-code-c0877054bdbcb4739ecc842e37b0f1d6ba7af8d0.tar.bz2 advent-of-code-c0877054bdbcb4739ecc842e37b0f1d6ba7af8d0.zip |
2022-01 in zig
Diffstat (limited to '2022/01/first.zig')
-rw-r--r-- | 2022/01/first.zig | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/2022/01/first.zig b/2022/01/first.zig new file mode 100644 index 0000000..0c967fd --- /dev/null +++ b/2022/01/first.zig @@ -0,0 +1,28 @@ +const std = @import("std"); + +const example = @embedFile("example"); +const input = @embedFile("input"); + +pub fn main() anyerror!void { + try std.testing.expectEqual(solve(example), 24000); + const result = try solve(input); + try std.io.getStdOut().writer().print("{}\n", .{result}); +} + +fn solve(puzzle: []const u8) !u64 { + var it = std.mem.split(u8, puzzle, "\n"); + var max: u64 = 0; + var tot: u64 = 0; + while (it.next()) |value| { + const n = std.fmt.parseInt(u64, value, 10) catch 0; + if (n == 0) { + if (tot > max) { + max = tot; + } + tot = 0; + } else { + tot += n; + } + } + return max; +} |