Advertisement
berninator

AOC Day 15 OpenCL

Dec 21st, 2022
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.01 KB | None | 0 0
  1. __kernel void can_contain_new_beacon(
  2.     const long base_x,
  3.     const uint num_of_sensors,
  4.     __constant long *sensor_x,
  5.     __constant long *sensor_y,
  6.     __constant long *beacon_distance,
  7.     __global long *beacon_location)
  8. {
  9.     long x = get_global_id(0) + base_x;
  10.     long y = get_global_id(1);
  11.  
  12.     // Check if this position is outside the range of ALL sensors
  13.     bool can_exist = true;
  14.     for (size_t i = 0; i < num_of_sensors; ++i) {
  15.         long x_distance = abs_diff(x, sensor_x[i]);
  16.         long y_distance = abs_diff(y, sensor_y[i]);
  17.         long total_distance = x_distance + y_distance;
  18.        
  19.         // Check fails if the position is within range of ANY sensor
  20.         if (total_distance <= beacon_distance[i]) {
  21.             can_exist = false;
  22.             break;
  23.         }
  24.     }
  25.    
  26.     // If this passes all test cases, return the position
  27.     // Assumes there is only ONE possible solution
  28.     if (can_exist) {
  29.         beacon_location[0] = x;
  30.         beacon_location[1] = y;
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement