From 4f69e038e143ff5104ff48ed9e7d582b19eab10d Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Fri, 2 Dec 2022 12:10:34 +0100 Subject: 2022-02 in zig --- 2022/02-rock-paper-scissors/first.zig | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 2022/02-rock-paper-scissors/first.zig (limited to '2022/02-rock-paper-scissors/first.zig') diff --git a/2022/02-rock-paper-scissors/first.zig b/2022/02-rock-paper-scissors/first.zig new file mode 100644 index 0000000..b1b8559 --- /dev/null +++ b/2022/02-rock-paper-scissors/first.zig @@ -0,0 +1,25 @@ +const std = @import("std"); + +const example = @embedFile("example"); +const input = @embedFile("input"); + +pub fn main() anyerror!void { + try std.testing.expectEqual(solve(example), 15); + const result = try solve(input); + try std.io.getStdOut().writer().print("{}\n", .{result}); +} + +const scores = [3][3]u8{ // X Y Z + [3]u8{ 4, 8, 3 }, // A 4 8 3 + [3]u8{ 1, 5, 9 }, // B 1 5 9 + [3]u8{ 7, 2, 6 }, // C 7 2 6 +}; + +fn solve(puzzle: []const u8) !u64 { + var it = std.mem.tokenize(u8, puzzle, "\n"); + var tot: u64 = 0; + while (it.next()) |line| { + tot += scores[line[0] - 'A'][line[2] - 'X']; + } + return tot; +} -- cgit v1.2.3