1
0
Fork 0
aoc2022/08/solution.c

64 lines
1.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
int main(int argc, char **argv) {
if (argc < 2) return 1;
struct stat s;
if (stat(argv[1], &s) == -1) return 1;
int fd = open(argv[1], O_RDONLY);
if (fd == -1) return 1;
char *in = mmap(NULL, s.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (in == MAP_FAILED) return 1;
size_t w = strchr(in, '\n') - in;
size_t stride = w + 1;
size_t h = s.st_size / (w+1);
size_t visible = w * 2 + (h - 2) * 2;
size_t maxscore = 0;
#pragma omp parallel for reduction(+:visible) reduction(max:maxscore) schedule(runtime)
for (size_t y = 1; y < h - 1; y++) {
for (size_t x = 1; x < w - 1; x++) {
size_t i, score = 0;
char val = in[stride * y + x];
int vis = 0;
for (i = 1; i <= y; i++)
if (val <= in[stride * (y - i) + x])
break;
vis += i == y + 1;
score = i - (i == y + 1);
for (i = y + 1; i < h; i++)
if (val <= in[stride * i + x])
break;
vis += i == h;
score *= i - y - (i == h);
for (i = 1; i <= x; i++)
if (val <= in[stride * y + x - i])
break;
vis += i == x + 1;
score *= i - (i == x + 1);
for (i = x + 1; i < w; i++)
if (val <= in[stride * y + i])
break;
score *= i - x - (i == w);
if (score > maxscore)
maxscore = score;
visible += vis || i == w;
}
}
printf("Silver: %lu\nGold: %lu\n", visible, maxscore);
}