#include #include #include #include typedef struct { uint8_t before[4]; uint8_t opcode; uint8_t a, b, c; uint8_t after[4]; } Sample; int sample_read(Sample *sample, FILE *file) { int read = 0; read += fscanf(file, "Before: [%" SCNu8 ", %" SCNu8 ", %" SCNu8 ", %" SCNu8 "]\n", &sample->before[0], &sample->before[1], &sample->before[2], &sample->before[3]); read += fscanf(file, "%" SCNu8 " %" SCNu8 " %" SCNu8 " %" SCNu8 "\n", &sample->opcode, &sample->a, &sample->b, &sample->c); read += fscanf(file, "After: [%" SCNu8 ", %" SCNu8 ", %" SCNu8 ", %" SCNu8 "]\n\n", &sample->after[0], &sample->after[1], &sample->after[2], &sample->after[3]); return read == 12; } void sample_print(const Sample sample) { printf( "{\n" " before: [%d, %d, %d, %d],\n" " opcode: %d, a: %d, b: %d, c: %d,\n" " after: [%d, %d, %d, %d]\n" "}\n", sample.before[0], sample.before[1], sample.before[2], sample.before[3], sample.opcode, sample.a, sample.b, sample.c, sample.after[0], sample.after[1], sample.after[2], sample.after[3] ); } int sample_possible_opcodes(const Sample s) { int possible_opcodes = 0; // Addition if (s.after[s.c] == s.before[s.a] + s.before[s.b]) { printf("addr, "); possible_opcodes += 1; } if (s.after[s.c] == s.before[s.a] + s.b) { printf("addi, "); possible_opcodes += 1; } // Multiplication if (s.after[s.c] == s.before[s.a] * s.before[s.b]) { printf("mulr, "); possible_opcodes += 1; } if (s.after[s.c] == s.before[s.a] * s.b) { printf("muli, "); possible_opcodes += 1; } // Bitwise AND if (s.after[s.c] == (s.before[s.a] & s.before[s.b])) { printf("banr, "); possible_opcodes += 1; } if (s.after[s.c] == (s.before[s.a] & s.b)) { printf("bani, "); possible_opcodes += 1; } // Bitwise OR if (s.after[s.c] == (s.before[s.a] | s.before[s.b])) { printf("borr, "); possible_opcodes += 1; } if (s.after[s.c] == (s.before[s.a] | s.b)) { printf("bori, "); possible_opcodes += 1; } // Assignment if (s.after[s.c] == s.before[s.a]) { printf("setr, "); possible_opcodes += 1; } if (s.after[s.c] == s.a) { printf("seti, "); possible_opcodes += 1; } // Greater-than testing if (s.after[s.c] == (s.a > s.before[s.b])) { printf("gtir, "); possible_opcodes += 1; } if (s.after[s.c] == (s.before[s.a] > s.b)) { printf("gtri, "); possible_opcodes += 1; } if (s.after[s.c] == (s.before[s.a] > s.before[s.b])) { printf("gtrr, "); possible_opcodes += 1; } // Equality testing if (s.after[s.c] == (s.a == s.before[s.b])) { printf("eqir, "); possible_opcodes += 1; } if (s.after[s.c] == (s.before[s.a] == s.b)) { printf("eqri, "); possible_opcodes += 1; } if (s.after[s.c] == (s.before[s.a] == s.before[s.b])) { printf("eqrr, "); possible_opcodes += 1; } return possible_opcodes; } int main(int argc, char **argv) { if (argc != 2) { printf("Usage: %s [input]\n", argv[0]); return EXIT_FAILURE; } FILE *file = fopen(argv[1], "r"); Sample sample = {0}; int gt3 = 0, samples = 0; while (sample_read(&sample, file)) { sample_print(sample); int possible_opcodes = sample_possible_opcodes(sample); printf("%d possible opcodes.\n\n", possible_opcodes); if (possible_opcodes >= 3) { gt3 += 1; } samples += 1; } printf("%d samples\n", samples); printf("%d\n", gt3); fclose(file); return EXIT_SUCCESS; }