commit ea1f13579150e3f377cccbe32ca770036901f677
parent 49719f031078c344de3c21a8ed4ed94f6e3705bb
Author: Louis Burda <quent.burda@gmail.com>
Date: Thu, 13 Apr 2023 10:16:06 -0400
Fix day 23
Diffstat:
2 files changed, 33 insertions(+), 8 deletions(-)
diff --git a/src/23/main.c b/src/23/main.c
@@ -6,6 +6,7 @@
#include <assert.h>
#include <stdbool.h>
+#include <stdint.h>
#include <stdlib.h>
struct packet {
@@ -90,9 +91,9 @@ send_packet(struct list *queues, struct maxint *val, int state, size_t ip)
packet = &nat_packet;
} else {
packet = malloc(sizeof(struct packet));
+ mi_init(&packet->x);
+ mi_init(&packet->y);
}
- mi_init(&packet->x);
- mi_init(&packet->y);
packet->src_ip = ip;
packet->dst_ip = dst;
packet->link = LIST_LINK_INIT;
@@ -164,6 +165,11 @@ part1(void)
feed_input(&icc[i], (mi_ul) i);
}
+ mi_init(&nat_packet.x);
+ mi_setv(&nat_packet.x, 0, MI_POS);
+ mi_init(&nat_packet.y);
+ mi_setv(&nat_packet.y, 0, MI_POS);
+
while (!nat_avail) {
aoc_debug("-- TICK --\n");
for (i = 0; i < 50; i++)
@@ -201,6 +207,12 @@ part2(void)
feed_input(&icc[i], (mi_ul) i);
}
+ mi_init(&nat_packet.x);
+ mi_setv(&nat_packet.x, 0, MI_POS);
+ mi_init(&nat_packet.y);
+ mi_setv(&nat_packet.y, 0, MI_POS);
+
+ last = SIZE_MAX;
while (1) {
aoc_debug("-- TICK --\n");
@@ -211,7 +223,13 @@ part2(void)
if (idle) {
assert(nat_avail);
packet = malloc(sizeof(struct packet));
- memcpy(packet, &nat_packet, sizeof(struct packet));
+ mi_init(&packet->x);
+ mi_set(&packet->x, &nat_packet.x);
+ mi_init(&packet->y);
+ mi_set(&packet->y, &nat_packet.y);
+ packet->dst_ip = nat_packet.dst_ip;
+ packet->src_ip = nat_packet.src_ip;
+ packet->link = LIST_LINK_INIT;
list_insert_back(&queues[0], &packet->link);
if (mi_cast_ul(&packet->y) == last) {
diff --git a/src/Makefile b/src/Makefile
@@ -20,7 +20,7 @@ endif
DAYS = $(shell seq 1 25 | xargs printf "%02i\n")
-all::
+all:: build run
include */info.mk
@@ -43,17 +43,22 @@ lib/libpq/build/libpq.a:
make -C lib/libpq build/libpq.a $(LIBPQ_ENV)
define make-day
-all:: $1/main
-run:: $1/run
-debug:: $1/debug
+
+build:: $1/main
$1/main: $($(1)_SRC) $($(1)_HDR)
$(CC) -o $1/main $($(1)_SRC) $(CFLAGS) $($(1)_CFLAGS) \
$(LDFLAGS) $($(1)_LDFLAGS) $(LDLIBS) $($(1)_LDLIBS)
+
+.PHONY: $1/run
+run:: $1/run
$1/run: $1/main
@echo "== day $1 =="
@echo -en "\npart 1: " && cd $1 && time ./main 1
@echo -en "\npart 2: " && cd $1 && time ./main 2
@echo ""
+
+.PHONY: $1/debug
+debug:: $1/debug
$1/debug: $1/main
@echo "== day $1 part 1 =="
@cd $1 && valgrind --error-exitcode=1 --leak-check=full \
@@ -61,7 +66,9 @@ $1/debug: $1/main
@echo "== day $1 part 2 =="
@cd $1 && valgrind --error-exitcode=1 --leak-check=full \
--show-leak-kinds=all ./main 2
+
endef
+
$(foreach day,$(DAYS),$(eval $(call make-day,$(day))))
clean:
@@ -73,4 +80,4 @@ cleanall: clean
make -C lib/libmaxint clean
make -C lib/liballoc clean
-.PHONY: all run debug clean cleanall
+.PHONY: all build run debug clean cleanall