aoc2022/day2/puzzle1.zig

38 lines
984 B
Zig

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
switch (op - me) {
-1, 2 => total_points += 6,
-2, 1 => total_points += 0,
else => total_points += 3,
}
total_points += @intCast(u4, me);
}
print("{}\n", .{total_points});
}