Some more intcode

This commit is contained in:
Bill Rossi 2024-12-21 07:04:56 -05:00
parent 9f7f50260d
commit 5cc36fc79f
2 changed files with 106 additions and 6 deletions

View File

@ -9,10 +9,17 @@ int main() {
ic_load_rom_from_input();
IC *c = ic_new_computer();
ic_run(c);
c->input = 1;
//ic_run(c);
printf("Part 1: %d\n", c->output_buffer[c->output_buffer_count - 1]);
// printf("Part 2: %d\n", noun * 100 + verb);
//printf("Part 1: %d\n", c->output_buffer[c->output_buffer_count - 1]);
ic_reset(c);
ic_print(c);
c->input = 6;
ic_run(c);
ic_print(c);
printf("Part 2: %d\n", c->output_buffer[0]);
aoc_free();
return 0;

View File

@ -15,6 +15,7 @@ typedef struct IC {
int *data;
int program_counter;
bool halted;
int input;
int output_buffer[OUTPUT_BUFFER_LENGTH];
int output_buffer_count;
} IC;
@ -35,6 +36,10 @@ void ic_write(IC *c, int value) {
ic_poke(c, ic_peek(c, c->program_counter++), value);
}
void ic_jump(IC *c, int address) {
c->program_counter = address;
}
void ic_halt(IC *c) {
c->halted = true;
}
@ -68,7 +73,7 @@ void ic_instruction_multiply(IC *c, int modes) {
}
void ic_instruction_input(IC *c, int _modes) {
ic_write(c, 1);
ic_write(c, c->input);
}
void ic_instruction_output(IC *c, int modes) {
@ -84,16 +89,92 @@ void ic_instruction_output(IC *c, int modes) {
c->output_buffer[c->output_buffer_count++] = to_output;
}
void ic_instruction_jump_if_true(IC *c, int modes) {
int predicate_mode = modes % 10;
modes /= 10;
int predicate = ic_read(c);
if (predicate_mode == POSITION_MODE) predicate = ic_peek(c, predicate);
printf("Predicate: %d\n", predicate);
if (predicate == 0) {
ic_read(c);
return;
}
int pointer_mode = 0;//modes % 10;
modes /= 10;
int pointer = ic_read(c);
if (pointer_mode == POSITION_MODE) pointer = ic_peek(c, pointer);
ic_jump(c, pointer);
}
void ic_instruction_jump_if_false(IC *c, int modes) {
int predicate_mode = modes % 10;
modes /= 10;
int predicate = ic_read(c);
if (predicate_mode == POSITION_MODE) predicate = ic_peek(c, predicate);
if (predicate != 0) {
ic_read(c);
return;
}
int pointer_mode = 0;//modes % 10;
modes /= 10;
int pointer = ic_read(c);
if (pointer_mode == POSITION_MODE) pointer = ic_peek(c, pointer);
ic_jump(c, pointer);
}
void ic_instruction_less_than(IC *c, int modes) {
int x_mode = modes % 10;
modes /= 10;
int x = ic_read(c);
if (x_mode == POSITION_MODE) x = ic_peek(c, x);
int y_mode = modes % 10;
modes /= 10;
int y = ic_read(c);
if (y_mode == POSITION_MODE) y = ic_peek(c, y);
int output_mode = 1;//modes % 10;
modes /= 10;
int output = ic_read(c);
if (output_mode == POSITION_MODE) output = ic_peek(c, output);
if (x < y) ic_poke(c, output, 1);
else ic_poke(c, output, 0);
}
void ic_instruction_equals(IC *c, int modes) {
int x_mode = modes % 10;
modes /= 10;
int x = ic_read(c);
if (x_mode == POSITION_MODE) x = ic_peek(c, x);
int y_mode = modes % 10;
modes /= 10;
int y = ic_read(c);
if (y_mode == POSITION_MODE) y = ic_peek(c, y);
int output_mode = 1;//modes % 10;
modes /= 10;
int output = ic_read(c);
if (output_mode == POSITION_MODE) output = ic_peek(c, output);
if (x == y) ic_poke(c, output, 1);
else ic_poke(c, output, 0);
}
void ic_instruction_halt(IC *c, int _modes) {
ic_halt(c);
}
int ic_execute_instruction(IC *c) {
//ic_print(c);
int instruction = ic_read(c);
int opcode = instruction % 100;
int modes = instruction / 100;
// printf("Running %d: opcode %d with modes %d\n", instruction, opcode, modes);
// printf("PC: %d\n", c->program_counter - 1);
printf("Running %d: opcode %d with modes %d\n", instruction, opcode, modes);
printf("PC: %d\n", c->program_counter - 1);
switch(opcode) {
case 1:
ic_instruction_add(c, modes);
@ -107,6 +188,18 @@ int ic_execute_instruction(IC *c) {
case 4:
ic_instruction_output(c, modes);
break;
case 5:
ic_instruction_jump_if_true(c, modes);
break;
case 6:
ic_instruction_jump_if_false(c, modes);
break;
case 7:
ic_instruction_less_than(c, modes);
break;
case 8:
ic_instruction_equals(c, modes);
break;
case 99:
ic_instruction_halt(c, modes);
break;