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 /4a.c |
Advent of code 2022 problems 1-4
Diffstat (limited to '4a.c')
-rw-r--r-- | 4a.c | 53 |
1 files changed, 53 insertions, 0 deletions
@@ -0,0 +1,53 @@ + +#include <stdio.h> +#include <stdlib.h> + + +char * +getsect(int * dest, char * pch, char expect) +{ + char * end; + + if (!pch) + return NULL; + *dest = strtol(pch, &end, 10); + if (*dest == 0 && end == pch) + return NULL; + if (*end != expect) + return NULL; + if (expect) + ++end; + return end; +} + + +int +main() +{ + char buf[BUFSIZ]; + int overlaps = 0; + + while (fgets(buf, sizeof(buf), stdin)) { + int n[4]; + char * pbuf; + + pbuf = getsect(&n[0], buf, '-'); + pbuf = getsect(&n[1], pbuf, ','); + pbuf = getsect(&n[2], pbuf, '-'); + pbuf = getsect(&n[3], pbuf, '\n'); + if (!pbuf) { + fprintf(stderr, "Unexpected line: %s\n", buf); + return -1; + } + if ((n[2] >= n[0] && n[3] <= n[1]) + || (n[0] >= n[2] && n[1] <= n[3])) + { + ++overlaps; + } + } + + printf("Overlaps: %d\n", overlaps); + + return 0; +} + |