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);
}

View File

@@ -0,0 +1,243 @@
interrupt program_start
`1 0:X` # 1 + heap size
# X is heap
# Y is values of heap
end
state start
if `ln` == 'E' then
`lisE`
end
if `ln` == 'S' then
# set for iterate heap
array_set('I', `li`, '.')
`lisS`
end
if `lw` == 0 and `ln` == 10 then
`li1+sw`
`0lw-0:V` # up
`1 1:V` # right
`lw2:V` # down
`_1 3:V` # left
end
end
interrupt program_end
`[starting]n10an`
# 0123 = NESW
`1` # start facing east
`lS10*+` # add in start position
`sv` # value for heap
`0sp` # priority starts at 0
heap_add() # add into heap
while iterate_heap() != 1 do
end
`lpn`
end
function iterate_heap
heap_pop()
`lv`
`10~`
`sdsi` # get direction and index
# if we've already been here
if array_get('L', `lv`) == 1 then
return 0
end
# finish
array_set('L', `lv`, 1)
`lin[ ]nlpn10an`
if `li` == `lE` then
#`[done]n`
return 1
end
if `li;I` != '.' then
#`[bad]n10an`
return 0
end
# inc cost
`lp1+sp`
# forward
`li`
`ld;V+` # add in vector
`10*ld+sv` # and direction
heap_add()
# inc cost again
`lp1000+sp`
# right
`ld1+4%sd`
`li`
`ld;V+` # add in vector
`10*ld+sv` # and direction
heap_add()
# left
`ld2+4%sd`
`li`
`ld;V+` # add in vector
`10*ld+sv` # and direction
heap_add()
0
end
function calculate_vecs
# 0 = forward
# 1 = right
# 2 = down
# 3 = left
if `ld` == 0 then
`lw-`
end
end
function heap_add
# priority in p, value in v
array_set('X', `0;X`, `lp`)
array_set('Y', `0;X`, `lv`)
# up heap
`0;X`
up_heap()
# inc heap size
#`[wow]n10an`
array_set('X', 0, `0;X1+`)
end
function heap_pop
if `0;X` == 1 then
`[nothing to pop]n10an`
`5Q`
end
# returns priority in p, value in v
# get return values
array_get('X', 1)
`sp`
array_get('Y', 1)
`sv`
# dec heap size
array_set('X', 0, `0;X1-`)
# move new things down
array_set('X', 1, array_get('X', `0;X`))
array_set('Y', 1, array_get('Y', `0;X`))
1
down_heap()
# down heap
end
function up_heap
# position is on the stack
`s.`
# if we're at the top of heap
return_if(`l.` == 1)
#`l.nl.2/dsn[|]n`
# if we need to swap
if array_get('X', `l.`) < array_get('X', `l.2/ds,`) then
# swap
array_swap('X', `l.`, `l,`)
array_swap('Y', `l.`, `l,`)
`l,`
up_heap()
# up heap another one
end
end
function down_heap
# position is on the stack
`dSPs,`
# check left
`lP2*s.`
if `l.` < array_get('X', 0) and array_get('X', `l.`) < array_get('X', `l,`) then
`l.s,`
end
# check right
inc('.')
if `l.` < array_get('X', 0) and array_get('X', `l.`) < array_get('X', `l,`) then
`l.s,`
end
# swap and more heap
if `l,` != `lP` then
array_swap('X', `l,`, `lP`)
array_swap('Y', `l,`, `lP`)
`l,`
down_heap()
end
`LPst`
end
function print_heap
`[size: ]n0;X1-n10an`
for '!' in 1 to `0;X` do
`[priority: ]l!;Xn[ value: ]nl!;Yn10an`
end
end

View File

@@ -0,0 +1,268 @@
interrupt program_start
`1 0:X` # 1 + heap size
# X is heap
# Y is values of heap
end
state start
if `ln` == 'E' then
`lisE`
end
if `ln` == 'S' then
# set for iterate heap
array_set('I', `li`, '.')
`lisS`
end
if `lw` == 0 and `ln` == 10 then
`li1+sw`
`0lw-0:V` # up
`1 1:V` # right
`lw2:V` # down
`_1 3:V` # left
end
end
interrupt program_end
`[starting]n10an`
# 0123 = NESW
`1` # start facing east
`lS10*+` # add in start position
array_set('O', `lE`, `2 32^`)
`sv` # value for heap
`0sp` # priority starts at 0
heap_add() # add into heap
while iterate_heap() != 1 do
end
`[backing up]n10an`
`0so`
`lE10*l++`
backtrack()
`lon`
end
function backtrack
end
function iterate_heap
heap_pop()
`lv`
`10~`
`sdsi` # get direction and index
#if we've already been here
if array_get('L', `lv`) == 1 and array_get('O', `li`) <= `lp` then
return 0
end
array_set('L', `lv`, 1)
`lin[ ]nlpn10an`
# finish
if `li` == `lE` then
#`[done]n`
# set this location as this value
if array_get('O', `li`) < `lp` then
return 1
end
`lds+`
array_set('O', `li`, `lp`)
return 0
end
if `li;I` != '.' then
#`[bad]n10an`
return 0
end
# set this location as this value
array_set('O', `li`, `lp`)
# inc cost
`lp1+sp`
# forward
`li`
`ld;V+` # add in vector
`10*ld+sv` # and direction
heap_add()
# inc cost again
`lp1000+sp`
# right
`ld1+4%sd`
`li`
`ld;V+` # add in vector
`10*ld+sv` # and direction
heap_add()
# left
`ld2+4%sd`
`li`
`ld;V+` # add in vector
`10*ld+sv` # and direction
heap_add()
0
end
function calculate_vecs
# 0 = forward
# 1 = right
# 2 = down
# 3 = left
if `ld` == 0 then
`lw-`
end
end
function heap_add
# priority in p, value in v
array_set('X', `0;X`, `lp`)
array_set('Y', `0;X`, `lv`)
# up heap
`0;X`
up_heap()
# inc heap size
#`[wow]n10an`
array_set('X', 0, `0;X1+`)
end
function heap_pop
if `0;X` == 1 then
`[nothing to pop]n10an`
`5Q`
end
# returns priority in p, value in v
# get return values
array_get('X', 1)
`sp`
array_get('Y', 1)
`sv`
# dec heap size
array_set('X', 0, `0;X1-`)
# move new things down
array_set('X', 1, array_get('X', `0;X`))
array_set('Y', 1, array_get('Y', `0;X`))
1
down_heap()
# down heap
end
function up_heap
# position is on the stack
`s.`
# if we're at the top of heap
return_if(`l.` == 1)
#`l.nl.2/dsn[|]n`
# if we need to swap
if array_get('X', `l.`) < array_get('X', `l.2/ds,`) then
# swap
array_swap('X', `l.`, `l,`)
array_swap('Y', `l.`, `l,`)
`l,`
up_heap()
# up heap another one
end
end
function down_heap
# position is on the stack
`dSPs,`
# check left
`lP2*s.`
if `l.` < array_get('X', 0) and array_get('X', `l.`) < array_get('X', `l,`) then
`l.s,`
end
# check right
inc('.')
if `l.` < array_get('X', 0) and array_get('X', `l.`) < array_get('X', `l,`) then
`l.s,`
end
# swap and more heap
if `l,` != `lP` then
array_swap('X', `l,`, `lP`)
array_swap('Y', `l,`, `lP`)
`l,`
down_heap()
end
`LPst`
end
function print_heap
`[size: ]n0;X1-n10an`
for '!' in 1 to `0;X` do
`[priority: ]l!;Xn[ value: ]nl!;Yn10an`
end
end

View File

@@ -0,0 +1,217 @@
interrupt program_start
`_1Sb` # bits ender
# combo operands
`[0]0:C`
`[1]1:C`
`[2]2:C`
`[3]3:C`
`[lA]4:C`
`[lB]5:C`
`[lC]6:C`
`[[7 combo operand]p20Q]7:C`
# opcodes
`[` # adv
`lA`
2
read_combo_operand()
`^/`
`sA`
`]0:O`
`[` # bxl
`lB`
fetch()
xor()
`sB`
`]1:O`
`[` #bst
read_combo_operand()
`8%`
`sB`
`]2:O`
`[` # jnz
return_if(`lA` == 0)
fetch()
`sp`
`]3:O`
`[` # bxc
fetch()
`st`
`lBlC`
xor()
`sB`
`]4:O`
`[` # out
if `l,` == 1 then
`[,]n`
end
read_combo_operand()
`8%n`
`1s,`
`]5:O`
`[` # bdv
`lA`
2
read_combo_operand()
`^/`
`sB`
`]6:O`
`[` # cdv
`lA`
2
read_combo_operand()
`^/`
`sC`
`]7:O`
end
state read_A
adjust_index(12)
read_number()
`sA`
set_state(read_B)
end
state read_B
adjust_index(12)
read_number()
`sB`
set_state(read_C)
end
state read_C
adjust_index(12)
read_number()
`sC`
set_state(read_program)
`0sc`
adjust_index(10)
end
state read_program
array_set('P', `lc`, `ln48-`)
inc('c')
adjust_index(1)
end
interrupt program_end
`[A:]nlAn[ B:]nlBn[ C:]nlCn[ p:]nlpn10an`
`0s,` # have commaed
`0sp` # program counter
`10an`
while `lp` < `lc` do
cpu_cycle()
#`[A:]nlAn[ B:]nlBn[ C:]nlCn[ p:]nlpn10an`
end
end
function fetch
array_get('P', `lp`)
inc('p')
end
function cpu_cycle
array_get('O', fetch())
`x`
end
function read_combo_operand
array_get('C', fetch())
`x`
end
# takes shit on the stack, leaves shit on the stack
function xor
`s1s2`
`_1Sb`
while `l1` != 0 or `l2` != 0 do
`l1 2~s3s1`
`l2 2~s4s2`
0 # this bit
if (`l3` == 0 and `l4` == 1) or (`l3` == 1 and `l4` == 0) then
`1+` # make it 1
end
`Sb`
end
0 # start at 0
for '1' in stack 'b' do
`2*` # double existing
`l1+` # add in
end
end
function read_number
# does no error checking
`1` # sign
if `ln` == '-' then
`2-` # negative
adjust_index(1)
end
`0` # c num
while `ln` >= '0' and `ln` <= '9' do
`10*`
`ln48-+` # add in number
adjust_index(1)
end
#adjust_index(-1)
`*`
end

View File

@@ -0,0 +1,264 @@
interrupt program_start
`[this program brute forces. it probably works after 90 hours though. i solved the actual thing in a spreadsheet by hand though]n10an`
`_1Sb` # bits ender
# combo operands
`[0]0:C`
`[1]1:C`
`[2]2:C`
`[3]3:C`
`[lA]4:C`
`[lB]5:C`
`[lC]6:C`
`[[7 combo operand]p20Q]7:C`
# opcodes
`[` # adv
`lA`
2
read_combo_operand()
`^/`
`sA`
`]0:O`
`[` # bxl
`lB`
fetch()
xor()
`sB`
`]1:O`
`[` #bst
read_combo_operand()
`8%`
`sB`
`]2:O`
`[` # jnz
return_if(`lA` == 0)
fetch()
`sp`
`]3:O`
`[` # bxc
fetch()
`st`
`lBlC`
xor()
`sB`
`]4:O`
`[` # out
read_combo_operand()
`8%`
`l,`
`:U`
if array_get('U', `l,`) != array_get('P', `l,`) then
`10000sp` # break early
end
inc(',')
#`[hi]n`
`]5:O`
1
`[` # bdv
`lA`
2
read_combo_operand()
`^/`
`sB`
`]6:O`
`[` # cdv
`lA`
2
read_combo_operand()
`^/`
`sC`
`]7:O`
end
state read_A
adjust_index(12)
read_number()
`sA`
set_state(read_B)
end
state read_B
adjust_index(12)
read_number()
`sB`
set_state(read_C)
end
state read_C
adjust_index(12)
read_number()
`sC`
set_state(read_program)
`0sc`
adjust_index(10)
end
state read_program
array_set('P', `lc`, `ln48-`)
inc('c')
adjust_index(1)
end
interrupt program_end
#`[A:]nlAn[ B:]nlBn[ C:]nlCn[ p:]nlpn10an`
`10an`
`0s!`
while 0 == 0 do
`l!sA`
`0sB`
`0sC`
run_program()
if `l!1000%` == 0 then
`[.]n`
#`l,n[ ]nlcn10an`
end
#`[A:]nlAn[ B:]nlBn[ C:]nlCn[ p:]nlpn10an`
#
if `l,` == `lc` then
0
for '?' in 0 to `l,` do
if array_get('U', `l?`) != array_get('P', `l?`) then
`1-`
break
end
end
`s.`
if `l.` == 0 then
`10anl!p`
break
end
end
inc('!')
end
end
function run_program
`0sp`
`0s,`
while `lp` < `lc` do
cpu_cycle()
end
end
function fetch
array_get('P', `lp`)
inc('p')
end
function cpu_cycle
array_get('O', fetch())
`x`
end
function read_combo_operand
array_get('C', fetch())
`x`
end
# takes shit on the stack, leaves shit on the stack
function xor
`s1s2`
`_1Sb`
while `l1` != 0 or `l2` != 0 do
`l1 2~s3s1`
`l2 2~s4s2`
0 # this bit
if (`l3` == 0 and `l4` == 1) or (`l3` == 1 and `l4` == 0) then
`1+` # make it 1
end
`Sb`
end
0 # start at 0
for '1' in stack 'b' do
`2*` # double existing
`l1+` # add in
end
end
function read_number
# does no error checking
`1` # sign
if `ln` == '-' then
`2-` # negative
adjust_index(1)
end
`0` # c num
while `ln` >= '0' and `ln` <= '9' do
`10*`
`ln48-+` # add in number
adjust_index(1)
end
#adjust_index(-1)
`*`
end

View File

@@ -0,0 +1,99 @@
state start
read_number()
`sm`
secret_number()
`lmlo+so`
`[.]n`
end
interrupt program_end
`10an`
`lop`
end
# thing on stack
function secret_number
for '!' in 0 to 2000 do
`lm64*`
mix_and_prune()
`lm32/`
mix_and_prune()
`lm2048*`
mix_and_prune()
#`lmn10an`
end
end
# thing on stack, number in m
function mix_and_prune
# mix
`lm`
xor()
# prune
`16777216%`
`sm`
end
# takes shit on the stack, leaves shit on the stack
function xor
`s1s2`
`_1Sb`
while `l1` != 0 or `l2` != 0 do
`l1 2~s3s1`
`l2 2~s4s2`
0 # this bit
if `l3l4+` == 1 then
`1+` # make it 1
end
`Sb`
end
0 # start at 0
for '1' in stack 'b' do
`2*` # double existing
`l1+` # add in
end
end
function read_number
# does no error checking
`1` # sign
if `ln` == '-' then
`2-` # negative
adjust_index(1)
end
`0` # c num
while `ln` >= '0' and `ln` <= '9' do
`10*`
`ln48-+` # add in number
adjust_index(1)
end
#adjust_index(-1)
`*`
end

View File

@@ -0,0 +1,106 @@
state start
read_number()
`sm`
secret_number()
`lmlo+so`
`[.]n`
end
interrupt program_end
`10an`
`lop`
end
# thing on stack
function secret_number
for '!' in 0 to 10 do
`lm10%sp` # previous
`lm64*`
mix_and_prune()
`lm32/`
mix_and_prune()
`lm2048*`
mix_and_prune()
# offset
`lm10%lp-p`
#`lmn10an`
end
end
# thing on stack, number in m
function mix_and_prune
# mix
`lm`
xor()
# prune
`16777216%`
`sm`
end
# takes shit on the stack, leaves shit on the stack
function xor
`s1s2`
`_1Sb`
while `l1` != 0 or `l2` != 0 do
`l1 2~s3s1`
`l2 2~s4s2`
0 # this bit
if `l3l4+` == 1 then
`1+` # make it 1
end
`Sb`
end
0 # start at 0
for '1' in stack 'b' do
`2*` # double existing
`l1+` # add in
end
end
function read_number
# does no error checking
`1` # sign
if `ln` == '-' then
`2-` # negative
adjust_index(1)
end
`0` # c num
while `ln` >= '0' and `ln` <= '9' do
`10*`
`ln48-+` # add in number
adjust_index(1)
end
#adjust_index(-1)
`*`
end

View File

@@ -0,0 +1,88 @@
state start
`ln`
'a'
`-`
`26*`
adjust_index(1)
`ln`
'a'
`-+`
`s1`
# past the '-'
adjust_index(2)
`ln`
'a'
`-`
`26*`
adjust_index(1)
`ln`
'a'
`-+`
`s2`
array_set('X', `l1676*l2+`, 1)
array_set('X', `l2676*l1+`, 1)
#`l1n[ ]nl2n10an`
adjust_index(1)
end
interrupt program_end
for '!' in 494 to 520 do
#for '!' in 495 to 496 do
#`l!26%`
#'a'
#`+an`
for '?' in 0 to 675 do
#`l?n`
#`[ ]n`
#array_get('X', `l?676*l!+`)
#`n`
#`10an`
if not (`l?` >= 494 and `l?` <= `l!`) and array_get('X', `l!676*l?+`) == 1 then
#`l?n[ ]n`
for '@' in `l?1+` to 676 do
if not (`l@` >= 494 and `l@` <= `l!`) then
if array_get('X', `l!676*l@+`) == 1 and array_get('X', `l?676*l@+`) == 1 then
#`[wow]n`
inc('o')
end
end
end
end
end
end
`lop`
end
# on stack
function print_node
`26~r`
'a'
`+an`
'a'
`+an`
`[ ]n`
end

View File

@@ -0,0 +1,117 @@
state start
`ln`
'a'
`-`
`26*`
adjust_index(1)
`ln`
'a'
`-+`
`s1`
# past the '-'
adjust_index(2)
`ln`
'a'
`-`
`26*`
adjust_index(1)
`ln`
'a'
`-+`
`s2`
array_get('X', `l1`)
`2l2^+`
`l1:X`
array_get('X', `l2`)
`2;1^+`
`l2:X`
array_set('Y', `l1`, 1)
array_set('Y', `l2`, 1)
#`l1n[ ]nl2n10an`
adjust_index(1)
end
interrupt program_end
0
for '!' in 0 to 676 do
if array_get('Y', `l!`) == 1 then
`2l!^+` # bitmap
end
end
`SP`
`0SR`
`0SX`
end
function bit_and
`s1s2`
0
for '.' in 0 to 676 do
if `l12l!^/2%` == 1 and `l22l!^/2%` == 1 then
`2l!^+`
end
end
end
function bit_or
`s1s2`
0
for '.' in 0 to 676 do
if `l12l!^/2%` == 1 or `l22l!^/2%` == 1 then
`2l!^+`
end
end
end
# R P X stacks
function bron_kerbosch
if `lP` == 0 and `lX` == 0 then
`LRSO`
`LPstLXst`
return
end
`0S!`
while `l!` < 676 do
if `lP`
`L!1+S!`
end
`LPstLXstLRstL!st`
end
# on stack
function print_node
`26~r`
'a'
`+an`
'a'
`+an`
`[ ]n`
end