plasl day2p1 and a lot of other junk i never pushed

This commit is contained in:
m
2025-12-03 19:47:49 -05:00
parent 69d5dabb05
commit 70facc84b9
11 changed files with 5582 additions and 59 deletions

146
src/c/2024/day16/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));
}

90
src/c/2024/day19/part1.c Normal file
View File

@@ -0,0 +1,90 @@
#include <stdio.h>
#include <string.h>
char towels[1000][100];
int towel_length[1000];
char wanted[1000][100];
int num_towels = 0;
// returns index of first mismatch
int first_mismatch(char* a, char* b) {
int i = 0;
while (a[i] == b[i] && a[i] && b[i]) {
i++;
}
return i;
}
// returns 1 if found, 0 if not
int find_arrangement(char* wanted) {
if (wanted[0] == 0) {
return 1;
}
for (int i = 0; i < num_towels; i++) {
//printf("%s v %s: %d\n", wanted, towels[i], first_mismatch(wanted, towels[i]));
if (first_mismatch(wanted, towels[i]) == towel_length[i]) {
if (find_arrangement(&wanted[towel_length[i]])) {
return 1;
}
}
}
return 0;
}
int main() {
char *line_buf = NULL;
size_t buffer_size = 1000;
int amount = getline(&line_buf, &buffer_size, stdin);
int last_comma = 0;
for (int i = 0; i < amount; i++) {
if (line_buf[i] == ',') {
strncpy(towels[num_towels], &line_buf[last_comma], i - last_comma);
towels[num_towels][i - last_comma] = 0;
towel_length[num_towels] = i - last_comma;
//printf("%s\n", towels[num_towels]);
num_towels++;
last_comma = i + 2;
}
}
// parse empty line
getline(&line_buf, &buffer_size, stdin);
int num_wanted = 0;
int n;
while ((n = getline(&line_buf, &buffer_size, stdin)) > 0) {
strncpy(wanted[num_wanted], line_buf, n - 1);
num_wanted++;
//printf("!%s\n", wanted[num_wanted - 1]);
}
int total = 0;
for (int i = 0; i < num_wanted; i++) {
total += find_arrangement(wanted[i]);
//printf("%s, %d\n", wanted[i], find_arrangement(wanted[i]));
}
printf("res: %d", total);
}

126
src/c/2024/day19/part2.c Normal file
View File

@@ -0,0 +1,126 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CACHE_SIZE 100000
char towels[1000][100];
int towel_length[1000];
char wanted[1000][100];
char cache_key[CACHE_SIZE][100];
long cache_value[CACHE_SIZE];
int cache_len = 0;
int num_towels = 0;
// returns index of first mismatch
int first_mismatch(char* a, char* b) {
int i = 0;
while (a[i] == b[i] && a[i] && b[i]) {
i++;
}
return i;
}
// returns 1 if found, 0 if not
long find_arrangement(char* wanted, int depth) {
if (wanted[0] == 0) {
return 1;
}
for (int i = 0; i < cache_len; i++) {
if (strcmp(cache_key[i], wanted) == 0) {
//printf("cache hit\n");
return cache_value[i];
}
}
long total = 0;
for (int i = 0; i < num_towels; i++) {
//if (depth == 0) {
//}
if (first_mismatch(wanted, towels[i]) == towel_length[i]) {
//if (depth <= 10) {
//printf("%d: %s v %s: %d.. %d\n", depth, wanted, towels[i], first_mismatch(wanted, towels[i]), total);
//}
total += find_arrangement(&wanted[towel_length[i]], depth + 1);
}
}
strcpy(cache_key[cache_len], wanted);
cache_value[cache_len] = total;
cache_len++;
return total;
}
int main() {
char *line_buf = NULL;
size_t buffer_size = 1000;
int amount = getline(&line_buf, &buffer_size, stdin);
int last_comma = 0;
for (int i = 0; i < amount; i++) {
if (line_buf[i] == ',') {
strncpy(towels[num_towels], &line_buf[last_comma], i - last_comma);
towels[num_towels][i - last_comma] = 0;
towel_length[num_towels] = i - last_comma;
//printf("%s, %d\n", towels[num_towels], towel_length[num_towels]);
num_towels++;
last_comma = i + 2;
}
}
strncpy(towels[num_towels], &line_buf[last_comma], amount - last_comma - 1);
towels[num_towels][amount - last_comma - 1] = 0;
towel_length[num_towels] = amount - last_comma - 1;
//printf("%s\n", towels[num_towels]);
num_towels++;
//exit(0);
// parse empty line
getline(&line_buf, &buffer_size, stdin);
int num_wanted = 0;
int n;
while ((n = getline(&line_buf, &buffer_size, stdin)) > 0) {
strncpy(wanted[num_wanted], line_buf, n - 1);
num_wanted++;
//printf("!%s\n", wanted[num_wanted - 1]);
}
long total = 0;
for (int i = 0; i < num_wanted; i++) {
total += find_arrangement(wanted[i], 0);
printf("%s, %ld\n", wanted[i], find_arrangement(wanted[i], 0));
//printf("%s, %d\n", wanted[i], find_arrangement(wanted[i]));
}
printf("res: %ld", total);
}

200
src/c/2024/day20/part1.c Normal file
View File

@@ -0,0 +1,200 @@
#include <stdio.h>
#include <string.h>
#define BOARD_SIZE 141
#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) || map[coord->x][coord->y] == '#') {
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)) {
//printf("%d, %d!", coord.x, coord.y);
int value = values[coord.x][coord.y];
//printf("%d, %d: %d\n", coord.x, coord.y, values[coord.x][coord.y]);
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 0;
}
int cheats = 0;
int test_cheat(int x, int y, int value) {
int v;
if (IN_BOUND(x, y) && (map[x][y] == '.' || map[x][y] == 'E') && (v = values[x][y] - value) > 2) {
v -= 2;
printf("cheat: %d\n", v, x, y);
if (v >= 100) {
cheats++;
}
return v;
}
return 0;
}
int test_next(int x, int y, int value) {
//printf("%d, %d\n", value, values[x][y]);
return (IN_BOUND(x, y) && map[x][y] == '.' && values[x][y] - value == 1);
}
int find_cheat(int x, int y) {
//printf("%d, %d ", x, y);
//printf("%c\n", map[x][y]);
if (!IN_BOUND(x, y)) {
return 0;
}
int value = values[x][y];
int v;
test_cheat(x, y + 2, value);
test_cheat(x, y - 2, value);
test_cheat(x + 2, y, value);
test_cheat(x - 2, y, value);
if (test_next(x + 1, y, value)) {
return find_cheat(x + 1, y);
}
if (test_next(x - 1, y, value)) {
return find_cheat(x - 1, y);
}
if (test_next(x, y + 1, value)) {
return find_cheat(x, y + 1);
}
if (test_next(x, y - 1, value)) {
return find_cheat(x, y - 1);
}
return 0;
}
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;
struct coordinate start, end;
int y = 0;
while (getline(&line_buf, &buffer_size, stdin) > 0) {
for (int x = 0; x < BOARD_SIZE; x++) {
map[x][y] = line_buf[x];
if (line_buf[x] == 'S') {
start.x = x;
start.y = y;
}
if (line_buf[x] == 'E') {
end.x = x;
end.y = y;
}
}
y++;
}
dfs(start);
find_cheat(start.x, start.y);
printf("res: %d", cheats);
}

224
src/c/2024/day20/part2.c Normal file
View File

@@ -0,0 +1,224 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BOARD_SIZE 141
#define CHEAT_LENGTH 20
#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];
struct coordinate cheat_deltas[10000];
int num_cheat_deltas;
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) || map[coord->x][coord->y] == '#') {
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)) {
//printf("%d, %d!", coord.x, coord.y);
int value = values[coord.x][coord.y];
//printf("%d, %d: %d\n", coord.x, coord.y, values[coord.x][coord.y]);
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 0;
}
int cheats = 0;
int test_cheat(int x, int y, int value, int dist) {
int v;
if (IN_BOUND(x, y) && (map[x][y] == '.' || map[x][y] == 'E') && (v = values[x][y] - value) > dist) {
v -= dist;
//printf("cheat: %d\n", v, x, y);
if (v >= 100) {
cheats++;
}
return v;
}
return 0;
}
int test_next(int x, int y, int value) {
//printf("%d, %d\n", value, values[x][y]);
return (IN_BOUND(x, y) && map[x][y] == '.' && values[x][y] - value == 1);
}
int find_cheat(int x, int y) {
printf("%d, %d ", x, y);
printf("%c\n", map[x][y]);
if (!IN_BOUND(x, y)) {
return 0;
}
int value = values[x][y];
int v;
for (int i = 0; i < num_cheat_deltas; i++) {
//printf("%d, %d\n", x + cheat_deltas[i].x, y + cheat_deltas[i].y);
test_cheat(x + cheat_deltas[i].x, y + cheat_deltas[i].y, value, abs(cheat_deltas[i].x) + abs(cheat_deltas[i].y));
}
if (test_next(x + 1, y, value)) {
return find_cheat(x + 1, y);
}
if (test_next(x - 1, y, value)) {
return find_cheat(x - 1, y);
}
if (test_next(x, y + 1, value)) {
return find_cheat(x, y + 1);
}
if (test_next(x, y - 1, value)) {
return find_cheat(x, y - 1);
}
return 0;
}
int main() {
// calc cheats
for (int x = -CHEAT_LENGTH; x <= CHEAT_LENGTH; x++) {
for (int y = -CHEAT_LENGTH; y <= CHEAT_LENGTH; y++) {
if (abs(x) + abs(y) <= CHEAT_LENGTH) {
//printf("%d, %d\n", x, y);
cheat_deltas[num_cheat_deltas].x = x;
cheat_deltas[num_cheat_deltas].y = y;
num_cheat_deltas++;
}
}
}
//exit(0);
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;
struct coordinate start, end;
int y = 0;
while (getline(&line_buf, &buffer_size, stdin) > 0) {
printf(line_buf);
for (int x = 0; x < BOARD_SIZE; x++) {
map[x][y] = line_buf[x];
if (line_buf[x] == 'S') {
start.x = x;
start.y = y;
}
if (line_buf[x] == 'E') {
end.x = x;
end.y = y;
}
}
y++;
}
dfs(start);
find_cheat(start.x, start.y);
printf("res: %d", cheats);
}

View File

@@ -21,6 +21,10 @@ int number_from_id(int id) {
}
int get_letter(int id) {
return id / 65536;
}
int operator_to_id(char* str){
if (strcmp(str, "AND") == 0) {
@@ -148,8 +152,11 @@ int main() {
num_rules++;
}
int found_zs[max_z];
max_z++;
int xy_ands[max_z];
int xy_xors[max_z];
int rule_applied[num_rules];
for (int i = 0; i < num_rules; i++) {
@@ -160,76 +167,32 @@ int main() {
for (int i = 0; i < max_z; i++) {
found_zs[i] = 0;
xy_ands[i] = 0;
xy_xors[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++) {
for (int i = 0; i < num_rules; i++) {
if (get_letter(rules[i].op1) == 'x' && get_letter(rules[i].op2) == 'y' ||
get_letter(rules[i].op1) == 'y' && get_letter(rules[i].op2) == 'x') {
int v1, v2;
if (rule_applied[i]) {
continue;
if (rules[i].operator == 3) {
xy_xors[number_from_id(rules[i].op1)] = rules[i].result;
} else if (rules[i].operator == 1) {
xy_ands[number_from_id(rules[i].op1)] = rules[i].result;
}
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);
printf("%d: %d %d\n", rules[i].operator, number_from_id(rules[i].op1), number_from_id(rules[i].op2));
}
int finished = 1;
for (int i = 0; i < max_z; i++) {
if (found_zs[i] == 0) {
finished = 0;
break;
}
if (rules[i].result / 65536 == 'z') {
//printf("%d", rules[i].operator);
}
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);
}