A little code refactoring for day 6.
This commit is contained in:
parent
2ae96c9a04
commit
443576621d
13
day6/p1.c
13
day6/p1.c
@ -3,7 +3,6 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
|
|
||||||
#define ISDIGIT(c) (c >= 0x30 && c <= 0x39)
|
#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;
|
ssize_t times_length = 2, distances_length = 2;
|
||||||
|
|
||||||
for (ssize_t i = time_line_length-2, j = 0;;) {
|
for (ssize_t i = time_line_length-2, j = 0;;) {
|
||||||
while (!ISDIGIT(time_line[i])) {
|
while (!ISDIGIT(time_line[i]))
|
||||||
if (time_line[--i] == ':')
|
if (time_line[--i] == ':')
|
||||||
goto tl_end;
|
goto tl_end;
|
||||||
}
|
|
||||||
|
|
||||||
if (j == times_length)
|
if (j == times_length)
|
||||||
times = (uint64_t *) realloc(times, ++times_length * sizeof(uint64_t));
|
times = (uint64_t *) realloc(times, ++times_length * sizeof(uint64_t));
|
||||||
@ -56,10 +54,9 @@ int main(int argc, char **argv) {
|
|||||||
tl_end:
|
tl_end:
|
||||||
|
|
||||||
for (ssize_t i = distance_line_length-2, j = 0;;) {
|
for (ssize_t i = distance_line_length-2, j = 0;;) {
|
||||||
while (!ISDIGIT(distance_line[i])) {
|
while (!ISDIGIT(distance_line[i]))
|
||||||
if (distance_line[--i] == ':')
|
if (distance_line[--i] == ':')
|
||||||
goto dl_end;
|
goto dl_end;
|
||||||
}
|
|
||||||
|
|
||||||
if (j == distances_length)
|
if (j == distances_length)
|
||||||
distances = (uint64_t *) realloc(distances, ++distances_length * sizeof(uint64_t));
|
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 tmid = times[i] / 2;
|
||||||
uint64_t ways = 0;
|
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])
|
if (ht * (times[i] - ht) > distances[i])
|
||||||
++ways;
|
++ways;
|
||||||
else
|
else
|
||||||
break;
|
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])
|
if (ht * (times[i] - ht) > distances[i])
|
||||||
++ways;
|
++ways;
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
total_ways *= ways;
|
total_ways *= ways;
|
||||||
}
|
}
|
||||||
|
14
day6/p2.c
14
day6/p2.c
@ -3,11 +3,11 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdbool.h>
|
|
||||||
|
|
||||||
|
|
||||||
#define ISDIGIT(c) (c >= 0x30 && c <= 0x39)
|
#define ISDIGIT(c) (c >= 0x30 && c <= 0x39)
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
FILE *input;
|
FILE *input;
|
||||||
size_t line_buf_length = 0;
|
size_t line_buf_length = 0;
|
||||||
@ -35,10 +35,9 @@ int main(int argc, char **argv) {
|
|||||||
uint64_t time = 0, distance = 0;
|
uint64_t time = 0, distance = 0;
|
||||||
|
|
||||||
for (ssize_t i = time_line_length-2, m = 1;; --i) {
|
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] == ':')
|
if (time_line[--i] == ':')
|
||||||
goto tl_end;
|
goto tl_end;
|
||||||
}
|
|
||||||
|
|
||||||
while (ISDIGIT(time_line[i])) {
|
while (ISDIGIT(time_line[i])) {
|
||||||
time += (time_line[i--]&0xf) * m;
|
time += (time_line[i--]&0xf) * m;
|
||||||
@ -48,10 +47,9 @@ int main(int argc, char **argv) {
|
|||||||
tl_end:
|
tl_end:
|
||||||
|
|
||||||
for (ssize_t i = distance_line_length-2, m = 1;; --i) {
|
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] == ':')
|
if (distance_line[--i] == ':')
|
||||||
goto dl_end;
|
goto dl_end;
|
||||||
}
|
|
||||||
|
|
||||||
while (ISDIGIT(distance_line[i])) {
|
while (ISDIGIT(distance_line[i])) {
|
||||||
distance += (distance_line[i--]&0xf) * m;
|
distance += (distance_line[i--]&0xf) * m;
|
||||||
@ -64,19 +62,17 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
uint64_t tmid = time / 2;
|
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)
|
if (ht * (time - ht) > distance)
|
||||||
++total_ways;
|
++total_ways;
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
for (uint64_t ht = tmid-1; ht != 0; --ht) {
|
for (uint64_t ht = tmid-1; ht != 0; --ht)
|
||||||
if (ht * (time - ht) > distance)
|
if (ht * (time - ht) > distance)
|
||||||
++total_ways;
|
++total_ways;
|
||||||
else
|
else
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
printf("%lu\n", total_ways);
|
printf("%lu\n", total_ways);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user