A cleanup for d5p1. Also removed an ABS macro since it doesn't needed.

This commit is contained in:
Alexander Andreev 2023-12-06 04:17:21 +04:00
parent f092158510
commit 7a19ad713f
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -6,9 +6,6 @@
#include <stdbool.h> #include <stdbool.h>
#define ABS(n) ((n < 0) ? n * -1 : n)
int64_t parse_number(char *end); int64_t parse_number(char *end);
@ -20,12 +17,10 @@ int main(int argc, char **argv) {
if (argc == 1 || argv[1][0] == '-') if (argc == 1 || argv[1][0] == '-')
input = stdin; input = stdin;
else if ((input = fopen(argv[1], "r")) == NULL) { else if ((input = fopen(argv[1], "r")) == NULL) {
printf("Cannot open file %s\n", argv[1]); fprintf(stderr, "Cannot open file %s\n", argv[1]);
return -1; return -1;
} }
int64_t *data = NULL;
getline(&line, &line_buf_length, input); getline(&line, &line_buf_length, input);
char *colon_pos = strchr(line, ':'); char *colon_pos = strchr(line, ':');
@ -36,8 +31,7 @@ int main(int argc, char **argv) {
space_pos++; space_pos++;
} }
data = (int64_t *) calloc(data_len, sizeof(int64_t)); int64_t *data = (int64_t *) calloc(data_len, sizeof(int64_t));
bool *data_v = (bool *) calloc(data_len, sizeof(bool)); bool *data_v = (bool *) calloc(data_len, sizeof(bool));
for (size_t i = 0, seeds_pos = (size_t) colon_pos+2;; seeds_pos++) { for (size_t i = 0, seeds_pos = (size_t) colon_pos+2;; seeds_pos++) {
@ -59,7 +53,7 @@ int main(int argc, char **argv) {
for (size_t i = 0; i < data_len; ++i) { for (size_t i = 0; i < data_len; ++i) {
if (!data_v[i] && data[i] >= src && data[i] <= src+rng-1) { if (!data_v[i] && data[i] >= src && data[i] <= src+rng-1) {
data[i] = dst + ABS(data[i] - src); data[i] = dst + (data[i] - src);
data_v[i] = true; data_v[i] = true;
} }
} }
@ -69,9 +63,8 @@ int main(int argc, char **argv) {
int64_t lowest_loc_n = INT32_MAX; int64_t lowest_loc_n = INT32_MAX;
for (size_t i = 0; i < data_len; ++i) { for (size_t i = 0; i < data_len; ++i)
lowest_loc_n = (data[i] < lowest_loc_n) ? data[i] : lowest_loc_n; lowest_loc_n = (data[i] < lowest_loc_n) ? data[i] : lowest_loc_n;
}
printf("%ld\n", lowest_loc_n); printf("%ld\n", lowest_loc_n);
@ -81,10 +74,10 @@ int main(int argc, char **argv) {
int64_t parse_number(char *end) { int64_t parse_number(char *end) {
int64_t num = 0; int64_t num = 0;
for (size_t n = 1;; n *= 10) { for (size_t m = 1;; m *= 10) {
if (*end>>4 != 0x3) if (*end>>4 != 0x3)
break; break;
num += (*end&0xf) * n; num += (*end&0xf) * m;
--end; --end;
} }