diff options
author | Nguyễn Gia Phong <cnx@loang.net> | 2024-12-05 15:36:38 +0900 |
---|---|---|
committer | Nguyễn Gia Phong <cnx@loang.net> | 2024-12-05 15:36:38 +0900 |
commit | 3399d661d5798041dfec7a06eab7ae5aad4ffc6c (patch) | |
tree | a04d8f26ee6e4464dfcde2b5af8e1c732c5b69e3 /aoc/2024/05/part-two.c | |
parent | b65dedcebd761653d8295e4b9356b4eef8e3ca02 (diff) | |
download | cp-main.tar.gz |
Diffstat (limited to 'aoc/2024/05/part-two.c')
-rw-r--r-- | aoc/2024/05/part-two.c | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/aoc/2024/05/part-two.c b/aoc/2024/05/part-two.c new file mode 100644 index 0000000..443e8b0 --- /dev/null +++ b/aoc/2024/05/part-two.c @@ -0,0 +1,35 @@ +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +static bool after[100][100]; + +int compare(const void *x, const void *y) +{ + return after[*(const unsigned char *) x][*(const unsigned char *) y]; +} + +int main() +{ + unsigned char x, y; + while (scanf("%hhu|%hhu\n", &x, &y) == 2) + after[y][x] = true; + + unsigned char line[90]; + unsigned sum = y = 0; + do { + bool right = true; + do { + line[y] = x; + for (unsigned char i = 0; right && i < y; ++i) + if (after[line[i]][line[y]]) + right = false; + } while (scanf(",%hhu", &x) == 1 && ++y); + if (right) + continue; + qsort(line, ++y, 1, compare); + sum += line[y >> 1]; + } while (y = 0, scanf("%hhu", &x) == 1); + printf("%u\n", sum); + return 0; +} |