aoc2023/day6/p1.c

110 lines
2.7 KiB
C

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define ISDIGIT(c) (c >= 0x30 && c <= 0x39)
inline uint64_t parse_number(const 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]))
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]))
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;
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;
total_ways *= ways;
}
free(times);
free(distances);
printf("%lu\n", total_ways);
return 0;
}
inline uint64_t parse_number(const char *end) {
uint64_t num = 0;
for (size_t m = 1;; m *= 10) {
if (*end>>4 != 0x3)
break;
num += (*end&0xf) * m;
--end;
}
return num;
}