Day 2: Gift Shop

Megathread guidelines

  • Keep top level comments as only solutions, if you want to say something other than a solution put it in a new post. (replies to comments can be whatever)
  • You can send code in code blocks by using three backticks, the code, and then three backticks or use something such as https://topaz.github.io/paste/ if you prefer sending it through a URL

FAQ

you are viewing a single comment's thread
view the rest of the comments
[โ€“] 3 points 11 months ago

Circling back around to go faster.

p1 isolated: 76ms

p2 isolated: 82ms

combined run: 156ms

static void
one(Point rng, usize* total) {
  for (usize i = rng.x; i <= rng.y; i++) {
    c8 pid[PID_BUFSZ] = {};
    snprintf(pid, sizeof(pid), "%zu", i);
    usize len = strlen(pid);
    if (len % 2 != 0) {
      continue;
    }
    usize hlen = len / 2;
    c8 a[PID_BUFSZ] = {};
    memcpy(a, pid, hlen);
    c8 b[PID_BUFSZ] = {};
    memcpy(b, pid + hlen, hlen);
    if (strcmp(a, b) == 0) {
      *total += i;
    }
  }
}

static void
two(Point rng, usize* total) {
  for (usize i = rng.x; i <= rng.y; i++) {
    c8 pid[PID_BUFSZ] = {};
    snprintf(pid, sizeof(pid), "%zu", i);
    usize len = strlen(pid);
    for (usize j = 1; j <= len / 2; j++) {
      if (len % j != 0) {
        continue;
      }
      bool valid = true;
      for (usize k = j; k < len; k++) {
        if (pid[k] != pid[k - j]) {
          valid = false;
          break;
        }
      }
      if (valid) {
        *total += i;
        break;
      }
    }
  }
}

static void
solve(Mode mode) {
  FILE* input = fopen("input", "r");
  c8 line[LINE_BUFSZ] = {};
  fgets(line, sizeof(line), input);
  line[strcspn(line, "\n")] = 0;
  usize total = 0;
  strc tok = strtok(line, ",");
  while (tok) {
    Point rng = {};
    sscanf(tok, "%zu-%zu", &rng.x, &rng.y);
    (mode == MODE_ONE ? one : two)(rng, &total);
    tok = strtok(nullptr, ",");
  }
  fclose(input);
  printf("%zu\n", total);
}

i32
main(void) {
  solve(MODE_ONE);
  solve(MODE_TWO);
}
  • source
  • parent