Created
December 10, 2022 00:17
-
-
Save weirddan455/b7b658f94290b94ba7ce1c96f740aee7 to your computer and use it in GitHub Desktop.
Advent of Code Day 9 Visualization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <SDL2/SDL.h> | |
#define TARGET_MS 20 | |
#define SEGMENT_SIZE 3 | |
static SDL_Renderer *init_sdl(void) | |
{ | |
if (SDL_Init(SDL_INIT_VIDEO) != 0) { | |
fprintf(stderr, "SDL_Init failed\n"); | |
exit(-1); | |
} | |
SDL_Window *window = SDL_CreateWindow("AOC Day 9 Part 2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, 0); | |
if (window == NULL) { | |
fprintf(stderr, "SDL_CreateWindow failed: %s\n", SDL_GetError()); | |
exit(-1); | |
} | |
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0); | |
if (renderer == NULL) { | |
fprintf(stderr, "SDL_CreateRenderer failed: %s\n", SDL_GetError()); | |
exit(-1); | |
} | |
return renderer; | |
} | |
static void update_rope(SDL_Rect *rope, FILE *input) | |
{ | |
char direction; | |
int steps; | |
SDL_Point move; | |
if (fscanf(input, " %c %d", &direction, &steps) == 2) { | |
switch (direction) { | |
case 'R': | |
move.x = SEGMENT_SIZE; | |
move.y = 0; | |
break; | |
case 'L': | |
move.x = -SEGMENT_SIZE; | |
move.y = 0; | |
break; | |
case 'U': | |
move.x = 0; | |
move.y = -SEGMENT_SIZE; | |
break; | |
case 'D': | |
move.x = 0; | |
move.y = SEGMENT_SIZE; | |
break; | |
default: | |
fprintf(stderr, "Invalid input\n"); | |
exit(-1); | |
} | |
for (int i = 0; i < steps; i++) { | |
rope[0].x += move.x; | |
rope[0].y += move.y; | |
for (int j = 1; j < 10; j++) { | |
SDL_Rect *head = rope + (j - 1); | |
SDL_Rect *tail = rope + j; | |
if (tail->x != head->x && tail->y != head->y) { | |
if (abs(head->x - tail->x) > SEGMENT_SIZE || abs(head->y - tail->y) > SEGMENT_SIZE) { | |
if (head->x > tail->x) { | |
tail->x += SEGMENT_SIZE; | |
} else { | |
tail->x -= SEGMENT_SIZE; | |
} | |
if (head->y > tail->y) { | |
tail->y += SEGMENT_SIZE; | |
} else { | |
tail->y -= SEGMENT_SIZE; | |
} | |
} | |
} else if (tail->x != head->x) { | |
if (abs(head->x - tail->x) > SEGMENT_SIZE) { | |
if (head->x > tail->x) { | |
tail->x += SEGMENT_SIZE; | |
} else { | |
tail->x -= SEGMENT_SIZE; | |
} | |
} | |
} else if (tail->y != head->y) { | |
if (abs(head->y - tail->y) > SEGMENT_SIZE) { | |
if (head->y > tail->y) { | |
tail->y += SEGMENT_SIZE; | |
} else { | |
tail->y -= SEGMENT_SIZE; | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
int main(void) | |
{ | |
FILE *input = fopen("input", "r"); | |
if (input == NULL) { | |
perror("fopen"); | |
return -1; | |
} | |
SDL_Renderer *renderer = init_sdl(); | |
SDL_Rect rope[10]; | |
for (int i = 0; i < 10; i++) { | |
rope[i].x = 640; | |
rope[i].y = 360; | |
rope[i].w = SEGMENT_SIZE; | |
rope[i].h = SEGMENT_SIZE; | |
} | |
Uint32 last_rendered = SDL_GetTicks(); | |
while (1) { | |
SDL_Event event; | |
while (SDL_PollEvent(&event)) { | |
switch (event.type) { | |
case SDL_QUIT: | |
return 0; | |
} | |
} | |
update_rope(rope, input); | |
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | |
SDL_RenderClear(renderer); | |
SDL_SetRenderDrawColor(renderer, 255, 0 ,0, 255); | |
SDL_RenderFillRects(renderer, rope, 10); | |
SDL_RenderPresent(renderer); | |
Uint32 cur_ticks = SDL_GetTicks(); | |
Uint32 elapsed = cur_ticks - last_rendered; | |
last_rendered = cur_ticks; | |
if (elapsed < TARGET_MS) { | |
Uint32 sleep_time = TARGET_MS - elapsed; | |
SDL_Delay(sleep_time); | |
last_rendered = SDL_GetTicks(); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment