A day02/.gitignore => day02/.gitignore +1 -0
A day02/Makefile => day02/Makefile +6 -0
@@ 0,0 1,6 @@
+CFLAGS += -std=c11 -Os
+
+day02: day02.o
+
+clean:
+ $(RM) day02 day02.o
A day02/day02.c => day02/day02.c +52 -0
@@ 0,0 1,52 @@
+/*
+ * day02.c
+ * Copyright (C) 2022 Adrian Perez de Castro <aperez@igalia.com>
+ *
+ * Distributed under terms of the MIT license.
+ */
+
+#include <assert.h>
+#include <stdbool.h>
+#include <stdio.h>
+
+enum Fig {
+ Rock = 1,
+ Paper = 2,
+ Scissors = 3,
+};
+
+static bool
+fig_wins(enum Fig a, enum Fig b)
+{
+ return (a == Rock && b == Scissors)
+ || (a == Paper && b == Rock)
+ || (a == Scissors && b == Paper);
+}
+
+int
+main(int argc, char *argv[])
+{
+ char other;
+ char ours;
+
+ unsigned score = 0;
+
+ while (!feof(stdin) && scanf("%c %c\n", &other, &ours) == 2) {
+ assert(other >= 'A' && other <= 'C');
+ assert(ours >= 'X' && ours <= 'Z');
+
+ const enum Fig fother = other - 'A' + 1;
+ const enum Fig fours = ours - 'X' + 1;
+
+ if (fig_wins(fours, fother)) {
+ score += fours + 6;
+ } else if (fother == fours) {
+ score += fours + 3;
+ } else {
+ score += fours + 0;
+ }
+ }
+ printf("%u\n", score);
+
+ return 0;
+}
A day02/day02.test => day02/day02.test +3 -0
@@ 0,0 1,3 @@
+A Y
+B X
+C Z