Advertisement
Guest User

AoC 2019 Day 3

a guest
Dec 3rd, 2019
442
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.06 KB | None | 0 0
  1. #include <limits.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. #define BUFSIZE 4096
  7.  
  8. int manhattan(int fstX, int sndX, int fstY, int sndY) {
  9.     return abs(fstX - sndX) + abs(fstY - sndY);
  10. }
  11.  
  12. int traceCables(int lenFst, char *fstWire, const int lenSnd, char *sndWire) {
  13.     int side         = 30000;
  14.     int *wireBox     = calloc(side * side, sizeof(int));
  15.     int *breadCrumbs = calloc(side * side, sizeof(int));
  16.     int shortest     = INT_MAX;
  17.  
  18.     int currentX = side / 2, currentY = side / 2;
  19.  
  20.     char *fstToken = strtok(fstWire, ","), *end;
  21.  
  22.     int stepCountX = 0, stepCountY = 0;
  23.  
  24.     while (fstToken != NULL) {
  25.         switch (fstToken++ [0]) {
  26.         case 'U':
  27.             for (int i = 0; i < strtol(fstToken, &end, 10); ++i) {
  28.                 stepCountY++;
  29.  
  30.                 if (!wireBox[currentX + side * --currentY])
  31.                     wireBox[currentX + side * currentY] =
  32.                         stepCountX + stepCountY;
  33.             }
  34.             break;
  35.         case 'R':
  36.             for (int i = 0; i < strtol(fstToken, &end, 10); ++i) {
  37.                 stepCountX++;
  38.  
  39.                 if (!wireBox[++currentX + side * currentY])
  40.                     wireBox[currentX + side * currentY] =
  41.                         stepCountX + stepCountY;
  42.             }
  43.             break;
  44.         case 'D':
  45.             for (int i = 0; i < strtol(fstToken, &end, 10); ++i) {
  46.                 stepCountY++;
  47.  
  48.                 if (!wireBox[currentX + side * ++currentY])
  49.                     wireBox[currentX + side * currentY] =
  50.                         stepCountX + stepCountY;
  51.             }
  52.             break;
  53.         case 'L':
  54.             for (int i = 0; i < strtol(fstToken, &end, 10); ++i) {
  55.                 stepCountX++;
  56.  
  57.                 if (!wireBox[--currentX + side * currentY])
  58.                     wireBox[currentX + side * currentY] =
  59.                         stepCountX + stepCountY;
  60.             }
  61.             break;
  62.         }
  63.  
  64.         fstToken = strtok(NULL, ",");
  65.     }
  66.  
  67.     int x, y;
  68.  
  69.     currentX = side / 2, currentY = side / 2;
  70.  
  71.     char *sndToken = strtok(sndWire, ",");
  72.  
  73.     stepCountX = 0, stepCountY = 0;
  74.  
  75.     while (sndToken != NULL) {
  76.         switch (sndToken++ [0]) {
  77.         case 'U':
  78.             for (int i = 0; i < strtol(sndToken, &end, 10); ++i) {
  79.                 stepCountY++;
  80.  
  81.                 if (wireBox[currentX + side * --currentY]) {
  82.                     breadCrumbs[stepCountX + stepCountY +
  83.                                 wireBox[currentX + side * currentY]]++;
  84.  
  85.                     int current =
  86.                         manhattan(side / 2, currentX, side / 2, currentY);
  87.  
  88.                     shortest = (current < shortest) ? current : shortest;
  89.                 }
  90.             }
  91.             break;
  92.         case 'R':
  93.             for (int i = 0; i < strtol(sndToken, &end, 10); ++i) {
  94.                 stepCountX++;
  95.  
  96.                 if (wireBox[++currentX + side * currentY]) {
  97.                     breadCrumbs[stepCountX + stepCountY +
  98.                                 wireBox[currentX + side * currentY]]++;
  99.  
  100.                     int current =
  101.                         manhattan(side / 2, currentX, side / 2, currentY);
  102.  
  103.                     shortest = (current < shortest) ? current : shortest;
  104.                 }
  105.             }
  106.             break;
  107.         case 'D':
  108.             for (int i = 0; i < strtol(sndToken, &end, 10); ++i) {
  109.                 stepCountY++;
  110.  
  111.                 if (wireBox[currentX + side * ++currentY]) {
  112.                     breadCrumbs[stepCountX + stepCountY +
  113.                                 wireBox[currentX + side * currentY]]++;
  114.  
  115.                     int current =
  116.                         manhattan(side / 2, currentX, side / 2, currentY);
  117.  
  118.                     shortest = (current < shortest) ? current : shortest;
  119.                 }
  120.             }
  121.             break;
  122.         case 'L':
  123.             for (int i = 0; i < strtol(sndToken, &end, 10); ++i) {
  124.                 stepCountX++;
  125.  
  126.                 if (wireBox[--currentX + side * currentY]) {
  127.                     breadCrumbs[stepCountX + stepCountY +
  128.                                 wireBox[currentX + side * currentY]]++;
  129.  
  130.                     int current =
  131.                         manhattan(side / 2, currentX, side / 2, currentY);
  132.  
  133.                     shortest = (current < shortest) ? current : shortest;
  134.                 }
  135.             }
  136.             break;
  137.         }
  138.  
  139.         sndToken = strtok(NULL, ",");
  140.     }
  141.  
  142.     for (y = 0; y < side; y++)
  143.         for (x = 0; x < side; x++)
  144.             if (breadCrumbs[x + side * y])
  145.                 return x + side * y;
  146.  
  147.     return shortest;
  148. }
  149.  
  150. int main() {
  151.     FILE *file = fopen("input.txt", "r");
  152.     char buffer[BUFSIZE];
  153.  
  154.     char fstWire[BUFSIZE], sndWire[BUFSIZE];
  155.  
  156.     fgets(buffer, BUFSIZE, file);
  157.  
  158.     strcpy(fstWire, buffer);
  159.  
  160.     fgets(buffer, BUFSIZE, file);
  161.  
  162.     strcpy(sndWire, buffer);
  163.  
  164.     int shortestDistance =
  165.         traceCables(strlen(fstWire), fstWire, strlen(sndWire), sndWire);
  166.  
  167.     printf("%d\n", shortestDistance);
  168.  
  169.     return 0;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement