Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
1.7 kB
5
Indexable
Never
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const char *textRepresentations[] = {"zero",  "one",  "two", "three",
                                            "four",  "five", "six", "seven",
                                            "eight", "nine"};

void getDigits(const char *const str, char result[3]) {
  for (size_t i = 0; i < strlen(str) + 1; i++) {
    char code = str[i];
    printf("%c\n", code);
    if (code >= 48 && code <= 57) {
      if (result[0] == 0) {
        result[0] = code;
      }
      result[1] = code;
    }
    if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) {
      for (size_t t = 0; t < 10; t++) {
        char substring[6] = {0};
        strncpy(substring, &str[i], 6);
        if (strncmp(substring, textRepresentations[t],
                    strlen(textRepresentations[t])) == 0) {
          if (result[0] == 0) {
            result[0] = t + 48;
          }
          result[1] = t + 48;
        }
      }
    }
  }
}

int main(int argc, char **argv) {
  int calibration = 0;
  char lines[1000][256] = {0};
  FILE *fp;
  fp = fopen("./day1.input", "r");
  if (fp == NULL) {
    fprintf(stderr, "Failed to load input.\n");
    return EXIT_FAILURE;
  }
  for (size_t i = 0; i < 1000; i++) {
    fgets(lines[i], 256, fp);
  }
  for (size_t i = 0; i < 1000; i++) {
    char val[3] = {0};
    printf("Line: %s", lines[i]);
    getDigits(lines[i], val);
    printf("val: %s\n", val);
    calibration += atoi(val);
  }
  printf("Calibration: %d\n", calibration);
  for (size_t i = 0; i < 10; i++) {
    printf("Str: %s, len: %zu\n", textRepresentations[i],
           strlen(textRepresentations[i]));
  }
}
Leave a Comment