day 25, trivial, made some benchmarks

This commit is contained in:
setop 2024-12-26 00:23:30 +01:00
parent 679d4788d4
commit 124508cc31
3 changed files with 74 additions and 0 deletions

20
d25/part1_imp.py Normal file
View File

@ -0,0 +1,20 @@
S = 0
I = open("input").read()
th=2
@par(num_threads=th,chunk_size=500//th)
for i in range(500):
if I[43*i] == '#':
#@par # much slower
for j in range(500):
if I[43*j] == '.':
for k in range(6,37):
if I[43*i+k]=='#' and I[43*j+k]=='#':
break
else:
S += 1
print(S)
# 130ms with cpython
# with codon
# par 1 th: 6ms
# par 2 th: 4ms
# more // is slower

45
d25/part1_mm.c Normal file
View File

@ -0,0 +1,45 @@
#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

9
d25/part1_set.py Normal file
View File

@ -0,0 +1,9 @@
A = {
frozenset({i for i, c in enumerate(B) if c == "#"})
for B in open("input").read().strip().split("\n\n")
}
L = {a for a in A if 1 in a}
U = A - L
print(sum(not l & u for l in L for u in U))
# 40ms with cpython
# 30ms with codon