From 2b922997fb893a5bc14f157ad5db27ae77cec6a6 Mon Sep 17 00:00:00 2001 From: Adam Spragg Date: Sat, 14 Jan 2023 17:57:58 +0000 Subject: Puzzle 2: Consolidate 2a/2b programs into `2` with `-p` option Like the later puzzles --- 2.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2a.c | 78 -------------------------------------- 2b.c | 90 -------------------------------------------- makefile | 2 +- 4 files changed, 129 insertions(+), 169 deletions(-) create mode 100644 2.c delete mode 100644 2a.c delete mode 100644 2b.c diff --git a/2.c b/2.c new file mode 100644 index 0000000..0fcd959 --- /dev/null +++ b/2.c @@ -0,0 +1,128 @@ + +#include +#include +#include +#include + + +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(int argc, char ** argv) +{ + int part = 1, i; + char buf[BUFSIZ]; + int score = 0; + + while ((i = getopt(argc, argv, "p:")) != -1) { + switch (i) { + case 'p': + part = atoi(optarg); + break; + + default: + return -1; + } + } + + 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: + fprintf(stderr, "Unexpected input: %c (0x%2d)\n", buf[0], buf[0]); + return -1; + } + switch (part) { + case 1: + switch (buf[2]) { + case 'X': b = ROCK; break; + case 'Y': b = PAPR; break; + case 'Z': b = SCRS; break; + default: + fprintf(stderr, "Unexpected input: %c (0x%2d)\n", buf[2], buf[2]); + return -1; + } + break; + + case 2: + switch (buf[2]) { + case 'X': b = rps_beats(a); break; + case 'Y': b = a; break; + case 'Z': b = rps_loses(a); break; + default: + fprintf(stderr, "Unexpected input: %c (0x%2d)\n", buf[2], buf[2]); + return -1; + } + break; + + default: + fprintf(stderr, "Unexpected puzzle part %d\n", part); + return -1; + } + + score += rps_pickscore(b) + rps_gamescore_b(a, b); + } + + printf("Score: %d\n", score); + + return 0; +} + diff --git a/2a.c b/2a.c deleted file mode 100644 index 0985442..0000000 --- a/2a.c +++ /dev/null @@ -1,78 +0,0 @@ - -#include -#include - - -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; -} - - -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 = ROCK; break; - case 'Y': b = PAPR; break; - case 'Z': b = SCRS; break; - default: return 1; - } - score += rps_pickscore(b) + rps_gamescore_b(a, b); - } - - printf("Score: %d\n", score); - - return 0; -} - diff --git a/2b.c b/2b.c deleted file mode 100644 index 640d6e4..0000000 --- a/2b.c +++ /dev/null @@ -1,90 +0,0 @@ - -#include -#include - - -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; -} - diff --git a/makefile b/makefile index 0d75c03..048a599 100644 --- a/makefile +++ b/makefile @@ -3,7 +3,7 @@ CFLAGS=-Wall -O2 all: bin \ bin/1 \ - bin/2a bin/2b \ + bin/2 \ bin/3a bin/3b \ bin/4a bin/4b \ bin/5 \ -- cgit v1.2.1