mosredna

AoC 2021 day 12

Dec 12th, 2021 (edited)
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.86 KB | None | 0 0
  1. #include <Arduino.h>
  2.  
  3. char testData[][2][6]= {
  4. {"dc","end"},
  5. {"HN","start"},
  6. {"start","kj"},
  7. {"dc","start"},
  8. {"dc","HN"},
  9. {"LN","dc"},
  10. {"HN","end"},
  11. {"kj","sa"},
  12. {"kj","HN"},
  13. {"kj","dc"}};
  14.  
  15. char data[][2][6]= {
  16. {"end","MY"},
  17. {"MY","xc"},
  18. {"ho","NF"},
  19. {"start","ho"},
  20. {"NF","xc"},
  21. {"NF","yf"},
  22. {"end","yf"},
  23. {"xc","TP"},
  24. {"MY","qo"},
  25. {"yf","TP"},
  26. {"dc","NF"},
  27. {"dc","xc"},
  28. {"start","dc"},
  29. {"yf","MY"},
  30. {"MY","ho"},
  31. {"EM","uh"},
  32. {"xc","yf"},
  33. {"ho","dc"},
  34. {"uh","NF"},
  35. {"yf","ho"},
  36. {"end","uh"},
  37. {"start","NF"}
  38. };
  39.  
  40. typedef struct Node {
  41.   int id;
  42.   int visited = 0;
  43.   bool bigRoom = false;
  44.   int nodes[10];
  45.   int index = 0;
  46. };
  47.  
  48. typedef struct  Graph{
  49.   Node nodes[20];
  50.   char nodeDict[20][6];
  51.   int index = 0;
  52. };
  53. Graph graph;
  54.  
  55. int getNode(char* a){
  56.   for(int i = 0; i < graph.index; i++){
  57.     if(strcmp(graph.nodeDict[i], a) == 0){
  58.       return i;
  59.     }
  60.   }
  61. }
  62.  
  63. void addToGraph(char* a){
  64.   for(int i = 0; i < graph.index; i++){
  65.     if(strcmp(graph.nodeDict[i], a) == 0){
  66.       return;
  67.     }
  68.   }
  69.   graph.nodes[graph.index].id = graph.index;
  70.   if(isUpperCase(a[0]))graph.nodes[graph.index].bigRoom = true;
  71.   strcpy(graph.nodeDict[graph.index], a);
  72.   graph.index++;
  73. }
  74.  
  75. void addToNode(Node &na, Node &nb){
  76.   na.nodes[na.index]=nb.id;
  77.   na.index++;
  78.   nb.nodes[nb.index]=na.id;
  79.   nb.index++;
  80. }
  81.  
  82. void addConnection(char* a, char* b){
  83.   addToGraph(a);
  84.   addToGraph(b);
  85.   addToNode(graph.nodes[getNode(a)],graph.nodes[getNode(b)]);
  86. }
  87.  
  88. void searchpath(Node &node, long& count, bool visitedTwice)
  89. {
  90.     node.visited++;
  91.     if (node.id == 1)
  92.     {
  93.         count++;
  94.         node.visited = 0;
  95.         return;
  96.     }
  97.  
  98.     for (int index = 0; index < node.index; index++)
  99.     {
  100.         Node n = graph.nodes[node.nodes[index]];
  101.  
  102.     if (n.id == 0)
  103.             continue;
  104.  
  105.         bool visited = visitedTwice;
  106.         if (n.visited > 0 && n.bigRoom == false && visitedTwice)
  107.             continue;
  108.         else if (n.visited > 0 && n.bigRoom == false )
  109.             visited = true;
  110.      
  111.         searchpath(graph.nodes[node.nodes[index]], count,visited);
  112.     }
  113.  
  114.     node.visited--;
  115. }
  116.  
  117. void setup() {
  118.  
  119.   Serial.begin(9600);
  120.   while(!Serial);
  121.  
  122.   addToGraph("start");
  123.   addToGraph("end");
  124.   for (int i=0; i<sizeof data/sizeof data[0]; i++) {
  125.     char* s = data[i][0];
  126.     char* e = data[i][1];
  127.     addConnection(s,e);
  128.   }
  129.  
  130.   long count = 0;
  131.   long time = millis();
  132.  
  133.   searchpath(graph.nodes[0], count, true);
  134.   Serial.print("Nummber of routes part 1: ");
  135.   Serial.println(count);
  136.   Serial.print("Millisec: ");
  137.   Serial.println(millis() - time);
  138.  
  139.   for(int i = 0; i < graph.index; i++){
  140.     graph.nodes[i].visited = 0;
  141.   }
  142.  
  143.   count = 0;
  144.   time = millis();
  145.  
  146.   searchpath(graph.nodes[0], count, false);
  147.   Serial.print("Nummber of routes part 2: ");
  148.   Serial.println(count);
  149.   Serial.print("Millisec: ");
  150.   Serial.println(millis() - time);
  151. }
  152.  
  153. void loop() {
  154.   // put your main code here, to run repeatedly:
  155. }
Add Comment
Please, Sign In to add comment