Compare commits

...

6 Commits

Author SHA1 Message Date
bd3ea98f7b Remove log 2024-12-25 11:17:21 -05:00
7394e66f5c C 2024 day 19 part 2 2024-12-25 11:16:47 -05:00
975c010a2c Hashmap 2024-12-25 11:16:39 -05:00
f5f0d6328c C 2024 day 19 part 1 2024-12-25 08:04:58 -05:00
62ae713bd2 C 2024 day 18 part 2 2024-12-25 06:15:52 -05:00
1449f587a4 C 2024 day 18 part 1 2024-12-25 06:06:19 -05:00
4 changed files with 230 additions and 0 deletions

108
c/2024/18/ram_run.c Normal file
View File

@ -0,0 +1,108 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "../../lib/aoc.h"
#define DIMENSION 71
#define QUEUE_LENGTH 200
typedef struct Node {
int x;
int y;
char c;
int cost;
} Node;
typedef struct Grid {
Node grid[DIMENSION][DIMENSION];
} Grid;
void grid_dijkstra(Grid *g) {
Node* queue[QUEUE_LENGTH];
queue[0] = &(g->grid[0][0]);
int queue_start = 0;
int queue_end = 1;
g->grid[0][0].c = '*';
g->grid[0][0].cost = 0;
while (queue_start != queue_end) {
// get first element of queue
Node* n = queue[queue_start++];
queue_start %= QUEUE_LENGTH;
n->c = ' ';
// add its neighbors to the queue
if (n->x - 1 >= 0 && g->grid[n->x - 1][n->y].c == '.') {
g->grid[n->x - 1][n->y].c = '*';
g->grid[n->x - 1][n->y].cost = n->cost + 1;
queue[queue_end++] = &(g->grid[n->x - 1][n->y]);
queue_end %= 200;
}
if (n->y - 1 >= 0 && g->grid[n->x][n->y - 1].c == '.') {
g->grid[n->x][n->y - 1].c = '*';
g->grid[n->x][n->y - 1].cost = n->cost + 1;
queue[queue_end++] = &(g->grid[n->x][n->y - 1]);
queue_end %= 200;
}
if (n->x + 1 < DIMENSION && g->grid[n->x + 1][n->y].c == '.') {
g->grid[n->x + 1][n->y].c = '*';
g->grid[n->x + 1][n->y].cost = n->cost + 1;
queue[queue_end++] = &(g->grid[n->x + 1][n->y]);
queue_end %= 200;
}
if (n->y + 1 < DIMENSION && g->grid[n->x][n->y + 1].c == '.') {
g->grid[n->x][n->y + 1].c = '*';
g->grid[n->x][n->y + 1].cost = n->cost + 1;
queue[queue_end++] = &(g->grid[n->x][n->y + 1]);
queue_end %= 200;
}
}
}
int main() {
char *line;
Grid g;
for (int i = 0; i < DIMENSION; i++) {
for (int j = 0; j < DIMENSION; j++) {
g.grid[i][j].x = i;
g.grid[i][j].y = j;
g.grid[i][j].c = '.';
g.grid[i][j].cost = -1;
}
}
int x, y;
//while ((line = aoc_read_line()) != NULL) {
for (int i = 0; i < 1024; i++) {
line = aoc_read_line();
sscanf(line, "%d,%d", &x, &y);
g.grid[x][y].c = '#';
}
Grid original_grid;
memcpy(&original_grid, &g, sizeof(Grid));
grid_dijkstra(&g);
printf("Part 1: %d\n", g.grid[DIMENSION - 1][DIMENSION - 1].cost);
while ((line = aoc_read_line()) != NULL) {
sscanf(line, "%d,%d", &x, &y);
original_grid.grid[x][y].c = '#';
memcpy(&g, &original_grid, sizeof(Grid));
grid_dijkstra(&g);
if (g.grid[DIMENSION - 1][DIMENSION - 1].cost == -1) break;
}
printf("Part 2: %d,%d\n", x, y);
aoc_free();
exit(0);
}

76
c/2024/19/linen_layout.c Normal file
View File

@ -0,0 +1,76 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "../../lib/aoc.h"
typedef char Towel[10];
typedef struct Towels {
Towel towels[2000];
int towels_count;
AocHashmap *map;
} Towels;
bool towels_valid_design(Towels *t, char *design) {
if (strlen(design) == 0) return true;
for (int i = 0; i < t->towels_count; i++) {
if (strstr(design, t->towels[i]) == design) {
if (towels_valid_design(t, design + strlen(t->towels[i]))) return true;
}
}
return false;
}
long towels_valid_designs(Towels *t, char *design) {
AocHashmapNode *memo = aoc_hashmap_get(t->map, design);
if (memo != NULL) return memo->value;
if (strlen(design) == 0) return 1;
long valid_designs = 0;
for (int i = 0; i < t->towels_count; i++) {
if (strstr(design, t->towels[i]) == design) {
valid_designs += towels_valid_designs(t, design + strlen(t->towels[i]));
}
}
aoc_hashmap_put(t->map, design, valid_designs);
return valid_designs;
}
int main() {
char *line;
Towels towels;
towels.towels_count = 0;
towels.map = malloc(sizeof(AocHashmap));
for (int i = 0; i < AOC_HASHMAP_RADIX; i++) {
towels.map->buckets[i] = NULL;
}
line = aoc_read_line();
char *token = strtok(line, ", ");
while (token != NULL) {
strcpy(towels.towels[towels.towels_count++], token);
token = strtok(NULL, ", ");
}
aoc_read_line(); // blank line
int valid_single_designs = 0;
long valid_multi_designs = 0;
while ((line = aoc_read_line()) != NULL) {
if (towels_valid_design(&towels, line)) valid_single_designs++;
valid_multi_designs += towels_valid_designs(&towels, line);
}
printf("Part 1: %d\n", valid_single_designs);
printf("Part 2: %ld\n", valid_multi_designs);
aoc_free();
exit(0);
}

View File

@ -2,6 +2,8 @@
#include <stdio.h>
#include <unistd.h>
#include "hashmap.h"
#define INITIAL_BUFFER_SIZE 2
char *input_buffer;

44
c/lib/hashmap.h Normal file
View File

@ -0,0 +1,44 @@
#define AOC_HASHMAP_RADIX 1024
int aoc_hash(const char *string) {
int len = strlen(string);
int hash = 1;
for (int i = 0; i < len; i++) {
hash *= 7;
hash += string[i] * 13;
hash %= AOC_HASHMAP_RADIX;
}
return hash;
}
typedef struct AocHashmapNode AocHashmapNode;
typedef struct AocHashmapNode {
char *key;
long value;
AocHashmapNode *next;
} AocHashmapNode;
typedef struct AocHashmap {
AocHashmapNode* buckets[AOC_HASHMAP_RADIX];
} AocHashmap;
AocHashmapNode *aoc_hashmap_get(AocHashmap *map, char *key) {
int hash = aoc_hash(key);
AocHashmapNode *node = map->buckets[hash];
while (node != NULL && strcmp(node->key, key) != 0) node = node->next;
return node;
}
void *aoc_hashmap_put(AocHashmap *map, char *key, long value) {
int hash = aoc_hash(key);
AocHashmapNode *node = map->buckets[hash];
while (node != NULL) node = node->next;
map->buckets[hash] = malloc(sizeof(AocHashmapNode));
node = map->buckets[hash];
node->key = malloc(strlen(key) + 1);
strcpy(node->key, key);
node->value = value;
node->next = NULL;
}