diff --git a/day5/p1.c b/day5/p1.c index 82918ba..d458ac8 100644 --- a/day5/p1.c +++ b/day5/p1.c @@ -6,9 +6,6 @@ #include -#define ABS(n) ((n < 0) ? n * -1 : n) - - int64_t parse_number(char *end); @@ -20,12 +17,10 @@ int main(int argc, char **argv) { if (argc == 1 || argv[1][0] == '-') input = stdin; 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; } - int64_t *data = NULL; - getline(&line, &line_buf_length, input); char *colon_pos = strchr(line, ':'); @@ -36,8 +31,7 @@ int main(int argc, char **argv) { 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)); 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) { 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; } } @@ -69,9 +63,8 @@ int main(int argc, char **argv) { 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; - } printf("%ld\n", lowest_loc_n); @@ -81,10 +74,10 @@ int main(int argc, char **argv) { int64_t parse_number(char *end) { int64_t num = 0; - for (size_t n = 1;; n *= 10) { + for (size_t m = 1;; m *= 10) { if (*end>>4 != 0x3) break; - num += (*end&0xf) * n; + num += (*end&0xf) * m; --end; }