From: Mart Lubbers Date: Tue, 21 Dec 2021 10:06:56 +0000 (+0100) Subject: cleanup X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=3d389bb384754ba1ef6fcb893d6fc59455d5d64d;p=advent21.git cleanup --- diff --git a/21b.c b/21b.c index 96153cc..521bd63 100644 --- a/21b.c +++ b/21b.c @@ -1,12 +1,13 @@ #include #include +#include #include -#include -struct result { unsigned long p1wins; unsigned long p2wins; }; -struct player { int pos; int score; }; +struct result { unsigned long p1; unsigned long p2; }; +struct player { uint8_t pos; uint8_t score; }; +struct key { struct player p1; struct player p2; bool p1turn; }; struct cache { - struct key { bool p1turn; struct player p1; struct player p2; } key; + struct key key; struct result res; UT_hash_handle hh; }; @@ -41,41 +42,40 @@ struct result play(bool p1turn, struct player p1, struct player p2) } } -void add_result(struct result *l, struct result r) +static inline void add_result(struct result *l, struct result r) { - l->p1wins += r.p1wins; - l->p2wins += r.p2wins; + l->p1 += r.p1; + l->p2 += r.p2; } +#define FOR_THROWS(x) \ + for (int i = 1; i<=3; i++)\ + for (int j = 1; j<=3; j++)\ + for (int k = 1; k<=3; k++)\ + x + struct result playd (bool p1turn, struct player p1, struct player p2) { - struct result r = {.p1wins=0, .p2wins=0}; - if (p1.score >= 21) { - r.p1wins++; - } else if (p2.score >= 21) { - r.p2wins++; - } else if (p1turn) { - for (int i = 1; i<=3; i++) - for (int j = 1; j<=3; j++) - for (int k = 1; k<=3; k++) - add_result(&r, play(!p1turn, move(p1, i+j+k), p2)); - } else { - for (int i = 1; i<=3; i++) - for (int j = 1; j<=3; j++) - for (int k = 1; k<=3; k++) - add_result(&r, play(!p1turn, p1, move(p2, i+j+k))); - } + struct result r = {.p1=0, .p2=0}; + if (p1.score >= 21) + r.p1++; + else if (p2.score >= 21) + r.p2++; + else if (p1turn) + FOR_THROWS(add_result(&r, play(!p1turn, move(p1, i+j+k), p2))); + else + FOR_THROWS(add_result(&r, play(!p1turn, p1, move(p2, i+j+k)))); return r; } int main() { struct player p1 = {.score=0}, p2 = {.score=0}; - if (2 != scanf( - "Player 1 starting position: %d\n" - "Player 2 starting position: %d\n", &p1.pos, &p2.pos)) + if (2 != scanf("Player 1 starting position: %" SCNu8 "\n" + "Player 2 starting position: %" SCNu8 "\n", + &p1.pos, &p2.pos)) return 1; struct result r = play(true, p1, p2); - printf("%lu\n", r.p1wins > r.p2wins ? r.p1wins : r.p2wins); + printf("%lu\n", r.p1 > r.p2 ? r.p1 : r.p2); }