mosredna

AoC 2021 day 15 part 1

Dec 15th, 2021 (edited)
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.59 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include "data.h"
  3. const int gridSize = 100;
  4. const int CLOSED_SIZE = 2000;
  5. const int OPEN_SIZE = 150;
  6.  
  7. /*
  8.   data looks like this:
  9.   const uint8_t data[] PROGMEM ={7,9,7,2,9,8,1,6,8,8,7,9,1,3,1,7,7,...};
  10. */
  11.  
  12. struct Node {
  13.   byte x=0;
  14.   byte y=0;
  15.  
  16.   int f = 0; // g+h;
  17.   int g = 9999; // cost so far
  18.   byte h = 0; // distance to goal
  19.  
  20.   bool inOpen = false;
  21. };
  22.  
  23. struct Graph{
  24.   Node nodes[OPEN_SIZE];
  25.   int index = 0;
  26.   int closed[CLOSED_SIZE] = {0};
  27.   int indexClosed = 0;
  28.  
  29. };
  30.  
  31. Graph graph;
  32. int neighbors[4][2] = {{-1,0},{1,0},{0,-1},{0,1}};
  33.  
  34. int endX = gridSize-1;
  35. int endY = gridSize-1;
  36. void unlockNode(int x, int y){
  37.  
  38.   graph.nodes[graph.index].x = x;
  39.   graph.nodes[graph.index].y = y;
  40.  
  41.  
  42.   graph.nodes[graph.index].h =  (endX - x) + (endY - y);
  43.   graph.index++;
  44. }
  45.  
  46. bool isClosed(int x, int y){
  47.  
  48.   for (int i = 0; i < CLOSED_SIZE; i++){
  49.     if( graph.closed[i] == y*gridSize+x){
  50.       return true;
  51.     }
  52.   }
  53.   return false;
  54. }
  55.  
  56. void removeNode(int x, int y){
  57.   bool found = false;
  58.   for (int i = 0; i < graph.index; i++)
  59.   {
  60.     if(graph.nodes[i].x == x && graph.nodes[i].y == y)
  61.       found = true;
  62.    
  63.     if(found){
  64.         memcpy( &graph.nodes[i], &graph.nodes[i+1], sizeof graph.nodes[i] );
  65.     }
  66.   }
  67.   graph.index--;
  68.   graph.closed[graph.indexClosed] = y*gridSize+x;
  69.   graph.indexClosed++;
  70.   if(graph.indexClosed > CLOSED_SIZE)
  71.     graph.indexClosed = 0;
  72.  
  73. }
  74.  
  75. Node* getNode(int x, int y);
  76. Node* getNode(int x, int y){
  77.   for (int i = 0; i < graph.index; i++)
  78.   {
  79.     if(graph.nodes[i].x == x && graph.nodes[i].y == y)
  80.       return &graph.nodes[i];
  81.   }
  82.   unlockNode(x,y);
  83.   return &graph.nodes[graph.index-1];
  84. }
  85.  
  86. Node* getLowestCostNode();
  87. Node* getLowestCostNode(){
  88.   int lowest = INFINITY;
  89.   Node* n;
  90.   for (int i = 0; i < graph.index; i++)
  91.   {
  92.     if(graph.nodes[i].f<lowest){
  93.       lowest = graph.nodes[i].f;
  94.       n = &graph.nodes[i];
  95.     }
  96.      
  97.   }
  98.   return n;
  99. }
  100.  
  101. void setup() {
  102.  
  103.   Serial.begin(9600);
  104.   while(!Serial);
  105.  
  106.   long t=millis(); // get current time
  107.  
  108.   Node* n = getNode(0,0);
  109.   n->inOpen = true;
  110.   n->g = 0;
  111.   bool destination = false;
  112.   while (destination == false){
  113.  
  114.     Node* n = getLowestCostNode();
  115.     if (n->x == endX && n->y == endY){
  116.       destination = true;
  117.       Serial.println("DONE!!!!");
  118.       Serial.println(n->g);
  119.     }
  120.     else{
  121.  
  122.       n->inOpen = false;
  123.  
  124.       for (auto edge : neighbors){
  125.  
  126.         // int f = g+h;
  127.         // int g = cost so far
  128.         // int h = distance to goal
  129.         int x = n->x+edge[0];
  130.         int y = n->y+edge[1];
  131.         if(x<0 || x>gridSize-1 || y<0 || y>gridSize-1)
  132.           continue;
  133.         if(isClosed(x,y))
  134.           continue;
  135.        
  136.         if(x>gridSize/2 && y<x-gridSize/2)
  137.           continue;
  138.  
  139.         if(y>gridSize/2 && x<y-gridSize/2)
  140.           continue;
  141.          
  142.         Node* n2 = getNode(x,y);
  143.  
  144.         int g = n->g + pgm_read_byte( data + (y*gridSize + x) );
  145.  
  146.         if (n2->g > g && n2->inOpen){
  147.           n2->g = g;
  148.           n2->f = g+n2->h;
  149.         }else if( n2->inOpen == false){
  150.           n2->g = g;
  151.           n2->f = g+n2->h;
  152.           n2->inOpen = true;
  153.         }
  154.       }
  155.     }
  156.     if(graph.index>OPEN_SIZE){
  157.       destination = true;
  158.       Serial.print(n->x);
  159.       Serial.print("--");
  160.       Serial.println(n->y);
  161.       Serial.println("-----graph out of bounds--------");
  162.     }
  163.     removeNode(n->x, n->y);
  164.   }
  165.  
  166.   Serial.println("----------------------");
  167.   Serial.println(graph.index);
  168.   Serial.println(millis()-t);
  169. }
  170.  
  171. void loop() {
  172.   // put your main code here, to run repeatedly:
  173. }
Advertisement
Add Comment
Please, Sign In to add comment