commit aeb75c79da07bbb6432c5d701acd094f39cb4877
parent d21b0e5339a4a2b0a4c1fb7c384f8b3c54de0ca9
Author: Louis Burda <quent.burda@gmail.com>
Date: Mon, 20 Mar 2023 01:44:25 +0100
Fixup day 10
Diffstat:
3 files changed, 49 insertions(+), 47 deletions(-)
diff --git a/10/Makefile b/10/Makefile
@@ -1,12 +0,0 @@
-CFLAGS = -g -I ../../libs/include -L ../../libs/build
-LDLIBS = -laoc -lm
-
-all: lib main
-
-clean:
- rm main
-
-lib:
- make -C ../../libs
-
-main: main.c ../../libs/build/libaoc.a
diff --git a/10/info.mk b/10/info.mk
@@ -0,0 +1,4 @@
+10_SRC = 10/main.c common/main.c common/aoc.c common/util.c
+10_SRC += lib/libhashmap/build/libhashmap.a lib/liballoc/build/liballoc.a
+10_HDR = common/aoc.h common/util.h
+10_LDLIBS = -lm
diff --git a/10/main.c b/10/main.c
@@ -1,9 +1,12 @@
+#include "allocator.h"
#include "aoc.h"
+#include "util.h"
#include "hashmap.h"
-#include <stdlib.h>
-#include <stdio.h>
#include <math.h>
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
struct asteroid {
int x, y;
@@ -19,14 +22,14 @@ struct asteroid *asteroids;
int width, height;
uint32_t
-dir_hasher(void *dp, int len)
+dir_hasher(const void *p, size_t len)
{
- struct dir *d;
+ const struct dir *dir;
uint32_t hash;
- d = dp;
- hash = ((uint16_t) d->dx & 0xFFFF);
- hash |= ((uint16_t) d->dy & 0xFFFF) << 16;
+ dir = p;
+ hash = ((uint16_t) dir->dx & 0xFFFF);
+ hash |= ((uint16_t) dir->dy & 0xFFFF) << 16;
return hash;
}
@@ -38,7 +41,8 @@ add_asteroid(int x, int y)
for (iter = &asteroids; *iter; iter = &((*iter)->next));
- *iter = CHKP(malloc(sizeof(struct asteroid)));
+ *iter = malloc(sizeof(struct asteroid));
+ assert(*iter != NULL);
(*iter)->x = x;
(*iter)->y = y;
(*iter)->next = NULL;
@@ -70,7 +74,7 @@ asteroids_init(void)
}
}
- ASSERT(width != 0 && height != 0);
+ assert(width != 0 && height != 0);
}
void
@@ -111,10 +115,11 @@ reduce_frac(struct dir *d)
void
get_vis(struct hashmap *hm, struct asteroid *a1)
{
- struct hashmap_key key;
struct hashmap_link *link;
struct asteroid *a2, *tmp;
struct dir dir, odir, *dira;
+ void *key, *value;
+ int rc;
for (a2 = asteroids; a2; a2 = a2->next) {
if (a1 == a2) continue;
@@ -123,15 +128,19 @@ get_vis(struct hashmap *hm, struct asteroid *a1)
dir.dy = a2->y - a1->y;
reduce_frac(&dir);
- key = HASHMAP_KEY(&dir, sizeof(dir));
- if (hashmap_get(hm, key, &link)) {
+ link = hashmap_get(hm, &dir, sizeof(dir));
+ if (link) {
tmp = link->value;
odir = (struct dir) { tmp->x - a1->x, tmp->y - a1->y };
dir = (struct dir) { a2->x - a1->x, a2->y - a1->y };
if (ABS(odir.dx) + ABS(odir.dy) > ABS(dir.dx) + ABS(dir.dy))
- link->value = a2;
+ link->value = memdup(a2, sizeof(struct asteroid));
} else {
- hashmap_set(hm, key, a2);
+ key = memdup(&dir, sizeof(struct dir));
+ value = memdup(a2, sizeof(struct asteroid));
+ rc = hashmap_set(hm, NULL, key, sizeof(struct dir),
+ value, sizeof(struct asteroid));
+ assert(!rc);
}
}
}
@@ -154,19 +163,20 @@ find_base(struct asteroid **a_out, int *vis_out)
struct hashmap dirmap;
int vis, maxvis;
- hashmap_init(&dirmap, 100, dir_hasher);
+ hashmap_init(&dirmap, 100, dir_hasher,
+ hashmap_default_keycmp, &stdlib_strict_heap_allocator);
maxvis = 0;
+ cand = NULL;
for (a1 = asteroids; a1; a1 = a1->next) {
- debug("Asteroid @ %i,%i => ", a1->x, a1->y);
+ aoc_debug("Asteroid @ %i,%i => ", a1->x, a1->y);
get_vis(&dirmap, a1);
vis = 0;
- iter = hashmap_iter();
- while (hashmap_next(&dirmap, &iter))
+ for (HASHMAP_ITER(&dirmap, &iter))
vis++;
+ aoc_debug("%i vis\n", vis);
- debug("%i vis\n", vis);
if (maxvis == 0 || vis > maxvis) {
maxvis = vis;
cand = a1;
@@ -175,8 +185,8 @@ find_base(struct asteroid **a_out, int *vis_out)
hashmap_clear(&dirmap);
}
- ASSERT(cand != NULL);
- hashmap_free(&dirmap);
+ assert(cand != NULL);
+ hashmap_deinit(&dirmap);
if (vis_out) *vis_out = maxvis;
if (a_out) *a_out = cand;
@@ -191,8 +201,8 @@ part1(void)
asteroids_init();
find_base(&a, &vis);
- debug("Best at %i,%i with %i vis\n", a->x, a->y, vis);
- aoc.answer = CHKP(aprintf("%i", vis));
+ aoc_debug("Best at %i,%i with %i vis\n", a->x, a->y, vis);
+ aoc.answer = aprintf("%i", vis);
aoc.solution = "299";
asteroids_free();
@@ -211,19 +221,18 @@ part2(void)
asteroids_init();
find_base(&base, NULL);
- hashmap_init(&dirmap, 100, dir_hasher);
+ hashmap_init(&dirmap, 100, dir_hasher,
+ hashmap_default_keycmp, &stdlib_strict_heap_allocator);
get_vis(&dirmap, base);
len = 0;
- iter = hashmap_iter();
- while (hashmap_next(&dirmap, &iter))
+ for (HASHMAP_ITER(&dirmap, &iter))
len++;
-
- list = CHKP(malloc(sizeof(struct asteroid *) * len));
-
- iter = hashmap_iter();
- for (i = 0; hashmap_next(&dirmap, &iter); i++)
- list[i] = iter.link->value;
+ list = malloc(sizeof(struct asteroid *) * len);
+ assert(list != NULL);
+ i = 0;
+ for (HASHMAP_ITER(&dirmap, &iter))
+ list[i++] = iter.link->value;
for (i = 0; i < len; i++) {
for (k = 0; k < len - 1; k++) {
@@ -237,12 +246,13 @@ part2(void)
prev = angle(list[0], base);
for (k = 0; k < len; k++) {
- debug("%i: %i %i %f\n", k+1, list[k]->x,
+ aoc_debug("%i: %i %i %f\n", k+1, list[k]->x,
list[k]->y, angle(list[k], base));
}
- aoc.answer = CHKP(aprintf("%i", list[199]->x * 100 + list[199]->y));
+ aoc.answer = aprintf("%i", list[199]->x * 100 + list[199]->y);
+ aoc.solution = "1419";
asteroids_free();
- hashmap_free(&dirmap);
+ hashmap_deinit(&dirmap);
}