A little cleanup in d5p2.
This commit is contained in:
parent
4a80bd2b08
commit
f092158510
35
day5/p2.c
35
day5/p2.c
@ -23,13 +23,14 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t *seed_ranges = NULL;
|
if (getline(&line, &line_buf_length, input) == -1) {
|
||||||
|
fprintf(stderr, "A file %s ended unexpectedly.", argv[1]);
|
||||||
getline(&line, &line_buf_length, input);
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
char *colon_pos = strchr(line, ':');
|
char *colon_pos = strchr(line, ':');
|
||||||
|
|
||||||
@ -39,8 +40,7 @@ int main(int argc, char **argv) {
|
|||||||
space_pos++;
|
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++) {
|
for (size_t i = 0, seeds_pos = (size_t) colon_pos+2;; seeds_pos++) {
|
||||||
while (*(char *)(seeds_pos)>>4 == 0x3) seeds_pos++;
|
while (*(char *)(seeds_pos)>>4 == 0x3) seeds_pos++;
|
||||||
seed_ranges[i++] = parse_number((char *)(seeds_pos-1));
|
seed_ranges[i++] = parse_number((char *)(seeds_pos-1));
|
||||||
@ -60,9 +60,8 @@ int main(int argc, char **argv) {
|
|||||||
if (line[0] == '\n') {
|
if (line[0] == '\n') {
|
||||||
maps_cur++;
|
maps_cur++;
|
||||||
continue;
|
continue;
|
||||||
} else if (line[0]>>4 != 0x3) {
|
} else if (line[0]>>4 != 0x3)
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
|
|
||||||
map_lengths[maps_cur]++;
|
map_lengths[maps_cur]++;
|
||||||
maps[maps_cur] = (map_t *) realloc(maps[maps_cur], map_lengths[maps_cur] * sizeof(map_t));
|
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);
|
memset(line, 0, line_buf_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
maps_cur = 0;
|
|
||||||
|
|
||||||
uint64_t lowest_loc_n = UINT64_MAX;
|
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)
|
#pragma omp parallel for shared(lowest_loc_n)
|
||||||
for (uint64_t r = seed_ranges[i]; r < seed_ranges[i] + seed_ranges[i+1]; ++r) {
|
for (uint64_t r = seed_ranges[i]; r < seed_ranges[i] + seed_ranges[i+1]; ++r) {
|
||||||
uint64_t seed = r;
|
uint64_t seed = r;
|
||||||
|
|
||||||
for (size_t j = 0; j < MAPS_LEN; ++j) {
|
for (size_t j = 0; j < MAPS_LEN; ++j)
|
||||||
for (size_t n = 0; n < map_lengths[j]; ++n) {
|
for (size_t k = 0; k < map_lengths[j]; ++k)
|
||||||
if (seed >= maps[j][n].src && seed <= maps[j][n].src + maps[j][n].rng) {
|
if (seed >= maps[j][k].src && seed <= maps[j][k].src + maps[j][k].rng) {
|
||||||
seed = maps[j][n].dst + (seed - maps[j][n].src);
|
seed = maps[j][k].dst + (seed - maps[j][k].src);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (seed < lowest_loc_n)
|
if (seed < lowest_loc_n)
|
||||||
lowest_loc_n = seed;
|
lowest_loc_n = seed;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
free(seed_ranges);
|
free(seed_ranges);
|
||||||
|
|
||||||
@ -112,10 +105,10 @@ int main(int argc, char **argv) {
|
|||||||
uint64_t parse_number(char *end) {
|
uint64_t parse_number(char *end) {
|
||||||
uint64_t num = 0;
|
uint64_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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user