Advertisement
Guest User

magic5x5.c

a guest
Dec 30th, 2018
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4.  
  5. /*
  6. IPQkm
  7. XhUjv
  8. SogLr
  9. YNtfW
  10. abcdE
  11. */
  12.  
  13. #define THREADS 6
  14.  
  15. #define CONDITION_START(a, A, B, C, D)   a = 65 - A - B - C - D; if (used[a] == 0) { used[a] = 1;
  16. #define LOOP_START(a)                    for (a = 1; a <= 25; a++) { if (used[a]) continue; used[a] = 1;
  17. #define END(a)                           used[a] = 0; }
  18.  
  19. int compute(int startValue) {
  20.     int usedArray[200];
  21.     int *used = usedArray + 100;
  22.     for (int i = -100; i < 100; i++)
  23.         used[i] = (i >= 1 && i <= 25) ? 0 : 1;
  24.     int a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y;
  25.     a=b=c=d=e=f=g=h=i=j=k=l=m=n=o=p=q=r=s=t=u=v=w=x=y=0;
  26.     int count = 0;
  27.    
  28.     LOOP_START(a)
  29.     for (b = startValue; b <= 25; b += THREADS) { if (used[b]) continue; used[b] = 1;
  30.     LOOP_START(c)
  31.     LOOP_START(d)
  32.     CONDITION_START(e, a, b, c, d)
  33.    
  34.     if (e < a) {
  35.         used[e] = 0;
  36.         used[d] = 0;
  37.         continue;
  38.     }
  39.    
  40.     LOOP_START(f)
  41.     LOOP_START(g)
  42.     LOOP_START(h)
  43.     CONDITION_START(i, h, g, f, e)
  44.    
  45.     if (i < a || i < e) {
  46.         used[i] = 0;
  47.         used[h] = 0;
  48.         continue;
  49.     }
  50.    
  51.     LOOP_START(j)
  52.     LOOP_START(k)
  53.     CONDITION_START(l, k, j, f, d)
  54.     LOOP_START(m)
  55.    
  56.     if (m < a) {
  57.         used[m] = 0;
  58.         continue;
  59.     }
  60.    
  61.     CONDITION_START(n, a, g, j, m)
  62.     LOOP_START(o)
  63.     CONDITION_START(p, h, o, n, b)
  64.     CONDITION_START(q, i, p, k, m)
  65.     LOOP_START(r)
  66.     CONDITION_START(s, o, g, l, r)
  67.     LOOP_START(t)
  68.     CONDITION_START(u, q, g, t, c)
  69.     LOOP_START(v)
  70.     CONDITION_START(w, m, v, r, e)
  71.     CONDITION_START(x, h, u, j, v)
  72.     y = 65 - i - x - s - a;
  73.    
  74.     count++;
  75.    
  76.     END(x)
  77.     END(w)
  78.     END(v)
  79.     END(u)
  80.     END(t)
  81.     END(s)
  82.     END(r)
  83.     END(q)
  84.     END(p)
  85.     END(o)
  86.     END(n)
  87.     END(m)
  88.     END(l)
  89.     END(k)
  90.     END(j)
  91.     END(i)
  92.     END(h)
  93.     END(g)
  94.     END(f)
  95.     END(e)
  96.     END(d)
  97.     END(c)
  98.     END(b)
  99.     END(a)
  100.    
  101.     count *= 8;
  102.     return count;
  103. }
  104.  
  105. void *threadFunc(void *argument) {
  106.     int threadId = *((int*)argument);
  107.     int *result = calloc(1, sizeof(int));
  108.     *result = compute(threadId + 1);
  109.     return result;
  110. }
  111.  
  112. int main() {
  113.     int arguments[THREADS];
  114.     pthread_t threads[THREADS];
  115.     int *results[THREADS];
  116.     for (int i = 0; i < THREADS; i++)
  117.         arguments[i] = i;
  118.     for (int i = 0; i < THREADS; i++)
  119.         pthread_create(&threads[i], NULL, threadFunc, &arguments[i]);
  120.     for (int i = 0; i < THREADS; i++)
  121.         pthread_join(threads[i], (void **)&results[i]);
  122.    
  123.     int count = 0;
  124.     for (int i = 0; i < THREADS; i++) {
  125.         count += *results[i];
  126.         free(results[i]);
  127.     }
  128.     printf("%u\n", (unsigned)count);
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement