From 5cc36fc79f2654f56082dad1f3b9df2fad22799a Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sat, 21 Dec 2024 07:04:56 -0500 Subject: [PATCH] Some more intcode --- c/2019/5/sunny_with_a_chance_of_asteroids.c | 13 ++- c/lib/intcode.h | 99 ++++++++++++++++++++- 2 files changed, 106 insertions(+), 6 deletions(-) diff --git a/c/2019/5/sunny_with_a_chance_of_asteroids.c b/c/2019/5/sunny_with_a_chance_of_asteroids.c index 9cccce1..596ae7d 100644 --- a/c/2019/5/sunny_with_a_chance_of_asteroids.c +++ b/c/2019/5/sunny_with_a_chance_of_asteroids.c @@ -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; diff --git a/c/lib/intcode.h b/c/lib/intcode.h index 646c23c..da1d155 100644 --- a/c/lib/intcode.h +++ b/c/lib/intcode.h @@ -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;