From fed4285a51b874113c41d9a9e78810640d71e5dc Mon Sep 17 00:00:00 2001 From: thing1 Date: Wed, 12 Mar 2025 11:30:18 +0000 Subject: to much work --- comp/work/46/test.mac | 3 +++ comp/work/46/turing | Bin 0 -> 20304 bytes comp/work/46/turing.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 comp/work/46/test.mac create mode 100755 comp/work/46/turing create mode 100644 comp/work/46/turing.c (limited to 'comp/work/46') diff --git a/comp/work/46/test.mac b/comp/work/46/test.mac new file mode 100644 index 0000000..3b3dca3 --- /dev/null +++ b/comp/work/46/test.mac @@ -0,0 +1,3 @@ +0 1 0 0 1 0 +0 0 1 0 1 0 +1 * 1 0 0 1 diff --git a/comp/work/46/turing b/comp/work/46/turing new file mode 100755 index 0000000..2b60bfe Binary files /dev/null and b/comp/work/46/turing differ diff --git a/comp/work/46/turing.c b/comp/work/46/turing.c new file mode 100644 index 0000000..cc41451 --- /dev/null +++ b/comp/work/46/turing.c @@ -0,0 +1,69 @@ +#include +#include +#include +#include + +typedef struct transfer { + int id; + char input; + int next; + char output; + int move; + bool end; +} transfer; + +transfer transfers[128]; +int transferptr = 0; +int count = 0; + +char tape[4096] = {0}; +char *head = tape; + +int main() { + FILE *f = fopen("test.mac", "r"); + + strcat(tape, "1110"); + + char *line = malloc(128); + while (fgets(line, 128, f) != NULL) { + char *tok = strtok(line, " "); // input + transfers[count].id = atoi(tok); + + tok = strtok(NULL, " "); + if (tok[0] == '*') transfers[count].input = -1; + else transfers[count].input = tok[0]; + + tok = strtok(NULL, " "); // next + transfers[count].next = atoi(tok); + + tok = strtok(NULL, " "); // output + transfers[count].output = tok[0]; + + tok = strtok(NULL, " "); // offset + transfers[count].move = atoi(tok); + + tok = strtok(NULL, " "); // end + transfers[count].end = atoi(tok); // must be 1 or 0 + + count++; + + line = malloc(128); + } + + for (;;) { + for (int i = 0; i < count; i++) { + if (transfers[i].id == transferptr) { + if (transfers[i].input == -1 || *head == transfers[i].input) { + *head = transfers[i].output; + head += transfers[i].move; + transferptr = transfers[i].next; + if (transfers[transferptr].end) break; + break; + } + } + } + } + printf("%s\n", tape); + return 0; +} + -- cgit v1.2.3