~aperezdc/aoc2022

6c505d730093ad8512f1c58e479c60d5cb3c152b — Adrian Perez de Castro 2 years ago de8dc5e
Day 02, first part
4 files changed, 62 insertions(+), 0 deletions(-)

A day02/.gitignore
A day02/Makefile
A day02/day02.c
A day02/day02.test
A day02/.gitignore => day02/.gitignore +1 -0
@@ 0,0 1,1 @@
day02

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