commit a3143f8fe845bef1e9e0fa76d97df038a2e34b77
parent db4cc51666ac8f7a698d6f9989df882f5a1f82c7
Author: Louis Burda <quent.burda@gmail.com>
Date: Fri, 17 Mar 2023 20:08:49 +0100
Fixup day 7
Diffstat:
5 files changed, 67 insertions(+), 67 deletions(-)
diff --git a/07/Makefile b/07/Makefile
@@ -1,13 +0,0 @@
-CFLAGS = -g -I ../../libs/include -L ../../libs/build
-LDLIBS = -licc -laoc
-
-all: lib main
-
-clean:
- rm main
-
-lib:
- make -C ../../libs
-
-main: main.c ../../libs/build/*
- $(CC) -o $@ $< $(CFLAGS) $(LDLIBS)
diff --git a/07/info.mk b/07/info.mk
@@ -0,0 +1,3 @@
+07_SRC = 07/main.c common/main.c common/icc.c common/aoc.c common/util.c
+07_SRC += lib/libdvec/build/libdvec.a
+07_HDR = common/aoc.h common/icc.h common/util.h
diff --git a/07/main.c b/07/main.c
@@ -1,6 +1,9 @@
#include "aoc.h"
#include "icc.h"
+#include "util.h"
+#include <assert.h>
+#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
@@ -11,16 +14,16 @@ run_amp(struct icc *icc, int phase, int input, int *out)
int in_cnt, out_cnt;
in_cnt = out_cnt = 0;
- while (icc->status != ICC_HALT) {
+ while (icc->state != ICC_HALT) {
icc_step_inst(icc);
- switch (icc->status) {
+ switch (icc->state) {
case ICC_INPUT:
- ASSERT(in_cnt <= 1);
+ assert(in_cnt <= 1);
icc->in = (in_cnt == 0) ? phase : input;
in_cnt++;
break;
case ICC_OUTPUT:
- ASSERT(out_cnt == 0);
+ assert(out_cnt == 0);
*out = icc->out;
out_cnt++;
break;
@@ -29,13 +32,13 @@ run_amp(struct icc *icc, int phase, int input, int *out)
}
void
-bf_phase(struct icc *icc, void *inst, int *state,
+bf_phase(struct icc *icc, struct dvec *inst, int *state,
int max_depth, int depth, int in, int *max, int *maxstate)
{
int i, k, out;
if (depth == max_depth) {
- debug("SETTING %i%i%i%i%i -> %i\n", state[0], state[1],
+ aoc_debug("SETTING %i%i%i%i%i -> %i\n", state[0], state[1],
state[2], state[3], state[4], in);
if (*max == 0 || in > *max) {
*max = in;
@@ -52,20 +55,21 @@ bf_phase(struct icc *icc, void *inst, int *state,
state[depth] = i;
icc_reset(icc, inst);
- debug("START AMP %i (%i):\n", depth, i);
+ aoc_debug("START AMP %i (%i):\n", depth, i);
run_amp(icc, i, in, &out);
- debug("END AMP %i (%i): %i -> %i\n", depth, i, in, out);
+ aoc_debug("END AMP %i (%i): %i -> %i\n", depth, i, in, out);
bf_phase(icc, inst, state, max_depth, depth + 1, out, max, maxstate);
}
}
int
-run_phase_loop(struct icc *iccs, void *inst, int *state, int n)
+run_phase_loop(struct icc *iccs, struct dvec *inst, int *state, int n)
{
int i, passes, done;
int *ins, out, in_cnt;
- ins = CHKP(malloc(n * sizeof(int)));
+ ins = malloc(n * sizeof(int));
+ assert(ins != NULL);
for (i = 0; i < n; i++)
icc_reset(&iccs[i], inst);
@@ -75,10 +79,10 @@ run_phase_loop(struct icc *iccs, void *inst, int *state, int n)
while (!done) {
for (i = 0; i < n; i++) {
in_cnt = 0;
- debug("START AMP %i (%i):\n", i, state[i]);
- while (iccs[i].status != ICC_HALT) {
+ aoc_debug("START AMP %i (%i):\n", i, state[i]);
+ while (iccs[i].state != ICC_HALT) {
icc_step_inst(&iccs[i]);
- switch (iccs[i].status) {
+ switch (iccs[i].state) {
case ICC_INPUT:
iccs[i].in = (!passes && !in_cnt) ? state[i] : ins[i];
in_cnt++;
@@ -87,12 +91,12 @@ run_phase_loop(struct icc *iccs, void *inst, int *state, int n)
ins[(i+1) % n] = iccs[i].out;
break;
}
- if (iccs[i].status == ICC_OUTPUT)
+ if (iccs[i].state == ICC_OUTPUT)
break;
}
- if (iccs[i].status == ICC_HALT)
+ if (iccs[i].state == ICC_HALT)
done = 1;
- debug("END AMP %i (%i): %i -> %i\n", i,
+ aoc_debug("END AMP %i (%i): %i -> %i\n", i,
state[i], ins[i], ins[(i+1) % n]);
}
passes += 1;
@@ -112,7 +116,7 @@ bf_phase_loop(int *state, int max_depth, int depth,
if (depth == max_depth) {
out = run_phase_loop(iccs, inst, state, max_depth);
- debug("SETTING %i%i%i%i%i -> %i\n", state[0], state[1],
+ aoc_debug("SETTING %i%i%i%i%i -> %i\n", state[0], state[1],
state[2], state[3], state[4], out);
if (*max == 0 || out > *max) {
*max = out;
@@ -131,56 +135,60 @@ bf_phase_loop(int *state, int max_depth, int depth,
}
}
-// TODO: create better disassembly (showing values in position mode and values received on input)
-// TODO: add icc initializer from instruction memvec
-// TODO: add function to parse input instructions into singe memvec
-// TODO: pad icc debug op output and add operation specific pseudocode
-
void
part1(void)
{
struct icc icc;
- void *inst;
+ struct dvec inst;
int max, state[5], maxstate[5];
- ASSERT(icc_init(&icc) == OK);
- ASSERT(icc_parse_inst(&icc, aoc.input, aoc.input_size) == OK);
- inst = icc_inst_copy(&icc);
+ icc_init(&icc);
+ icc_parse_inst(&icc, aoc.input, aoc.input_size);
+
+ dvec_init(&inst, 1, 0);
+ dvec_copy(&inst, &icc.instructions);
max = 0;
- bf_phase(&icc, inst, state, 5, 0, 0, &max, maxstate);
+ bf_phase(&icc, &inst, state, 5, 0, 0, &max, maxstate);
- debug("\nMAX SETTING: %i%i%i%i%i\n", maxstate[0], maxstate[1],
+ aoc_debug("\nMAX SETTING: %i%i%i%i%i\n", maxstate[0], maxstate[1],
maxstate[2], maxstate[3], maxstate[4]);
- aoc.answer = CHKP(aprintf("%i", max));
+
+ aoc.answer = aprintf("%i", max);
aoc.solution = "24625";
- free(inst);
- icc_free(&icc);
+ dvec_deinit(&inst);
+ icc_deinit(&icc);
}
void
part2(void)
{
struct icc iccs[5];
- void *inst;
+ struct dvec inst;
int i, max, state[5], maxstate[5];
- ASSERT(icc_init(&iccs[0]) == OK);
- ASSERT(icc_parse_inst(&iccs[0], aoc.input, aoc.input_size) == OK);
- inst = icc_inst_copy(&iccs[0]);
- for (i = 1; i < 5; i++)
+ icc_init(&iccs[0]);
+ icc_parse_inst(&iccs[0], aoc.input, aoc.input_size);
+
+ dvec_init(&inst, 1, 0);
+ dvec_copy(&inst, &iccs[0].instructions);
+
+ for (i = 1; i < 5; i++) {
+ icc_init(&iccs[i]);
icc_copy(&iccs[i], &iccs[0]);
+ }
max = 0;
- bf_phase_loop(state, 5, 0, iccs, inst, &max, maxstate);
+ bf_phase_loop(state, 5, 0, iccs, &inst, &max, maxstate);
- debug("\nMAX SETTING: %i%i%i%i%i\n", maxstate[0], maxstate[1],
+ aoc_debug("\nMAX SETTING: %i%i%i%i%i\n", maxstate[0], maxstate[1],
maxstate[2], maxstate[3], maxstate[4]);
- aoc.answer = CHKP(aprintf("%i", max));
+
+ aoc.answer = aprintf("%i", max);
aoc.solution = "36497698";
- free(inst);
+ dvec_deinit(&inst);
for (i = 0; i < 5; i++)
- icc_free(&iccs[i]);
+ icc_deinit(&iccs[i]);
}
diff --git a/common/icc.c b/common/icc.c
@@ -2,6 +2,8 @@
#include "aoc.h"
#include "util.h"
#include "dvec.h"
+
+#include <assert.h>
#include <stdbool.h>
const char *icc_err[] = {
@@ -30,7 +32,7 @@ icc_init(struct icc *icc)
icc->line_terminated = true;
rc = dvec_init(&icc->instructions, sizeof(int), 0);
- if (rc) die("icc: dvec_init: failed");
+ assert(!rc);
}
void
@@ -60,7 +62,7 @@ icc_reset(struct icc *icc, struct dvec *inst)
icc->base = 0;
if (inst) {
rc = dvec_copy(&icc->instructions, inst);
- if (rc) die("icc: icc_reset: dvec_copy");
+ assert(!rc);
}
}
@@ -78,7 +80,7 @@ icc_parse_inst(struct icc *icc, const char *str, size_t len)
while (readtok(buf, sizeof(buf), ',', &pos, end)) {
val = parsei64(buf);
slot = dvec_add_slot(&icc->instructions, &rc);
- if (rc) die("icc: dvec_add_slot");
+ assert(slot);
*slot = val;
}
}
@@ -118,7 +120,7 @@ icc_debug_op_main(struct icc *icc, const char *opstr, int n)
if (!aoc.debug) return;
rc = icc_get_inst(icc, icc->rip, &inst);
- if (rc) die("icc: debug: oob rip");
+ assert(!rc);
fprintf(stderr, "%s ", opstr);
icc->line_terminated = false;
@@ -126,7 +128,7 @@ icc_debug_op_main(struct icc *icc, const char *opstr, int n)
for (i = 1; i <= n; i++) {
if (i > 1) fprintf(stderr, ", ");
rc = icc_get_inst(icc, icc->rip + i, &val);
- if (rc) die("icc: debug: oob param %i", i);
+ assert(!rc);
switch (icc_param_mode(inst, i)) {
case ICC_PARAM_IMM:
fprintf(stderr, "%i", val);
@@ -137,7 +139,7 @@ icc_debug_op_main(struct icc *icc, const char *opstr, int n)
else fprintf(stderr, "[%i]=?", val);
break;
default:
- die("icc: unknown parmeter mode");
+ assert(0);
}
}
}
@@ -151,7 +153,7 @@ icc_debug_op_post(struct icc *icc, int dst)
if (!aoc.debug) return;
rc = icc_get_inst(icc, icc->rip, &inst);
- if (rc) die("icc: debug: oob rip");
+ assert(!rc);
fprintf(stderr, " -> ");
@@ -459,8 +461,7 @@ icc_set_inst(struct icc *icc, size_t addr, int val)
if (addr >= icc->instructions.len) {
icc->write_addr = addr;
- if (icc->abort_on_err)
- die("icc: oob write");
+ assert(icc->abort_on_err);
return ICC_OOB_WRITE;
}
@@ -475,8 +476,7 @@ icc_get_inst(struct icc *icc, size_t addr, int *out)
{
if (addr >= icc->instructions.len) {
icc->read_addr = addr;
- if (icc->abort_on_err)
- die("icc: oob read");
+ assert(icc->abort_on_err);
return ICC_OOB_READ;
}
@@ -519,7 +519,7 @@ icc_get_param(struct icc *icc, int param, int *out)
*out = icc->base + val;
break;
default:
- die("icc: unknown parmeter mode");
+ assert(0);
};
return 0;
diff --git a/lib/libdvec/src/dvec.c b/lib/libdvec/src/dvec.c
@@ -82,10 +82,10 @@ dvec_copy(struct dvec *dst, struct dvec *src)
{
int rc;
+ dst->dsize = src->dsize;
rc = dvec_reserve(dst, src->len);
if (rc) return rc;
- dst->dsize = src->dsize;
dst->len = src->len;
memcpy(dst->data, src->data, src->len * src->dsize);
@@ -119,6 +119,7 @@ dvec_reserve(struct dvec *dvec, size_t len)
dvec->cap *= 2;
if (len > dvec->cap) dvec->cap = len;
+
alloc = realloc(dvec->data, dvec->cap * dvec->dsize);
if (!alloc) return -errno;
dvec->data = alloc;
@@ -140,6 +141,7 @@ dvec_shrink(struct dvec *dvec)
}
dvec->cap = dvec->len;
+
alloc = realloc(dvec->data, dvec->cap * dvec->dsize);
if (!alloc) return -errno;
dvec->data = alloc;