Advertisement
quetzelcoatlus

15b.c

Dec 15th, 2022 (edited)
1,393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define SIGN(x) (((x) < 0) ? (-1) : (1))
  5.  
  6. #define MAX_SENSORS 25
  7. #define QUESTION_CONSTANT 4000000
  8.  
  9. void main(){
  10.     int sensors[MAX_SENSORS][3], s=0, bx, by;
  11.  
  12.     while(scanf("Sensor at x=%d, y=%d: closest beacon is at x=%d, y=%d\n",&(sensors[s][0]),&(sensors[s][1]),&bx,&by) == 4){
  13.         sensors[s][2]=abs(sensors[s][0]-bx)+abs(sensors[s][1]-by);
  14.         s++;
  15.     }
  16.  
  17.     int x1,x2,y1,y2,qx,qy,good,k;
  18.     for(int i=0; i<s;i++){
  19.         for(int j=i+1;j<s;j++){
  20.             if(abs(sensors[i][0]-sensors[j][0])+abs(sensors[i][1]-sensors[j][1]) == sensors[i][2]+sensors[j][2]+2){ //"neighbours"
  21.                 x1=sensors[i][0] + SIGN(sensors[j][0]-sensors[i][0])*(sensors[i][2]+1);
  22.                 y1=sensors[i][1];
  23.                 x2=sensors[i][0];
  24.                 y2=sensors[i][1] + SIGN(sensors[j][1]-sensors[i][1])*(sensors[i][2]+1);
  25.  
  26.                 for(qx=x1,qy=y1; qx!=x2 && qy!=y2; qx+=SIGN(x2-x1),qy+=SIGN(y2-y1)){ //for every point between them
  27.                     //check if this point is out of scan range of every scanner
  28.                     for(good=1, k=0;k<s;k++) if(abs(qx-sensors[k][0])+abs(qy-sensors[k][1]) <= sensors[k][2]) good=0;
  29.                     if(good) goto exit;
  30.                 }
  31.             }
  32.         }
  33.     }
  34.  
  35. exit:
  36.     printf("%d * %d + %d = %lld\n",qx,QUESTION_CONSTANT,qy,(long long)qx*QUESTION_CONSTANT+qy);
  37. }
Tags: adventofcode
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement