From 1e64c4c56f775eb26fb80d4de2e2e9a8bd64e784 Mon Sep 17 00:00:00 2001 From: dusk Date: Fri, 2 Dec 2022 22:03:19 +0100 Subject: [PATCH] Day 2 --- day2/puzzle1.zig | 10 +++++----- day2/puzzle2.zig | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 day2/puzzle2.zig diff --git a/day2/puzzle1.zig b/day2/puzzle1.zig index dc028cc..49d7551 100644 --- a/day2/puzzle1.zig +++ b/day2/puzzle1.zig @@ -25,11 +25,11 @@ pub fn main() !void { //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 += switch (op - me) { + -1, 2 => 6, + -2, 1 => 0, + else => 3, + }; total_points += @intCast(u4, me); } diff --git a/day2/puzzle2.zig b/day2/puzzle2.zig new file mode 100644 index 0000000..dfb192e --- /dev/null +++ b/day2/puzzle2.zig @@ -0,0 +1,30 @@ +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 = line[0] -| 'A'; + const me = line[2] -| 'X'; + + // A 3 1 2 + // B 1 2 3 + // C 2 3 1 + // X Y Z + + // This works, OK? Trust me bro + total_points += ((me + op + 2) % 3) + 1 + (me * 3); + } + print("{}\n", .{total_points}); +}