advent2025

Advent of Code 2025 Solutions
git clone git://bsandro.tech/advent2025
Log | Files | Refs | LICENSE

commit f699d90224cd32750520247f2de2979d6272f9b2
parent 53314f3614e0e12b364ae7e48a061255fe2f3d98
Author: bsandro <email@bsandro.tech>
Date:   Wed, 10 Dec 2025 01:49:52 +0200

day09 cleanup

Diffstat:
Mday09.c | 79++++---------------------------------------------------------------------------
1 file changed, 4 insertions(+), 75 deletions(-)

diff --git a/day09.c b/day09.c @@ -43,66 +43,15 @@ static bool square_has_points(int points_l, Point points[points_l], Point p1, Po int maxy = MAX(p1.y, p2.y); for (int i=0;i<points_l;++i) { Point p = points[i]; - if ((p.x==p1.x&&p.y==p1.y)||(p.x==p2.x&&p.y==p2.y)) continue; // skip self - if (p.x<maxx&&p.x>minx&&p.y<maxy&&p.y>miny) { - //if (p1.x==9&&p1.y==5&&p2.x==2&&p2.y==3) printf("square_has_points failed\n"); - return true; - } - } - //if (p1.x==9&&p1.y==5&&p2.x==2&&p2.y==3) printf("square_has_points succeeded\n"); - - return false; -} - -static bool __attribute__((unused)) lines_have_point(int lines_l, Line lines[lines_l], Point p) { - for (int i=0;i<lines_l;++i) { - Point p1 = lines[i].p1; - Point p2 = lines[i].p2; - if (p1.x==p2.x&&p1.x==p.x) { // vertical lines only - int miny = MIN(p1.y, p2.y); - int maxy = MAX(p1.y, p2.y); - if (p.y>=miny&&p.y<=maxy) return true; - } - } - return false; -} - -static bool __attribute__((unused)) lines_have_point1(int lines_l, Line lines[lines_l], Point p) { - for (int i=0;i<lines_l;++i) { - Point p1 = lines[i].p1; - Point p2 = lines[i].p2; - int miny = MIN(p1.y, p2.y); - int maxy = MAX(p1.y, p2.y); - int minx = MIN(p1.x, p2.x); - int maxx = MAX(p1.x, p2.x); - if (p1.x==p2.x&&p1.x==p.x) { - if (p.y>=miny&&p.y<=maxy) return true; - } - if (p1.y==p2.y&&p1.y==p.y) { - if (p.x>=minx&&p.x<=maxx) return true; - } + if ((p.x==p1.x&&p.y==p1.y)||(p.x==p2.x&&p.y==p2.y)) continue; + if (p.x<maxx&&p.x>minx&&p.y<maxy&&p.y>miny) return true; } return false; } -static bool point_is_inside(int lines_l, Line lines[lines_l], int maxx, Point p) { - //printf("check point %d:%d...\n", p.x,p.y); - int num = 0; - for (;p.x<=maxx+1;p.x++) { - if (lines_have_point(lines_l, lines, p)) { - num++; - //printf("%d:%d is on lines\n", p.x,p.y); - } - } - //printf("%d:%d intersections: %d (%d)\n",p.x,p.y,num,num%2==1); - return num%2==1; -} - -static bool square_is_valid(int lines_l, Line lines[lines_l], int maxx, Point p1, Point p2) { +static bool square_is_valid(int lines_l, Line lines[lines_l], Point p1, Point p2) { Point p3 = (Point){ .x=p1.x, .y=p2.y }; Point p4 = (Point){ .x=p2.x, .y=p1.y }; - (void)maxx; - (void)point_is_inside; int x1 = MIN(p1.x, p2.x); int x2 = MAX(p1.x, p2.x); int y1 = MIN(p1.y, p2.y); @@ -125,28 +74,16 @@ static bool square_is_valid(int lines_l, Line lines[lines_l], int maxx, Point p1 (p2.x==lx2&&p2.y==ly2) ) continue; if (lx1<=x1&&lx2>=x2&&ly1>=y1&&ly2<=y2) { - //printf("square_is_valid %d,%d->%d,%d check 1 failed: lx1:%d,ly1:%d,lx2:%d,ly2:%d; line %d:%d->%d:%d\n",p1.x,p1.y,p2.x,p2.y,lx1,ly1,lx1,ly2,l.p1.x,l.p1.y,l.p2.x,l.p2.y); return false; } if (ly1<=y1&&ly2>=y2&&lx1>=x1&&lx2<=x2) { - //printf("square_is_valid %d,%d->%d,%d check 2 failed: lx1:%d,ly1:%d,lx2:%d,ly2:%d; line %d:%d->%d:%d\n",p1.x,p1.y,p2.x,p2.y,lx1,ly1,lx1,ly2,l.p1.x,l.p1.y,l.p2.x,l.p2.y); return false; } } - //return (point_is_inside(lines_l, lines, maxx, p3) && point_is_inside(lines_l, lines, maxx, p4)); - //if (p1.x==9&&p1.y==5&&p2.x==2&&p2.y==3) printf("square_is_valid succeeded\n"); return true; } -static int get_max_x(int points_l, Point points[points_l]) { - int maxx = 0; - for (int i=0;i<points_l;++i) { - if (points[i].x>maxx) maxx=points[i].x; - } - return maxx; -} - int main(void) { int64_t part1 = 0; int64_t part2 = 0; @@ -168,9 +105,6 @@ int main(void) { return 1; } } - //printf("%d points\n", points_l); - int maxx = get_max_x(points_l, points); - //printf("maxx: %d\n", maxx); int lines_max = points_l; Line lines[lines_max]; int lines_l = 0; @@ -188,19 +122,14 @@ int main(void) { if (i==j) continue; int64_t sq = square(points[i], points[j]); squares[squares_l++] = sq; - //printf("----------------------\n"); - //if (points[i].x==9&&points[i].y==5&&points[j].x==2&&points[j].y==3) printf("checking...\n"); if (!square_has_points(points_l, points, points[i], points[j]) && - square_is_valid(lines_l, lines, maxx, points[i], points[j])) { + square_is_valid(lines_l, lines, points[i], points[j])) { squares2[squares2_l++] = sq; - //printf("square %2d,%2d->%2d,%2d valid %lu\n", points[i].x,points[i].y, points[j].x,points[j].y,sq); } } } - //printf("%d/%d squares\n", squares2_l, squares_max); qsort(squares, squares_l, sizeof(squares[0]), cmp_int64); qsort(squares2, squares2_l, sizeof(squares2[0]), cmp_int64); - //for (int i=0;i<squares2_l;++i) printf("%"PRIu64"\n", squares2[i]); part1 = squares[0]; part2 = squares2[0]; free(points);