A little cleanup in d5p2.

This commit is contained in:
Alexander Andreev 2023-12-06 04:11:43 +04:00
parent 4a80bd2b08
commit f092158510
Signed by: Arav
GPG Key ID: D22A817D95815393

View File

@ -23,13 +23,14 @@ 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;
}
uint64_t *seed_ranges = NULL;
getline(&line, &line_buf_length, input);
if (getline(&line, &line_buf_length, input) == -1) {
fprintf(stderr, "A file %s ended unexpectedly.", argv[1]);
return -1;
}
char *colon_pos = strchr(line, ':');
@ -39,8 +40,7 @@ int main(int argc, char **argv) {
space_pos++;
}
seed_ranges = (uint64_t *) calloc(seed_ranges_len, sizeof(uint64_t));
uint64_t *seed_ranges = (uint64_t *) calloc(seed_ranges_len, sizeof(uint64_t));
for (size_t i = 0, seeds_pos = (size_t) colon_pos+2;; seeds_pos++) {
while (*(char *)(seeds_pos)>>4 == 0x3) seeds_pos++;
seed_ranges[i++] = parse_number((char *)(seeds_pos-1));
@ -60,9 +60,8 @@ int main(int argc, char **argv) {
if (line[0] == '\n') {
maps_cur++;
continue;
} else if (line[0]>>4 != 0x3) {
} else if (line[0]>>4 != 0x3)
continue;
}
map_lengths[maps_cur]++;
maps[maps_cur] = (map_t *) realloc(maps[maps_cur], map_lengths[maps_cur] * sizeof(map_t));
@ -75,29 +74,23 @@ int main(int argc, char **argv) {
memset(line, 0, line_buf_length);
}
maps_cur = 0;
uint64_t lowest_loc_n = UINT64_MAX;
for (size_t i = 0; i < seed_ranges_len; i += 2) {
for (size_t i = 0; i < seed_ranges_len; i += 2)
#pragma omp parallel for shared(lowest_loc_n)
for (uint64_t r = seed_ranges[i]; r < seed_ranges[i] + seed_ranges[i+1]; ++r) {
uint64_t seed = r;
for (size_t j = 0; j < MAPS_LEN; ++j) {
for (size_t n = 0; n < map_lengths[j]; ++n) {
if (seed >= maps[j][n].src && seed <= maps[j][n].src + maps[j][n].rng) {
seed = maps[j][n].dst + (seed - maps[j][n].src);
for (size_t j = 0; j < MAPS_LEN; ++j)
for (size_t k = 0; k < map_lengths[j]; ++k)
if (seed >= maps[j][k].src && seed <= maps[j][k].src + maps[j][k].rng) {
seed = maps[j][k].dst + (seed - maps[j][k].src);
break;
}
}
}
if (seed < lowest_loc_n)
lowest_loc_n = seed;
}
}
free(seed_ranges);
@ -112,10 +105,10 @@ int main(int argc, char **argv) {
uint64_t parse_number(char *end) {
uint64_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;
}