Day 1: Secret Entrance

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 9 months ago* (last edited 9 months ago)

c

#include "aoc.h"
#include <stdio.h>
#include <string.h>

constexpr usize LINE_BUFSZ = (1 << 3);
constexpr i32 START = 50;
constexpr i32 TOP = 100;

static void
solve(Mode mode) {
  FILE* input = fopen("input", "r");
  c8 line[LINE_BUFSZ] = {};
  u32 zs = 0;
  i32 idx = START;
  while (fgets(line, sizeof(line), input)) {
    line[strcspn(line, "\n")] = 0;
    i32 val = 0;
    sscanf(&line[1], "%d", &val);
    if (mode == MODE_ONE) {
      i32 d = line[0] == 'L' ? -val : val;
      idx = (idx + d) % TOP;
      idx += idx < 0 ? TOP : 0;
      idx == 0 ? zs++ : 0;
    } else {
      for (i32 i = 0; i < val; i++) {
        idx = line[0] == 'L' ? idx - 1 : idx + 1;
        idx = idx == -1 ? TOP - 1 : idx;
        idx = idx == TOP ? 0 : idx;
        idx == 0 ? zs++ : 0;
      }
    }
  }
  fclose(input);
  printf("%u\n", zs);
}

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