Skip to content

Instantly share code, notes, and snippets.

@weirddan455
Created December 18, 2022 01:14
Show Gist options
  • Save weirddan455/b2d573c464fbdedc08a3da64da72f6d1 to your computer and use it in GitHub Desktop.
Save weirddan455/b2d573c464fbdedc08a3da64da72f6d1 to your computer and use it in GitHub Desktop.
Advent of Code Day 16 Part 1
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#define MAX_NEIGHBORS 8
typedef struct Room
{
char name[3];
int flow;
int num_neighbors;
int neighbors[8];
} Room;
typedef struct RoomArray
{
int size;
Room *data;
} RoomArray;
static int get_index(char *ptr, RoomArray *arr)
{
for (int i = 0; i < arr->size; i++) {
Room *cur = arr->data + i;
if (cur->name[0] == ptr[0] && cur->name[1] == ptr[1]) {
return i;
}
}
fprintf(stderr, "Failed to find index for %c%c\n", ptr[0], ptr[1]);
exit(-1);
}
static void parse_input(RoomArray *arr)
{
int fd = open("sample", O_RDONLY);
if (fd == -1) {
perror("open");
exit(-1);
}
struct stat file_info;
if (fstat(fd, &file_info) == -1) {
perror("fstat");
exit(-1);
}
char *buffer = malloc(file_info.st_size);
if (buffer == NULL) {
fprintf(stderr, "malloc failed\n");
exit(-1);
}
ssize_t bytes = read(fd, buffer, file_info.st_size);
if (bytes != file_info.st_size) {
if (bytes == -1) {
perror("read");
} else {
fprintf(stderr, "Read %zu bytes. Expected %zu\n", bytes, file_info.st_size);
}
exit(-1);
}
close(fd);
arr->size = 0;
for (ssize_t i = 0; i < bytes; i++) {
if (buffer[i] == '\n') {
arr->size += 1;
}
}
arr->data = malloc(arr->size * sizeof(Room));
if (arr->data == NULL) {
fprintf(stderr, "malloc failed\n");
exit(-1);
}
char *ptr = buffer;
for (int i = 0; i < arr->size; i++) {
ptr += 6;
Room *cur = arr->data + i;
cur->name[0] = *ptr++;
cur->name[1] = *ptr++;
cur->name[2] = '\0';
while (*ptr != '\n') {
ptr++;
}
ptr++;
}
ptr = buffer;
for (int i = 0; i < arr->size; i++) {
ptr += 23;
Room *cur = arr->data + i;
cur->flow = strtol(ptr, &ptr, 10);
ptr += 24;
if (*ptr == ' ') {
ptr++;
}
cur->num_neighbors = 0;
while (1) {
if (cur->num_neighbors >= MAX_NEIGHBORS) {
fprintf(stderr, "Too many neighbors\n");
exit(-1);
}
cur->neighbors[cur->num_neighbors] = get_index(ptr, arr);
cur->num_neighbors += 1;
ptr += 2;
if (*ptr == '\n') {
break;
}
ptr += 2;
}
ptr++;
}
free(buffer);
}
static int get_best(RoomArray *arr, Room *cur, int minutes, int score)
{
if (minutes == 0) {
return score;
}
minutes--;
int best = -1;
for (int i = 0; i < cur->num_neighbors; i++) {
int test = get_best(arr, arr->data + cur->neighbors[i], minutes, score);
if (test > best) {
best = test;
}
}
if (cur->flow > 0) {
int temp = cur->flow;
cur->flow = 0;
int test = get_best(arr, cur, minutes, score + (temp * minutes));
if (test > best) {
best = test;
}
cur->flow = temp;
}
return best;
}
int main(void)
{
RoomArray arr;
parse_input(&arr);
int answer = get_best(&arr, arr.data, 30, 0);
printf("Answer: %d\n", answer);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment