aoc2022/day2/puzzle1.zig

38 lines
953 B
Zig
Raw Normal View History

2022-12-02 19:41:31 +00:00
const std = @import("std");
const mem = std.mem;
const fmt = std.fmt;
const print = std.debug.print;
const input = @embedFile("input");
pub fn main() !void {
var total_points: u32 = 0;
// Split the input
var input_iter = mem.split(u8, input, "\n");
while (input_iter.next()) |line| {
// Input ends with an empty line
if (line.len == 0) continue;
// Converting character code to points
const op: i4 = @intCast(i4, line[0] -| 64);
const me: i4 = @intCast(i4, line[2] -| 87);
//rock - paper = -1 WIN
//rock - scissors = -2 LOSE
//paper - rock = 1 LOSE
//paper - scissors = -1 WIN
//scissors - rock = 2 WIN
//scissors - paper = 1 LOSE
2022-12-02 21:03:19 +00:00
total_points += switch (op - me) {
-1, 2 => 6,
-2, 1 => 0,
else => 3,
};
2022-12-02 19:41:31 +00:00
total_points += @intCast(u4, me);
}
print("{}\n", .{total_points});
}