Advertisement
JohnGabrielUK

AoC2022 Day 3 C solution

Dec 3rd, 2022
1,095
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.80 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define MAX_LEN 64
  6.  
  7. char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  8.  
  9. int readLine(char* buffer, FILE* fp) {
  10.     int result = fgets(buffer, MAX_LEN, fp);
  11.     if (result == 0) return 0;
  12.     buffer[strcspn(buffer, "\n")] = 0;
  13.     return result;
  14. }
  15.  
  16. char getSharedChar(char* line) {
  17.     int i, j;
  18.     int lineLength = strlen(line);
  19.     for (i = 0; i < lineLength / 2; i++) {
  20.         for (j = lineLength / 2; j < lineLength; j++) {
  21.             if (line[i] == line[j]) {
  22.                 return line[i];
  23.             }
  24.         }  
  25.     }
  26.     // It's hecked, lads
  27.     return '';
  28. }
  29.  
  30. char getBadge(char* lineA, char* lineB, char* lineC) {
  31.     int i, j, k;
  32.     int lineALength = strlen(lineA);
  33.     int lineBLength = strlen(lineB);
  34.     int lineCLength = strlen(lineC);
  35.     for (i = 0; i < lineALength; i++) {
  36.         for (j = 0; j < lineBLength; j++) {
  37.             for (k = 0; k < lineCLength; k++) {
  38.                 if (lineA[i] == lineB[j] && lineB[j] == lineC[k]) {
  39.                     return lineA[i];
  40.                 }
  41.             }
  42.         }  
  43.     }
  44.     // It's hecked, lads
  45.     return '';
  46. }
  47.  
  48. int getScoreForChar(char c) {
  49.     int i;
  50.     int l = strlen(chars);
  51.     for (i = 0; i < l; i++) {
  52.         if (chars[i] == c) {
  53.             return i+1;
  54.         }
  55.     }
  56.     return -1;
  57. }
  58.  
  59. void doStuff(FILE* fp) {
  60.     char bufferA[MAX_LEN];
  61.     char bufferB[MAX_LEN];
  62.     char bufferC[MAX_LEN];
  63.     char bufferASharedChar;
  64.     char bufferBSharedChar;
  65.     char bufferCSharedChar;
  66.     char badge;
  67.     int cursorAX = 14;
  68.     int cursorAY = 5;
  69.     int cursorBX = 14;
  70.     int cursorBY = 11;
  71.  
  72.     int partOne = 0;
  73.     int partTwo = 0;
  74.  
  75.     printf("Items:\n\n\n\n\n\nBadges:\n\n\nPart one:\nPart two:");
  76.  
  77.     while (readLine(bufferA, fp)) {
  78.         readLine(bufferB, fp);
  79.         readLine(bufferC, fp);
  80.  
  81.         bufferASharedChar = getSharedChar(bufferA);
  82.         bufferBSharedChar = getSharedChar(bufferB);
  83.         bufferCSharedChar = getSharedChar(bufferC);
  84.         badge = getBadge(bufferA, bufferB, bufferC);
  85.  
  86.         partOne += getScoreForChar(bufferASharedChar);
  87.         partOne += getScoreForChar(bufferBSharedChar);
  88.         partOne += getScoreForChar(bufferCSharedChar);
  89.         partTwo += getScoreForChar(badge);
  90.  
  91.         gotoxy(cursorAX, cursorAY);
  92.         printf("%c%c%c", bufferASharedChar, bufferBSharedChar, bufferCSharedChar);
  93.         cursorAX += 3;
  94.         if (cursorAX > 74) {
  95.             cursorAX = 14;
  96.             cursorAY += 1;
  97.         }
  98.        
  99.         gotoxy(cursorBX, cursorBY);
  100.         printf("%c", badge);
  101.         cursorBX += 1;
  102.         if (cursorBX > 74) {
  103.             cursorBX = 14;
  104.             cursorBY += 1;
  105.         }
  106.  
  107.         gotoxy(14, 14);
  108.         printf("%d", partOne);
  109.         gotoxy(14, 15);
  110.         printf("%d", partTwo);
  111.     }
  112.  
  113.     gotoxy(1, 16);
  114.     printf("\n");
  115. }
  116.  
  117. int main(int argc, char **argv) {
  118.     FILE* fp;
  119.  
  120.     clrscr();
  121.     printf("\n ADVENT OF CODE 2022 / DAY 03\n u/JohnGabrielUK\n\n");
  122.  
  123.     if (argc != 2) {
  124.         printf("Usage: day3.exe [input file]\n");
  125.     }
  126.  
  127.     fp = fopen(argv[1], "r");
  128.     if (fp == NULL) {
  129.         perror("Failed to open fail: ");
  130.         return 1;
  131.     }
  132.    
  133.     doStuff(fp);
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement