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
- What is this?: Here is a post with a large amount of details: https://programming.dev/post/6637268
- Where do I participate?: https://adventofcode.com/
- Is there a leaderboard for the community?: We have a programming.dev leaderboard with the info on how to join in this post: https://programming.dev/post/6631465


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); }