aoc2024/d25/part1_mm.c

45 lines
1.3 KiB
C
Raw Permalink Normal View History

2024-12-25 23:23:30 +00:00
#include <stdio.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <time.h>
int main() {
// from stdin
//char I[21500]; // 500 * 43
//fread(I, sizeof(char), sizeof(I), stdin);
// mmap input
struct stat sb;
int fd = open("input", O_RDONLY);
fstat(fd, &sb);
char *I = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
// timer
struct timespec start, end;
clock_gettime(CLOCK_MONOTONIC, &start);
int S = 0;
for (int i = 0; i < 500; i++) {
if (I[43 * i] == '#') {
for (int j = 0; j < 500; j++) {
if (I[43 * j] == '.') {
int skip = 0;
for (int k = 6; k < 37; k++) {
if (I[43 * i + k] == '#' && I[43 * j + k] == '#') {
skip = 1;
break;
}
}
S += !skip; // Increment S only if skip is false
}
}
}
}
// stop timer
clock_gettime(CLOCK_MONOTONIC, &end);
double time_taken = ((end.tv_sec - start.tv_sec) * 1e9 + (end.tv_nsec - start.tv_nsec)) * 1e-9;
printf("%f\n", time_taken);
printf("%d\n", S);
return 0;
}
// 2ms ; read input is 0.5ms ; process is 1.5ms