Day 6 part 1 complete.

This commit is contained in:
Alexander Andreev 2023-12-06 18:56:28 +04:00
parent 69b8c48ef3
commit e3fa47e071
Signed by: Arav
GPG Key ID: D22A817D95815393
4 changed files with 129 additions and 0 deletions

View File

@ -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*

2
day6/in.my Normal file
View File

@ -0,0 +1,2 @@
Time: 44 89 96 91
Distance: 277 1136 1890 1768

2
day6/in.test Normal file
View File

@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200

121
day6/p1.c Normal file
View File

@ -0,0 +1,121 @@
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#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;
}