From e3fa47e07151345e7cf6d924a309664f0a534619 Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Wed, 6 Dec 2023 18:56:28 +0400 Subject: [PATCH] Day 6 part 1 complete. --- Makefile | 4 ++ day6/in.my | 2 + day6/in.test | 2 + day6/p1.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 day6/in.my create mode 100644 day6/in.test create mode 100644 day6/p1.c diff --git a/Makefile b/Makefile index 2da10eb..92eedd8 100644 --- a/Makefile +++ b/Makefile @@ -29,5 +29,9 @@ day5: day5/p1.c day5/p2.c ${CC} ${CFLAGS} $@/p1.c -o bin/$@p1 ${CC} ${CFLAGS} -fopenmp $@/p2.c -o bin/$@p2 +day6: day6/p1.c day6/p2.c + ${CC} ${CFLAGS} $@/p1.c -o bin/$@p1 +# ${CC} ${CFLAGS} $@/p2.c -o bin/$@p2 + clean: rm -f bin/day*p* \ No newline at end of file diff --git a/day6/in.my b/day6/in.my new file mode 100644 index 0000000..2eb8017 --- /dev/null +++ b/day6/in.my @@ -0,0 +1,2 @@ +Time: 44 89 96 91 +Distance: 277 1136 1890 1768 diff --git a/day6/in.test b/day6/in.test new file mode 100644 index 0000000..28f5ae9 --- /dev/null +++ b/day6/in.test @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 diff --git a/day6/p1.c b/day6/p1.c new file mode 100644 index 0000000..9c32545 --- /dev/null +++ b/day6/p1.c @@ -0,0 +1,121 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include + + +#define ISDIGIT(c) (c >= 0x30 && c <= 0x39) + +int64_t parse_number(char *end); + + +int main(int argc, char **argv) { + FILE *input; + size_t line_buf_length = 0; + + if (argc == 1 || argv[1][0] == '-') + input = stdin; + else if ((input = fopen(argv[1], "r")) == NULL) { + fprintf(stderr, "Cannot open file %s\n", argv[1]); + return -1; + } + + char *time_line = NULL, *distance_line = NULL; + ssize_t time_line_length = 0, distance_line_length = 0; + + if ((time_line_length = getline(&time_line, &line_buf_length, input)) == -1) { + fprintf(stderr, "No time line in a file %s.", argv[1]); + return -1; + } + + if ((distance_line_length = getline(&distance_line, &line_buf_length, input)) == -1) { + fprintf(stderr, "No distance line in a file %s.", argv[1]); + return -1; + } + + uint64_t *times = (uint64_t *) calloc(2, sizeof(uint64_t)); + uint64_t *distances = (uint64_t *) calloc(2, sizeof(uint64_t)); + + ssize_t times_length = 2, distances_length = 2; + + for (ssize_t i = time_line_length-2, j = 0;;) { + while (!ISDIGIT(time_line[i])) { + --i; + if (time_line[i] == ':') + goto tl_end; + } + + if (j == times_length) + times = (uint64_t *) realloc(times, ++times_length * sizeof(uint64_t)); + + times[j++] = parse_number(time_line+i); + + while(ISDIGIT(time_line[i])) --i; + } + tl_end: + + for (ssize_t i = distance_line_length-2, j = 0;;) { + while (!ISDIGIT(distance_line[i])) { + --i; + if (distance_line[i] == ':') + goto dl_end; + } + + if (j == distances_length) + distances = (uint64_t *) realloc(distances, ++distances_length * sizeof(uint64_t)); + + distances[j++] = parse_number(distance_line+i); + + while(ISDIGIT(distance_line[i])) --i; + } + dl_end: + + uint64_t total_ways = 1; + + for (ssize_t i = 0; i < times_length; ++i) { + uint64_t tmid = times[i] / 2; + uint64_t ways = 0; + + printf(":: %lu\n", times[i]); + + for (uint64_t ht = tmid; ht <= times[i]; ++ht) { + if (ht * (times[i] - ht) > distances[i]) + ++ways; + else + break; + } + + for (uint64_t ht = tmid-1; ht != 0; --ht) { + if (ht * (times[i] - ht) > distances[i]) + ++ways; + else + break; + } + + printf(":: %lu = %lu\n", times[i], ways); + + total_ways *= ways; + } + + free(times); + free(distances); + + printf("%lu\n", total_ways); + + return 0; +} + +int64_t parse_number(char *end) { + int64_t num = 0; + + for (size_t m = 1;; m *= 10) { + if (*end>>4 != 0x3) + break; + num += (*end&0xf) * m; + --end; + } + + return num; +} \ No newline at end of file