diff --git a/.gitignore b/.gitignore index 4c6f485..e0dbac3 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ */main +day10p1/out/*.pbm diff --git a/day10p1/Makefile b/day10p1/Makefile new file mode 100644 index 0000000..48fbe3b --- /dev/null +++ b/day10p1/Makefile @@ -0,0 +1,16 @@ +CFLAGS := -Wall -Wextra -std=c17 -D_GNU_SOURCE + +LIBS := glib-2.0 +CFLAGS += $(shell pkg-config --cflags $(LIBS)) +LDLIBS := $(shell pkg-config --libs $(LIBS)) + +.PHONY: all +all: main out/ + +.PHONY: clean +clean: + rm -f main out/* + +out/: + mkdir -p $@ + touch $@/.gitkeep diff --git a/day10p1/input b/day10p1/input new file mode 100644 index 0000000..a50b9e9 --- /dev/null +++ b/day10p1/input @@ -0,0 +1,324 @@ +position=<-30509, 41062> velocity=< 3, -4> +position=<-50990, -20297> velocity=< 5, 2> +position=<-50968, 51288> velocity=< 5, -5> +position=<-51003, -40754> velocity=< 5, 4> +position=<-51011, 10375> velocity=< 5, -1> +position=< 30840, 41058> velocity=<-3, -4> +position=<-30546, 51284> velocity=< 3, -5> +position=<-10090, 41065> velocity=< 1, -4> +position=<-51024, -40755> velocity=< 5, 4> +position=< 30832, -40757> velocity=<-3, 4> +position=< 20597, -20297> velocity=<-2, 2> +position=<-50964, 51292> velocity=< 5, -5> +position=<-20303, 20608> velocity=< 2, -2> +position=< 10351, 51292> velocity=<-1, -5> +position=< 30792, 41065> velocity=<-3, -4> +position=<-51008, 30833> velocity=< 5, -3> +position=< 41023, 51287> velocity=<-4, -5> +position=< 41067, -50987> velocity=<-4, 5> +position=<-50966, 41060> velocity=< 5, -4> +position=< 30792, -30527> velocity=<-3, 3> +position=< 51246, 10375> velocity=<-5, -1> +position=< 20586, 30831> velocity=<-2, -3> +position=<-40789, -10071> velocity=< 4, 1> +position=< 51248, 30833> velocity=<-5, -3> +position=<-10071, -50987> velocity=< 1, 5> +position=< 10378, 30831> velocity=<-1, -3> +position=< 20581, -50978> velocity=<-2, 5> +position=<-10116, -50984> velocity=< 1, 5> +position=<-20335, -10073> velocity=< 2, 1> +position=<-50968, -30529> velocity=< 5, 3> +position=< 51267, -20299> velocity=<-5, 2> +position=<-51016, -20306> velocity=< 5, 2> +position=<-10084, 41057> velocity=< 1, -4> +position=< 20589, -30526> velocity=<-2, 3> +position=< 30849, 41065> velocity=<-3, -4> +position=< 10383, 41065> velocity=<-1, -4> +position=< 41059, 51286> velocity=<-4, -5> +position=< 41022, -50987> velocity=<-4, 5> +position=< 20600, 30838> velocity=<-2, -3> +position=<-20335, -50981> velocity=< 2, 5> +position=<-20285, 30838> velocity=< 2, -3> +position=<-10073, 10377> velocity=< 1, -1> +position=< 41069, -50978> velocity=<-4, 5> +position=<-30567, -50987> velocity=< 3, 5> +position=<-20291, -30524> velocity=< 2, 3> +position=<-10076, 51287> velocity=< 1, -5> +position=<-40788, 51287> velocity=< 4, -5> +position=< 30808, 10378> velocity=<-3, -1> +position=<-30562, -40759> velocity=< 3, 4> +position=<-51016, 30833> velocity=< 5, -3> +position=< 20565, -20302> velocity=<-2, 2> +position=<-51019, 41056> velocity=< 5, -4> +position=< 30802, 10378> velocity=<-3, -1> +position=< 51307, 41059> velocity=<-5, -4> +position=< 51265, -50982> velocity=<-5, 5> +position=< 41080, -50980> velocity=<-4, 5> +position=< 30813, 10378> velocity=<-3, -1> +position=< 30809, -10078> velocity=<-3, 1> +position=< 10346, -20299> velocity=<-1, 2> +position=<-51016, 10381> velocity=< 5, -1> +position=<-10081, -20297> velocity=< 1, 2> +position=< 51265, -40755> velocity=<-5, 4> +position=< 51303, 20602> velocity=<-5, -2> +position=<-50992, -40755> velocity=< 5, 4> +position=< 41059, 51291> velocity=<-4, -5> +position=< 10370, -30529> velocity=<-1, 3> +position=<-30549, -10071> velocity=< 3, 1> +position=<-20308, -20297> velocity=< 2, 2> +position=< 10341, 51287> velocity=<-1, -5> +position=< 41079, -40756> velocity=<-4, 4> +position=< 10339, 51292> velocity=<-1, -5> +position=<-20283, 30829> velocity=< 2, -3> +position=< 41067, -30530> velocity=<-4, 3> +position=<-10105, -30531> velocity=< 1, 3> +position=<-40765, -30527> velocity=< 4, 3> +position=< 10356, 51288> velocity=<-1, -5> +position=< 51278, -10073> velocity=<-5, 1> +position=<-51016, -20298> velocity=< 5, 2> +position=<-40780, -30528> velocity=< 4, 3> +position=<-30522, -50980> velocity=< 3, 5> +position=<-40776, 51289> velocity=< 4, -5> +position=< 20597, -50984> velocity=<-2, 5> +position=<-10076, -10079> velocity=< 1, 1> +position=< 51294, -50978> velocity=<-5, 5> +position=< 41051, 30836> velocity=<-4, -3> +position=< 41028, -10075> velocity=<-4, 1> +position=<-50973, 10384> velocity=< 5, -1> +position=< 51246, -20304> velocity=<-5, 2> +position=< 30843, 20611> velocity=<-3, -2> +position=<-40797, 10378> velocity=< 4, -1> +position=< 51254, 20611> velocity=<-5, -2> +position=< 41052, 51292> velocity=<-4, -5> +position=< 10343, -20297> velocity=<-1, 2> +position=<-30570, 20609> velocity=< 3, -2> +position=<-40753, 51284> velocity=< 4, -5> +position=< 20581, 41061> velocity=<-2, -4> +position=<-30514, -20304> velocity=< 3, 2> +position=< 51246, -30527> velocity=<-5, 3> +position=< 10378, 30836> velocity=<-1, -3> +position=< 51290, 20603> velocity=<-5, -2> +position=< 51247, 51292> velocity=<-5, -5> +position=<-51008, -20299> velocity=< 5, 2> +position=<-30553, 30834> velocity=< 3, -3> +position=< 51257, -30526> velocity=<-5, 3> +position=< 41060, 10380> velocity=<-4, -1> +position=<-30511, 20602> velocity=< 3, -2> +position=< 10367, -50978> velocity=<-1, 5> +position=< 41051, 41056> velocity=<-4, -4> +position=<-40777, 20603> velocity=< 4, -2> +position=< 30853, 10380> velocity=<-3, -1> +position=<-10112, 41065> velocity=< 1, -4> +position=<-40757, 20609> velocity=< 4, -2> +position=< 51303, 20606> velocity=<-5, -2> +position=<-51022, 41065> velocity=< 5, -4> +position=<-10106, 10378> velocity=< 1, -1> +position=< 41024, 30829> velocity=<-4, -3> +position=<-50965, -50987> velocity=< 5, 5> +position=< 51307, -50985> velocity=<-5, 5> +position=< 51270, -30532> velocity=<-5, 3> +position=<-51014, 30835> velocity=< 5, -3> +position=< 10354, -30526> velocity=<-1, 3> +position=<-40786, 41058> velocity=< 4, -4> +position=<-10095, -30524> velocity=< 1, 3> +position=<-40797, -10072> velocity=< 4, 1> +position=< 20621, 20606> velocity=<-2, -2> +position=< 10346, -10079> velocity=<-1, 1> +position=< 20569, -20297> velocity=<-2, 2> +position=< 51282, 41065> velocity=<-5, -4> +position=< 10398, -40751> velocity=<-1, 4> +position=< 20567, -20306> velocity=<-2, 2> +position=<-20282, -20305> velocity=< 2, 2> +position=<-40795, 10379> velocity=< 4, -1> +position=< 51303, 51287> velocity=<-5, -5> +position=<-10107, 41061> velocity=< 1, -4> +position=< 10338, -20305> velocity=<-1, 2> +position=< 20584, 51283> velocity=<-2, -5> +position=<-20306, -20297> velocity=< 2, 2> +position=< 41027, 10379> velocity=<-4, -1> +position=< 41040, -30526> velocity=<-4, 3> +position=< 51254, -20301> velocity=<-5, 2> +position=< 30840, -10076> velocity=<-3, 1> +position=< 10370, 10376> velocity=<-1, -1> +position=< 30817, 51292> velocity=<-3, -5> +position=<-50968, -30527> velocity=< 5, 3> +position=< 30812, -30528> velocity=<-3, 3> +position=<-30557, 20611> velocity=< 3, -2> +position=< 20575, 41062> velocity=<-2, -4> +position=<-40763, -10070> velocity=< 4, 1> +position=< 30851, 41060> velocity=<-3, -4> +position=< 30795, -20297> velocity=<-3, 2> +position=< 30848, 20605> velocity=<-3, -2> +position=<-40757, -10078> velocity=< 4, 1> +position=< 10362, -10070> velocity=<-1, 1> +position=<-51003, -10074> velocity=< 5, 1> +position=< 20581, -20298> velocity=<-2, 2> +position=< 30792, -50981> velocity=<-3, 5> +position=<-50991, -30524> velocity=< 5, 3> +position=<-20319, 41062> velocity=< 2, -4> +position=<-10072, -50986> velocity=< 1, 5> +position=< 30804, 41057> velocity=<-3, -4> +position=<-20303, -50984> velocity=< 2, 5> +position=< 41047, 51292> velocity=<-4, -5> +position=< 41038, -20306> velocity=<-4, 2> +position=<-20284, 41065> velocity=< 2, -4> +position=< 41067, -20298> velocity=<-4, 2> +position=<-30509, 51290> velocity=< 3, -5> +position=<-50965, -50983> velocity=< 5, 5> +position=< 20621, 30837> velocity=<-2, -3> +position=< 30796, -10079> velocity=<-3, 1> +position=<-50999, -40751> velocity=< 5, 4> +position=<-20339, 10379> velocity=< 2, -1> +position=< 20589, -10077> velocity=<-2, 1> +position=<-40754, -30526> velocity=< 4, 3> +position=< 41027, -30530> velocity=<-4, 3> +position=< 51278, -50979> velocity=<-5, 5> +position=< 51304, -30533> velocity=<-5, 3> +position=<-30533, 30838> velocity=< 3, -3> +position=< 10338, 51286> velocity=<-1, -5> +position=<-40747, 51292> velocity=< 4, -5> +position=<-10071, -20297> velocity=< 1, 2> +position=< 41061, -50981> velocity=<-4, 5> +position=<-30511, -50987> velocity=< 3, 5> +position=<-50964, 10375> velocity=< 5, -1> +position=<-30546, 41059> velocity=< 3, -4> +position=< 51248, 41065> velocity=<-5, -4> +position=< 20589, -20306> velocity=<-2, 2> +position=< 10338, 30837> velocity=<-1, -3> +position=< 10338, -40753> velocity=<-1, 4> +position=<-40789, -30531> velocity=< 4, 3> +position=<-40789, -50984> velocity=< 4, 5> +position=< 51294, -30528> velocity=<-5, 3> +position=<-20303, 41063> velocity=< 2, -4> +position=<-10071, 10384> velocity=< 1, -1> +position=<-30529, 30833> velocity=< 3, -3> +position=< 41075, -20298> velocity=<-4, 2> +position=< 41078, -30529> velocity=<-4, 3> +position=< 51262, 30831> velocity=<-5, -3> +position=< 20613, 20602> velocity=<-2, -2> +position=<-50968, 41063> velocity=< 5, -4> +position=<-30512, -40756> velocity=< 3, 4> +position=< 51304, -50987> velocity=<-5, 5> +position=< 30800, -20297> velocity=<-3, 2> +position=< 10370, -20303> velocity=<-1, 2> +position=<-20282, 30837> velocity=< 2, -3> +position=<-20335, -50986> velocity=< 2, 5> +position=<-50964, -30524> velocity=< 5, 3> +position=<-20319, -40756> velocity=< 2, 4> +position=< 10350, 51291> velocity=<-1, -5> +position=<-51024, 41064> velocity=< 5, -4> +position=<-40748, 20611> velocity=< 4, -2> +position=<-51000, 30837> velocity=< 5, -3> +position=<-10097, 30834> velocity=< 1, -3> +position=< 20613, -20305> velocity=<-2, 2> +position=< 51258, 41057> velocity=<-5, -4> +position=<-40757, -50982> velocity=< 4, 5> +position=< 10388, -20297> velocity=<-1, 2> +position=<-51003, -20297> velocity=< 5, 2> +position=<-10068, 41062> velocity=< 1, -4> +position=< 10359, -20304> velocity=<-1, 2> +position=<-10073, 51290> velocity=< 1, -5> +position=< 30840, -20304> velocity=<-3, 2> +position=< 30800, 30834> velocity=<-3, -3> +position=< 10394, 30838> velocity=<-1, -3> +position=<-40794, -20306> velocity=< 4, 2> +position=<-51016, -30528> velocity=< 5, 3> +position=<-10067, 10384> velocity=< 1, -1> +position=< 41067, -10071> velocity=<-4, 1> +position=<-10076, -50982> velocity=< 1, 5> +position=<-40741, 10376> velocity=< 4, -1> +position=< 51247, -20302> velocity=<-5, 2> +position=<-10076, -20305> velocity=< 1, 2> +position=<-50976, -20302> velocity=< 5, 2> +position=< 41036, 30830> velocity=<-4, -3> +position=<-50976, -40758> velocity=< 5, 4> +position=<-10100, -40753> velocity=< 1, 4> +position=<-30530, 51288> velocity=< 3, -5> +position=< 20613, 51286> velocity=<-2, -5> +position=<-20311, 10380> velocity=< 2, -1> +position=< 20570, -40760> velocity=<-2, 4> +position=< 41040, -40756> velocity=<-4, 4> +position=<-30530, 51292> velocity=< 3, -5> +position=< 20613, 10381> velocity=<-2, -1> +position=<-20287, -10074> velocity=< 2, 1> +position=< 30849, -10079> velocity=<-3, 1> +position=< 41027, 41056> velocity=<-4, -4> +position=< 51287, 51287> velocity=<-5, -5> +position=<-50971, -30524> velocity=< 5, 3> +position=<-40776, -50980> velocity=< 4, 5> +position=< 10395, 51283> velocity=<-1, -5> +position=< 10354, -20300> velocity=<-1, 2> +position=< 10378, 20603> velocity=<-1, -2> +position=< 20597, -50985> velocity=<-2, 5> +position=<-30570, -50987> velocity=< 3, 5> +position=< 20609, 20610> velocity=<-2, -2> +position=< 41062, -50980> velocity=<-4, 5> +position=< 41064, -10079> velocity=<-4, 1> +position=< 30792, -30533> velocity=<-3, 3> +position=< 10362, -30530> velocity=<-1, 3> +position=<-20282, 10380> velocity=< 2, -1> +position=< 30853, 41057> velocity=<-3, -4> +position=< 41027, -40756> velocity=<-4, 4> +position=<-20322, 51285> velocity=< 2, -5> +position=<-30558, -30532> velocity=< 3, 3> +position=< 20623, -20302> velocity=<-2, 2> +position=< 10395, 10379> velocity=<-1, -1> +position=< 51295, 41065> velocity=<-5, -4> +position=<-10116, 30833> velocity=< 1, -3> +position=<-40741, 51286> velocity=< 4, -5> +position=< 10362, 10380> velocity=<-1, -1> +position=< 30816, -10071> velocity=<-3, 1> +position=< 30803, 20604> velocity=<-3, -2> +position=< 30834, 20605> velocity=<-3, -2> +position=<-30534, 41065> velocity=< 3, -4> +position=<-50984, 20608> velocity=< 5, -2> +position=< 51294, -50981> velocity=<-5, 5> +position=< 10371, -40751> velocity=<-1, 4> +position=< 41059, -50978> velocity=<-4, 5> +position=<-40797, -10078> velocity=< 4, 1> +position=< 10354, 20605> velocity=<-1, -2> +position=<-50992, -30525> velocity=< 5, 3> +position=<-30554, -50983> velocity=< 3, 5> +position=< 30795, -30529> velocity=<-3, 3> +position=<-10055, -30526> velocity=< 1, 3> +position=<-50992, -40759> velocity=< 5, 4> +position=<-20295, -40755> velocity=< 2, 4> +position=<-20322, -10071> velocity=< 2, 1> +position=< 41060, 51288> velocity=<-4, -5> +position=<-51011, 30838> velocity=< 5, -3> +position=< 30819, -20297> velocity=<-3, 2> +position=< 10359, 30837> velocity=<-1, -3> +position=< 30801, 10379> velocity=<-3, -1> +position=< 10394, -50982> velocity=<-1, 5> +position=<-30546, -20303> velocity=< 3, 2> +position=<-30562, -20304> velocity=< 3, 2> +position=<-20339, 41056> velocity=< 2, -4> +position=<-50964, -20302> velocity=< 5, 2> +position=< 10343, 20611> velocity=<-1, -2> +position=< 10370, -20301> velocity=<-1, 2> +position=<-20343, 20606> velocity=< 2, -2> +position=< 30832, 10383> velocity=<-3, -1> +position=< 20565, 20611> velocity=<-2, -2> +position=< 30797, 30838> velocity=<-3, -3> +position=< 41080, 30831> velocity=<-4, -3> +position=< 20597, -10076> velocity=<-2, 1> +position=<-50995, 41065> velocity=< 5, -4> +position=<-40793, -10079> velocity=< 4, 1> +position=< 10378, -30531> velocity=<-1, 3> +position=< 30848, 10375> velocity=<-3, -1> +position=<-10076, -40758> velocity=< 1, 4> +position=<-40736, -30530> velocity=< 4, 3> +position=<-40776, -20301> velocity=< 4, 2> +position=<-10068, 41056> velocity=< 1, -4> +position=<-30529, 10379> velocity=< 3, -1> +position=<-30570, 10377> velocity=< 3, -1> +position=<-20295, -10075> velocity=< 2, 1> +position=< 51247, -40760> velocity=<-5, 4> +position=<-40773, -40755> velocity=< 4, 4> +position=< 30809, -50982> velocity=<-3, 5> +position=<-30551, 51283> velocity=< 3, -5> +position=<-10116, 10376> velocity=< 1, -1> +position=<-10115, -40751> velocity=< 1, 4> +position=<-20311, -20298> velocity=< 2, 2> +position=<-10098, 10375> velocity=< 1, -1> +position=< 30849, -50978> velocity=<-3, 5> diff --git a/day10p1/main.c b/day10p1/main.c new file mode 100644 index 0000000..cafc522 --- /dev/null +++ b/day10p1/main.c @@ -0,0 +1,157 @@ +#include +#include + +#include + +struct light { + int x; + int y; + int velx; + int vely; +}; + +struct rect { + int x; + int y; + int w; + int h; +}; + +struct light *parse_lights(const char *fname, unsigned *len) +{ + FILE *f = fopen(fname, "r"); + if (!f) { + perror(fname); + return NULL; + } + + GArray *array = g_array_new(FALSE, TRUE, sizeof(struct light)); + + char *line = NULL; + size_t line_len = 0; + while (getline(&line, &line_len, f) != -1) { + struct light light; + sscanf(line, "position=<%d, %d> velocity=<%d, %d>", + &light.x, &light.y, &light.velx, &light.vely); + g_array_append_val(array, light); + } + + *len = array->len; + + free(line); + fclose(f); + return (struct light *)g_array_free(array, FALSE); +} + +struct rect get_frame(struct light *lights, const unsigned lights_len) +{ + struct rect res = {0}; + + if (!lights_len) return res; + + res.x = lights->x; + res.y = lights->y; + int xmax = res.x; + int ymax = res.y; + + for (struct light *light = lights + 1; light < lights + lights_len; light++) { + if (light->x < res.x) res.x = light->x; + if (light->y < res.y) res.y = light->y; + if (light->x > xmax) xmax = light->x; + if (light->y > ymax) ymax = light->y; + } + + res.w = xmax - res.x + 1; + res.h = ymax - res.y + 1; + + return res; +} + +void advance_frame(struct light *lights, const unsigned lights_len) +{ + for (struct light *light = lights; light < lights + lights_len; light++) { + light->x += light->velx; + light->y += light->vely; + } +} + +int check_limit(const struct rect limit, struct light *lights, + const unsigned lights_len) +{ + for (struct light *light = lights; light < lights + lights_len; light++) { + if (light->x < limit.x) return 1; + if (light->y < limit.y) return 1; + if (light->x > limit.x + limit.w) return 1; + if (light->y > limit.y + limit.h) return 1; + } + return 0; +} + +int make_bitmap(const char *fname, const struct rect frame, + struct light *lights, const unsigned lights_len) +{ + int bitmap_width = frame.w / CHAR_BIT; + if (frame.w % CHAR_BIT != 0) bitmap_width++; + + size_t bitmap_size = frame.h * bitmap_width; + char (*bitmap)[bitmap_width] = calloc(1, bitmap_size); + if (!bitmap) { + perror("calloc"); + return 1; + } + + for (struct light *light = lights; light < lights + lights_len; light++) { + if (light->x < frame.x) continue; + if (light->y < frame.y) continue; + if (light->x > frame.x + frame.w) continue; + if (light->y > frame.y + frame.h) continue; + + int bitmap_x = light->x - frame.x; + int bitmap_y = light->y - frame.y; + + bitmap[bitmap_y][bitmap_x / CHAR_BIT] |= 1 << (CHAR_BIT - bitmap_x % CHAR_BIT - 1); + } + + FILE *f = fopen(fname, "w"); + if (!f) { + perror(fname); + free(bitmap); + return 1; + } + + fprintf(f, "P4\n%d %d\n", frame.w, frame.h); + fwrite(bitmap, bitmap_size, 1, f); + + free(bitmap); + fclose(f); + return 0; +} + +#define REASONABLE_HEIGHT 100 + +int main() +{ + unsigned lights_len; + struct light *lights = parse_lights("input", &lights_len); + if (!lights) return 1; + + struct rect frame; + + while (1) { + frame = get_frame(lights, lights_len); + if (frame.h <= REASONABLE_HEIGHT) break; + advance_frame(lights, lights_len); + } + + GString *fname = g_string_new(NULL); + unsigned frame_id = 0; + while (check_limit(frame, lights, lights_len) == 0) { + g_string_printf(fname, "out/frame%03d.pbm", frame_id++); + if (make_bitmap(fname->str, frame, lights, lights_len)) break; + advance_frame(lights, lights_len); + } + + g_string_free(fname, TRUE); + free(lights); + return 0; +} diff --git a/day10p1/makegif.sh b/day10p1/makegif.sh new file mode 100755 index 0000000..099ba48 --- /dev/null +++ b/day10p1/makegif.sh @@ -0,0 +1,2 @@ +#!/bin/sh +convert -resize 200% -filter point -delay 20 -loop 0 out/*.pbm out.gif diff --git a/day10p1/out.gif b/day10p1/out.gif new file mode 100644 index 0000000..8a70a83 Binary files /dev/null and b/day10p1/out.gif differ diff --git a/day10p1/out/.gitkeep b/day10p1/out/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/day9p1/Makefile b/day9p1/Makefile index 5ad0e7f..042aa30 100644 --- a/day9p1/Makefile +++ b/day9p1/Makefile @@ -1,9 +1,5 @@ CFLAGS := -Wall -Wextra -std=c17 -D_GNU_SOURCE -#LIBS := glib-2.0 -#CFLAGS += $(shell pkg-config --cflags $(LIBS)) -#LDLIBS := $(shell pkg-config --libs $(LIBS)) - .PHONY: all all: main diff --git a/day9p2/Makefile b/day9p2/Makefile index 5ad0e7f..042aa30 100644 --- a/day9p2/Makefile +++ b/day9p2/Makefile @@ -1,9 +1,5 @@ CFLAGS := -Wall -Wextra -std=c17 -D_GNU_SOURCE -#LIBS := glib-2.0 -#CFLAGS += $(shell pkg-config --cflags $(LIBS)) -#LDLIBS := $(shell pkg-config --libs $(LIBS)) - .PHONY: all all: main