diff options
author | Adam Spragg <adam@spra.gg> | 2022-12-04 16:47:21 +0000 |
---|---|---|
committer | Adam Spragg <adam@spra.gg> | 2022-12-04 16:47:21 +0000 |
commit | e0c2037ee5cede636e907e8af462371ee0e775db (patch) | |
tree | a2546a2e5cafe43ef67737d774e7ad0fd8d1d781 /2b.c |
Advent of code 2022 problems 1-4
Diffstat (limited to '2b.c')
-rw-r--r-- | 2b.c | 90 |
1 files changed, 90 insertions, 0 deletions
@@ -0,0 +1,90 @@ + +#include <stdio.h> +#include <string.h> + + +enum RPS { + ROCK, + PAPR, + SCRS, +}; + + +enum RPS +rps_beats(enum RPS rps) +{ + switch (rps) { + case ROCK: return SCRS; + case PAPR: return ROCK; + case SCRS: return PAPR; + } + return -1; +} + + +enum RPS +rps_loses(enum RPS rps) +{ + switch (rps) { + case ROCK: return PAPR; + case PAPR: return SCRS; + case SCRS: return ROCK; + } + return -1; +} + + +int +rps_pickscore(enum RPS rps) +{ + switch (rps) { + case ROCK: return 1; + case PAPR: return 2; + case SCRS: return 3; + } + return 0; +} + + +int +rps_gamescore_b(enum RPS a, enum RPS b) +{ + if (b == a) + return 3; + if (rps_beats(b) == a) + return 6; + return 0; +} + + +int +main() +{ + char buf[BUFSIZ]; + int score = 0; + + while (fgets(buf, sizeof(buf), stdin)) { + enum RPS a, b; + + if (strlen(buf) != 4) + break; + switch (buf[0]) { + case 'A': a = ROCK; break; + case 'B': a = PAPR; break; + case 'C': a = SCRS; break; + default: return 1; + } + switch (buf[2]) { + case 'X': b = rps_beats(a); break; + case 'Y': b = a; break; + case 'Z': b = rps_loses(a); break; + default: return 1; + } + score += rps_pickscore(b) + rps_gamescore_b(a, b); + } + + printf("Score: %d\n", score); + + return 0; +} + |