diff options
author | June McEnroe <june@causal.agency> | 2018-12-09 01:26:30 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-11-22 00:14:25 -0500 |
commit | 9a8d40d65a33d18b1592a84b57c3d0e259c024b6 (patch) | |
tree | 6f6ed0855b5f703c21ccf7a572b58fafd40a152d | |
parent | Solve day 8 part 2 (diff) | |
download | aoc-9a8d40d65a33d18b1592a84b57c3d0e259c024b6.tar.gz aoc-9a8d40d65a33d18b1592a84b57c3d0e259c024b6.zip |
Solve day 9 part 1
-rw-r--r-- | 2018/day09.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/2018/day09.c b/2018/day09.c new file mode 100644 index 0000000..804392b --- /dev/null +++ b/2018/day09.c @@ -0,0 +1,68 @@ +#include <stdio.h> +#include <stdlib.h> + +typedef unsigned uint; + +static struct Marble { + uint num; + struct Marble *prev; + struct Marble *next; +} *current; + +static struct Marble *ins(struct Marble *prev, uint num) { + struct Marble *next = malloc(sizeof(*next)); + next->num = num; + next->prev = prev; + next->next = prev->next; + next->prev->next = next; + next->next->prev = next; + return next; +} + +static struct Marble *rem(struct Marble *marb) { + struct Marble *next = marb->next; + marb->prev->next = marb->next; + marb->next->prev = marb->prev; + free(marb); + return next; +} + +int main() { + uint players, marbles; + scanf("%u players; last marble is worth %u points", &players, &marbles); + marbles += 1; + + uint scores[players]; + for (uint i = 0; i < players; ++i) { + scores[i] = 0; + } + + current = malloc(sizeof(*current)); + current->num = 0; + current->prev = current; + current->next = current; + + uint marble = 1; + while (marble < marbles) { + for (uint i = 0; i < players; ++i) { + if (marble % 23) { + current = ins(current->next, marble); + } else { + scores[i] += marble; + struct Marble *marb = current; + for (uint j = 0; j < 7; ++j) { + marb = marb->prev; + } + scores[i] += marb->num; + current = rem(marb); + } + if (++marble == marbles) break; + } + } + + uint max = 0; + for (uint i = 0; i < players; ++i) { + if (scores[i] > max) max = scores[i]; + } + printf("%u\n", max); +} |