A little code refactoring for day 6.

This commit is contained in:
Alexander Andreev 2023-12-07 00:20:35 +04:00
parent 2ae96c9a04
commit 443576621d
Signed by: Arav
GPG Key ID: D22A817D95815393
2 changed files with 9 additions and 18 deletions

View File

@ -3,7 +3,6 @@
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#define ISDIGIT(c) (c >= 0x30 && c <= 0x39)
@ -41,10 +40,9 @@ int main(int argc, char **argv) {
ssize_t times_length = 2, distances_length = 2;
for (ssize_t i = time_line_length-2, j = 0;;) {
while (!ISDIGIT(time_line[i])) {
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));
@ -56,10 +54,9 @@ int main(int argc, char **argv) {
tl_end:
for (ssize_t i = distance_line_length-2, j = 0;;) {
while (!ISDIGIT(distance_line[i])) {
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));
@ -76,19 +73,17 @@ int main(int argc, char **argv) {
uint64_t tmid = times[i] / 2;
uint64_t ways = 0;
for (uint64_t ht = tmid; ht <= times[i]; ++ht) {
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) {
for (uint64_t ht = tmid-1; ht != 0; --ht)
if (ht * (times[i] - ht) > distances[i])
++ways;
else
break;
}
total_ways *= ways;
}

View File

@ -3,11 +3,11 @@
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#define ISDIGIT(c) (c >= 0x30 && c <= 0x39)
int main(int argc, char **argv) {
FILE *input;
size_t line_buf_length = 0;
@ -35,10 +35,9 @@ int main(int argc, char **argv) {
uint64_t time = 0, distance = 0;
for (ssize_t i = time_line_length-2, m = 1;; --i) {
while (!ISDIGIT(time_line[i])) {
while (!ISDIGIT(time_line[i]))
if (time_line[--i] == ':')
goto tl_end;
}
while (ISDIGIT(time_line[i])) {
time += (time_line[i--]&0xf) * m;
@ -48,10 +47,9 @@ int main(int argc, char **argv) {
tl_end:
for (ssize_t i = distance_line_length-2, m = 1;; --i) {
while (!ISDIGIT(distance_line[i])) {
while (!ISDIGIT(distance_line[i]))
if (distance_line[--i] == ':')
goto dl_end;
}
while (ISDIGIT(distance_line[i])) {
distance += (distance_line[i--]&0xf) * m;
@ -64,19 +62,17 @@ int main(int argc, char **argv) {
uint64_t tmid = time / 2;
for (uint64_t ht = tmid; ht <= time; ++ht) {
for (uint64_t ht = tmid; ht <= time; ++ht)
if (ht * (time - ht) > distance)
++total_ways;
else
break;
}
for (uint64_t ht = tmid-1; ht != 0; --ht) {
for (uint64_t ht = tmid-1; ht != 0; --ht)
if (ht * (time - ht) > distance)
++total_ways;
else
break;
}
printf("%lu\n", total_ways);