added idk a bunch of junk

This commit is contained in:
m
2024-12-25 10:34:54 -05:00
parent d9ff4e14fe
commit aecea3dde7
21 changed files with 15258 additions and 1 deletions

146
src/c/2024/day18/part1.c Normal file
View File

@@ -0,0 +1,146 @@
#include <stdio.h>
#include <string.h>
#define BOARD_SIZE 71
#define NUM_BYTES 1024
#define WALL 10005
#define IN_BOUND(x, y) (x < BOARD_SIZE && x >= 0 && y < BOARD_SIZE && y >= 0)
#define MIN(a,b) (((a)<(b))?(a):(b))
struct coordinate {
int x, y;
};
struct coordinate queue[10000];
int queue_len;
int queue_start;
char map[BOARD_SIZE][BOARD_SIZE];
int values[BOARD_SIZE][BOARD_SIZE];
int queue_pop(struct coordinate *coord) {
if (queue_len == 0) {
return -1;
}
memcpy(coord, &queue[queue_start], sizeof(struct coordinate));
queue_start++;
queue_len--;
return 0;
}
void queue_push(struct coordinate *coord) {
//printf("%d, %da\n", coord->x, coord->y);
memcpy(&queue[queue_start + queue_len], coord, sizeof(struct coordinate));
queue_len++;
}
void check_and_push(struct coordinate *coord, int depth) {
//printf("%d\n", depth);
if (!IN_BOUND(coord->x, coord->y) || values[coord->x][coord->y] == WALL) {
return;
}
if (values[coord->x][coord->y] == 100000) {
//printf("%d\n", depth);
values[coord->x][coord->y] = depth + 1;
queue_push(coord);
}
}
int dfs(struct coordinate coord) {
queue_push(&coord);
values[coord.x][coord.y] = 0;
while (!queue_pop(&coord)) {
int value = values[coord.x][coord.y];
//printf("%d, %d: %d\n", coord.x, coord.y, values[coord.x][coord.y]);
if (coord.x == BOARD_SIZE - 1 && coord.y == BOARD_SIZE - 1) {
break;
}
coord.x++;
check_and_push(&coord, value);
coord.x -= 2;
check_and_push(&coord, value);
coord.x++;
coord.y++;
check_and_push(&coord, value);
coord.y -= 2;
check_and_push(&coord, value);
coord.y++;
}
return values[BOARD_SIZE - 1][BOARD_SIZE - 1];
}
int main() {
char *line_buf = NULL;
size_t buffer_size = 100;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
map[i][j] = '.';
values[i][j] = 100000;
}
}
struct coordinate coords[10000];
int num_cords = 0;
while (getline(&line_buf, &buffer_size, stdin) > 0) {
sscanf(line_buf, "%d,%d\n", &coords[num_cords].x, &coords[num_cords].y);
num_cords++;
}
for (int i = 0; i < NUM_BYTES; i++) {
struct coordinate coord = coords[i];
values[coords[i].x][coords[i].y] = WALL;
//printf("%d, %dwa\n", coords[i].x, coords[i].y);
//values[coord.x][coord.y] = 10000;
}
struct coordinate start;
start.x = 0;
start.y = 0;
printf("res: %d\n", dfs(start));
}

170
src/c/2024/day18/part2.c Normal file
View File

@@ -0,0 +1,170 @@
#include <stdio.h>
#include <string.h>
#define BOARD_SIZE 71
#define WALL 10005
#define IN_BOUND(x, y) (x < BOARD_SIZE && x >= 0 && y < BOARD_SIZE && y >= 0)
#define MIN(a,b) (((a)<(b))?(a):(b))
struct coordinate {
int x, y;
};
struct coordinate queue[10000];
int queue_len;
int queue_start;
char map[BOARD_SIZE][BOARD_SIZE];
int values[BOARD_SIZE][BOARD_SIZE];
void queue_empty() {
queue_start = 0;
queue_len = 0;
}
int queue_pop(struct coordinate *coord) {
if (queue_len == 0) {
return -1;
}
memcpy(coord, &queue[queue_start], sizeof(struct coordinate));
queue_start++;
queue_len--;
return 0;
}
void queue_push(struct coordinate *coord) {
//printf("%d, %da\n", coord->x, coord->y);
memcpy(&queue[queue_start + queue_len], coord, sizeof(struct coordinate));
queue_len++;
}
void check_and_push(struct coordinate *coord, int depth) {
//printf("%d\n", depth);
if (!IN_BOUND(coord->x, coord->y) || values[coord->x][coord->y] == WALL) {
return;
}
if (values[coord->x][coord->y] == 100000) {
//printf("%d\n", depth);
values[coord->x][coord->y] = depth + 1;
queue_push(coord);
}
}
int dfs(struct coordinate coord) {
queue_push(&coord);
values[coord.x][coord.y] = 0;
while (!queue_pop(&coord)) {
int value = values[coord.x][coord.y];
//printf("%d, %d: %d\n", coord.x, coord.y, values[coord.x][coord.y]);
if (coord.x == BOARD_SIZE - 1 && coord.y == BOARD_SIZE - 1) {
break;
}
coord.x++;
check_and_push(&coord, value);
coord.x -= 2;
check_and_push(&coord, value);
coord.x++;
coord.y++;
check_and_push(&coord, value);
coord.y -= 2;
check_and_push(&coord, value);
coord.y++;
}
return values[BOARD_SIZE - 1][BOARD_SIZE - 1];
}
int main() {
char *line_buf = NULL;
size_t buffer_size = 100;
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
map[i][j] = '.';
values[i][j] = 100000;
}
}
struct coordinate coords[10000];
int num_cords = 0;
while (getline(&line_buf, &buffer_size, stdin) > 0) {
sscanf(line_buf, "%d,%d\n", &coords[num_cords].x, &coords[num_cords].y);
num_cords++;
}
for (int c = 0; c < num_cords; c++) {
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
values[i][j] = 100000;
}
}
for (int i = 0; i < c; i++) {
struct coordinate coord = coords[i];
values[coords[i].x][coords[i].y] = WALL;
}
struct coordinate start;
start.x = 0;
start.y = 0;
int res = dfs(start);
printf("%d: %d\n", c, res);
if (res == 100000) {
printf("res: %d,%d\n", coords[c - 1].x, coords[c - 1].y);
break;
}
queue_empty();
//printf("res: %d\n", dfs(start));
}
}

235
src/c/2024/day24/part1.c Normal file
View File

@@ -0,0 +1,235 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int numeric_id(char* string) {
return string[0] * 65536 + string[1] * 256 + string[2];
}
int number(char* string) {
return (string[1] - 48) * 10 + string[2] - 48;
}
int number_from_id(int id) {
return ((id / 256 % 256) - 48) * 10 + (id % 256 - 48);
}
int operator_to_id(char* str){
if (strcmp(str, "AND") == 0) {
return 1;
} else if (strcmp(str, "OR") == 0) {
return 2;
} else if (strcmp(str, "XOR") == 0) {
return 3;
}
printf("Bad operator: %s\n", str);
exit(-1);
}
int zvalue_to_id(int znum) {
return 'z' * 65536 + (znum / 10 + 48) * 256 + znum % 10 + 48;
}
int apply_operator(int v1, int op, int v2) {
if (op == 1) {
return v1 & v2;
} else if (op == 2) {
return v1 | v2;
} else if (op == 3) {
return v1 ^ v2;
}
printf("Bad operator id: %d\n", op);
exit(-1);
}
struct rule {
int op1;
int op2;
int operator;
int result;
};
struct value {
int key;
int value;
};
struct value values[1000];
struct rule rules[1000];
int num_values = 0;
int num_rules = 0;
int add_value(int key, int value) {
values[num_values].value = value;
values[num_values].key = key;
num_values++;
}
int get_value(int id) {
for (int i = 0; i < num_values; i++) {
if (values[i].key == id) {
return values[i].value;
}
}
return -1;
}
int main() {
int max_z = 0;
// reading junk
size_t buffer_size = 100;
char *line_buf = NULL;
char buffer[buffer_size];
char op1[buffer_size];
char op2[buffer_size];
char operator[buffer_size];
char end_goal[buffer_size];
int value;
while (getline(&line_buf, &buffer_size, stdin) > 1) {
sscanf(line_buf, "%3s: %d\n", buffer, &value);
add_value(numeric_id(buffer), value);
}
while (getline(&line_buf, &buffer_size, stdin) > 0) {
sscanf(line_buf, "%3s %3s %3s -> %3s", op1, operator, op2, end_goal);
if (end_goal[0] == 'z') {
if (number(end_goal) > max_z) {
max_z = number(end_goal);
}
}
rules[num_rules].op1 = numeric_id(op1);
rules[num_rules].op2 = numeric_id(op2);
rules[num_rules].operator = operator_to_id(operator);
rules[num_rules].result = numeric_id(end_goal);
num_rules++;
}
int found_zs[max_z];
max_z++;
int rule_applied[num_rules];
for (int i = 0; i < num_rules; i++) {
rule_applied[i] = 0;
}
for (int i = 0; i < max_z; i++) {
found_zs[i] = 0;
}
printf("Read %d values and %d rules, trying to find %d zs\n", num_values, num_rules, max_z);
for (;;) {
for (int i = 0; i < num_rules; i++) {
int v1, v2;
if (rule_applied[i]) {
continue;
}
if ((v1 = get_value(rules[i].op1)) >= 0 && (v2 = get_value(rules[i].op2)) >= 0) {
int res = apply_operator(v1, rules[i].operator, v2);
//printf("%d: %d\n", rules[i].result, res);
add_value(rules[i].result, res);
if (rules[i].result / 65536 == 'z') {
found_zs[number_from_id(rules[i].result)] = 1;
printf("Found z%d\n", number_from_id(rules[i].result));
//printf("%x", rules[i].result);
}
rule_applied[i] = 1;
}
//printf("%d, %d\n", v1, v2);
}
int finished = 1;
for (int i = 0; i < max_z; i++) {
if (found_zs[i] == 0) {
finished = 0;
break;
}
}
if (finished) {
break;
}
}
long out = 0;
for (int i = max_z - 1; i >= 0; i--) {
out *= 2;
int n = get_value(zvalue_to_id(i));
printf("%d", n);
out += n;
//printf("%x\n", zvalue_to_id(i));
}
printf("\nres: %ld\n", out);
}

235
src/c/2024/day24/part2.c Normal file
View File

@@ -0,0 +1,235 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int numeric_id(char* string) {
return string[0] * 65536 + string[1] * 256 + string[2];
}
int number(char* string) {
return (string[1] - 48) * 10 + string[2] - 48;
}
int number_from_id(int id) {
return ((id / 256 % 256) - 48) * 10 + (id % 256 - 48);
}
int operator_to_id(char* str){
if (strcmp(str, "AND") == 0) {
return 1;
} else if (strcmp(str, "OR") == 0) {
return 2;
} else if (strcmp(str, "XOR") == 0) {
return 3;
}
printf("Bad operator: %s\n", str);
exit(-1);
}
int zvalue_to_id(int znum) {
return 'z' * 65536 + (znum / 10 + 48) * 256 + znum % 10 + 48;
}
int apply_operator(int v1, int op, int v2) {
if (op == 1) {
return v1 & v2;
} else if (op == 2) {
return v1 | v2;
} else if (op == 3) {
return v1 ^ v2;
}
printf("Bad operator id: %d\n", op);
exit(-1);
}
struct rule {
int op1;
int op2;
int operator;
int result;
};
struct value {
int key;
int value;
};
struct value values[1000];
struct rule rules[1000];
int num_values = 0;
int num_rules = 0;
int add_value(int key, int value) {
values[num_values].value = value;
values[num_values].key = key;
num_values++;
}
int get_value(int id) {
for (int i = 0; i < num_values; i++) {
if (values[i].key == id) {
return values[i].value;
}
}
return -1;
}
int main() {
int max_z = 0;
// reading junk
size_t buffer_size = 100;
char *line_buf = NULL;
char buffer[buffer_size];
char op1[buffer_size];
char op2[buffer_size];
char operator[buffer_size];
char end_goal[buffer_size];
int value;
while (getline(&line_buf, &buffer_size, stdin) > 1) {
sscanf(line_buf, "%3s: %d\n", buffer, &value);
add_value(numeric_id(buffer), value);
}
while (getline(&line_buf, &buffer_size, stdin) > 0) {
sscanf(line_buf, "%3s %3s %3s -> %3s", op1, operator, op2, end_goal);
if (end_goal[0] == 'z') {
if (number(end_goal) > max_z) {
max_z = number(end_goal);
}
}
rules[num_rules].op1 = numeric_id(op1);
rules[num_rules].op2 = numeric_id(op2);
rules[num_rules].operator = operator_to_id(operator);
rules[num_rules].result = numeric_id(end_goal);
num_rules++;
}
int found_zs[max_z];
max_z++;
int rule_applied[num_rules];
for (int i = 0; i < num_rules; i++) {
rule_applied[i] = 0;
}
for (int i = 0; i < max_z; i++) {
found_zs[i] = 0;
}
printf("Read %d values and %d rules, trying to find %d zs\n", num_values, num_rules, max_z);
for (;;) {
for (int i = 0; i < num_rules; i++) {
int v1, v2;
if (rule_applied[i]) {
continue;
}
if ((v1 = get_value(rules[i].op1)) >= 0 && (v2 = get_value(rules[i].op2)) >= 0) {
int res = apply_operator(v1, rules[i].operator, v2);
//printf("%d: %d\n", rules[i].result, res);
add_value(rules[i].result, res);
if (rules[i].result / 65536 == 'z') {
found_zs[number_from_id(rules[i].result)] = 1;
printf("Found z%d\n", number_from_id(rules[i].result));
//printf("%x", rules[i].result);
}
rule_applied[i] = 1;
}
//printf("%d, %d\n", v1, v2);
}
int finished = 1;
for (int i = 0; i < max_z; i++) {
if (found_zs[i] == 0) {
finished = 0;
break;
}
}
if (finished) {
break;
}
}
long out = 0;
for (int i = max_z - 1; i >= 0; i--) {
out *= 2;
int n = get_value(zvalue_to_id(i));
printf("%d", n);
out += n;
//printf("%x\n", zvalue_to_id(i));
}
printf("\nres: %ld\n", out);
}

83
src/c/2024/day25/part1.c Normal file
View File

@@ -0,0 +1,83 @@
#include <stdio.h>
struct lockkey {
int positions[5];
};
int main() {
size_t buffer_size = 100;
char* line_buf = NULL;
struct lockkey locks[1000];
struct lockkey keys[1000];
int num_locks, num_keys;
int is_key;
while (getline(&line_buf, &buffer_size, stdin) > 0) {
struct lockkey *current;
if (line_buf[0] == '.') {
current = &keys[num_keys];
num_keys++;
printf("Read key: ");
} else {
current = &locks[num_locks];
num_locks++;
printf("Read lock: ");
}
for (int i = 0; i < 5; i++) {
current->positions[i] = 0;
}
for (int i = 0; i < 5; i++) {
getline(&line_buf, &buffer_size, stdin);
for (int j = 0; j < 5; j++) {
if (line_buf[j] == '#') {
current->positions[j]++;
}
}
}
printf("%d,%d,%d,%d,%d\n", current->positions[0], current->positions[1], current->positions[2], current->positions[3], current->positions[4]);
getline(&line_buf, &buffer_size, stdin);
getline(&line_buf, &buffer_size, stdin);
}
long out = 0;
for (int i = 0; i < num_locks; i++) {
for (int j = 0; j < num_keys; j++) {
int ok = 1;
for (int k = 0; k < 5; k++) {
if (locks[i].positions[k] + keys[j].positions[k] > 5) {
ok = 0;
break;
}
}
if (ok) {
out++;
}
}
}
printf("res: %ld\n", out);
}