2019 day 5: fix wrong link in README, replace struct with typedefs

This commit is contained in:
2022-09-25 12:26:07 +02:00
parent b6af1f3edb
commit 3d18e36ff4
2 changed files with 29 additions and 22 deletions

View File

@@ -5,7 +5,7 @@ ship computer to support the Thermal Environment Supervision Terminal.
The Thermal Environment Supervision Terminal (TEST) starts by running a The Thermal Environment Supervision Terminal (TEST) starts by running a
/diagnostic program/ (your puzzle input). The TEST diagnostic program /diagnostic program/ (your puzzle input). The TEST diagnostic program
will run on [[file:2][your existing Intcode computer]] after a few will run on [[file:~/dev/advent-of-code/2019/day02][your existing Intcode computer]] after a few
modifications: modifications:
/First/, you'll need to add /two new instructions/: /First/, you'll need to add /two new instructions/:

View File

@@ -28,16 +28,27 @@ typedef enum {
} opcode_t; } opcode_t;
/** /**
* ops - array of op-codes mnemoand number of parameters * ops - array of op-codes, mnemo, and number of parameters
* @len: number of parameters * @op: An integer, the opcode
* @mnemo: op mnemo * @nargs: Opcode number of parameters (unused)
* @jump: Next instruction (usually @nargs + 1)
* @mnemo: Opcode mnemo (unused, for debug)
*/ */
static struct { typedef struct {
opcode_t op; int op;
u8 len; u8 nargs;
u8 next; u8 jump;
char *mnemo; char *mnemo;
} ops[] = { } ops_t;
#define MAXOPS 1024
typedef struct {
int length; /* total program length */
int cur; /* current position */
int mem [MAXOPS]; /* should really be dynamic */
} program_t;
static ops_t ops[] = {
[ADD] = { ADD, 3, 4, __stringify(ADD) }, [ADD] = { ADD, 3, 4, __stringify(ADD) },
[MUL] = { MUL, 3, 4, __stringify(MUL) }, [MUL] = { MUL, 3, 4, __stringify(MUL) },
[INP] = { INP, 1, 2, __stringify(INP) }, [INP] = { INP, 1, 2, __stringify(INP) },
@@ -49,12 +60,6 @@ static struct {
[HLT] = { HLT, 0, 1, __stringify(HLT) } [HLT] = { HLT, 0, 1, __stringify(HLT) }
}; };
#define MAXOPS 1024
struct program {
int length; /* total program length */
int cur; /* current position */
int mem [MAXOPS]; /* should really be dynamic */
};
static int _flag_pow10[] = {1, 100, 1000, 10000}; static int _flag_pow10[] = {1, 100, 1000, 10000};
#define OP(p, n) ((p->mem[n]) % 100) #define OP(p, n) ((p->mem[n]) % 100)
@@ -63,13 +68,15 @@ static int _flag_pow10[] = {1, 100, 1000, 10000};
#define INDIRECT(p, i) (DIRECT(p, DIRECT(p, i))) #define INDIRECT(p, i) (DIRECT(p, DIRECT(p, i)))
#define peek(p, n, i) (ISDIRECT(p, n, i)? DIRECT(p, n + i): INDIRECT(p, n + i)) #define peek(p, n, i) (ISDIRECT(p, n, i)? DIRECT(p, n + i): INDIRECT(p, n + i))
#define poke(p, n, i, val) do { INDIRECT(p, n + i) = val; } while (0) #define poke(p, n, i, val) do { \
INDIRECT(p, n + i) = val; } \
while (0)
static int run(struct program *p, int in) static int run(program_t *p, int in)
{ {
int out = -1; int out = -1;
while (1) { while (1) {
int op = OP(p, p->cur), _cur = p->cur; int op = OP(p, p->cur), cur = p->cur;
if (!(ops[op].op)) { if (!(ops[op].op)) {
fprintf(stderr, "PANIC: illegal instruction %d at %d.\n", op, p->cur); fprintf(stderr, "PANIC: illegal instruction %d at %d.\n", op, p->cur);
@@ -105,12 +112,12 @@ static int run(struct program *p, int in)
case HLT: case HLT:
return out; return out;
} }
if (p->cur == _cur) if (p->cur == cur)
p->cur += ops[op].next; p->cur += ops[op].jump;
} }
} }
static void parse(struct program *prog) static void parse(program_t *prog)
{ {
while (scanf("%d%*c", &prog->mem[prog->length++]) > 0) while (scanf("%d%*c", &prog->mem[prog->length++]) > 0)
; ;
@@ -125,7 +132,7 @@ static int usage(char *prg)
int main(int ac, char **av) int main(int ac, char **av)
{ {
int opt, part = 1, in = -1; int opt, part = 1, in = -1;
struct program p = { 0 }; program_t p = { 0 };
while ((opt = getopt(ac, av, "d:p:i:")) != -1) { while ((opt = getopt(ac, av, "d:p:i:")) != -1) {
switch (opt) { switch (opt) {