const std = @import("std"); pub fn main() void { const input = @embedFile("input"); var iter = std.mem.split(u8, input, "\n"); var totalPoints: usize = 0; while (iter.next()) |round| { // Not a valid round. Not necessary, but just in case if (round.len < 3) continue; if (round[0] < 'A' or round[0] > 'C') continue; if (round[2] < 'X' or round[2] > 'Z') continue; // Convert letters to numbers const them = round[0] - 'A'; const result = round[2] - 'X'; // Now the points per result is easy // {0,1,2} * 3 == {0,3,6} // which is what we want. const resultPoints = result * 3; // Now this is a bit more complex. // We need some kind of operation that // returns us this: // // them (0,1,2) // die(0) {2,0,1} {3,1,2} // tie(1) {0,1,2} -+1-> {1,2,3} // win(2) {1,2,0} {2,3,1} // // ex.: if they do rock (0) // and I die (0) // I have to choose scissors (2) // which gives me 3 points (2+1) // // We notice that 'them + result' gives: // // them (0,1,2) // die(0) {0,1,2} // tie(1) {1,2,3} // win(2) {2,3,4} // // If we wrap the results with '(them + result)%3' // and then shift it by adding before wrapping // '(them + result + 2)%3': // // them (0,1,2) // die(0) {0,1,2} {2,0,1} // tie(1) {1,2,0} -+2-> {0,1,2} // win(2) {2,0,1} {1,2,0} // // Now it's just a matter of adding one to that const shapePoints = ((them + result + 2) % 3) + 1; totalPoints += resultPoints + shapePoints; } std.debug.print("{}\n", .{totalPoints}); }