commit 537e8e9ebfd79fdc0cba37596e75c57c12012403
parent a15787646c702afe8d9f9808d2c943992e1f7c36
Author: bsandro <email@bsandro.tech>
Date: Fri, 26 Dec 2025 01:52:27 +0200
day03 p2
Diffstat:
| M | day03.c | | | 76 | +++++++++++++++++++++++++++++++++++++++++----------------------------------- |
1 file changed, 41 insertions(+), 35 deletions(-)
diff --git a/day03.c b/day03.c
@@ -15,6 +15,7 @@ typedef struct {
typedef struct {
Vec2 p1, p2;
+ int len;
} Line;
typedef struct {
@@ -37,45 +38,32 @@ Line normalize(Line l) {
return l;
}
-bool get_intersection(Line * restrict l1, Line * restrict l2, Vec2 * restrict p) {
- (void)p;
- if (l1->p1.x==l1->p2.x) { // l1 vertical
- if (l2->p1.x==l2->p2.x) return false; // l2 is vertical too
- if (l1->p1.x>l2->p1.x && l1->p2.x<l2->p2.x &&
- l2->p1.y>l1->p1.y && l2->p2.y<l1->p2.y) {
- p->x = l1->p1.x;
- p->y = l2->p1.y;
+bool get_intersection(Line * restrict l1, Line * restrict l2, Vec2 * restrict p, int64_t * restrict steps) {
+ (void)steps;
+ Line ln1 = normalize(*l1);
+ Line ln2 = normalize(*l2);
+ if (ln1.p1.x==ln1.p2.x) { // ln1 vertical
+ if (ln2.p1.x==ln2.p2.x) return false; // ln2 is vertical too
+ if (ln1.p1.x>ln2.p1.x && ln1.p2.x<ln2.p2.x &&
+ ln2.p1.y>ln1.p1.y && ln2.p2.y<ln1.p2.y) {
+ p->x = ln1.p1.x;
+ p->y = ln2.p1.y;
+ *steps = abs(l1->p1.y - p->y) + abs(l2->p1.x - p->x);
return true;
}
- } else if (l1->p1.y==l1->p2.y) { // l1 horizontal
- if (l2->p1.y==l2->p2.y) return false; // l2 is horizontal too
- if (l2->p1.x>l1->p1.x && l2->p2.x<l1->p2.x &&
- l1->p1.y>l2->p1.y && l1->p2.y<l2->p2.y) {
- p->x = l2->p1.x;
- p->y = l1->p1.y;
+ } else if (ln1.p1.y==ln1.p2.y) { // ln1 horizontal
+ if (ln2.p1.y==ln2.p2.y) return false; // ln2 is horizontal too
+ if (ln2.p1.x>ln1.p1.x && ln2.p2.x<ln1.p2.x &&
+ ln1.p1.y>ln2.p1.y && ln1.p2.y<ln2.p2.y) {
+ p->x = ln2.p1.x;
+ p->y = ln1.p1.y;
+ *steps = abs(l1->p1.x - p->x) + abs(l2->p1.y - p->y);
return true;
}
}
return false;
}
-int64_t calc_p1(Wire *w1, Wire *w2) {
- int64_t r = -1;
- for (int i=0;i<w1->len;++i) {
- for (int j=0;j<w2->len;++j) {
- Vec2 p = {0};
- if (get_intersection(&w1->lines[i], &w2->lines[j], &p)) {
- int dist = abs(p.x)+abs(p.y);
- printf("intersection: {%d,%d}, dist: %d\n", p.x, p.y, dist);
- if (r==-1) r = dist;
- else if (dist<r) r = dist;
- }
- }
- }
- return r;
-}
-// 369 too low
-// 1167 too high
int main(void) {
Buffer buf = {0};
Vec2 prev = {0};
@@ -92,7 +80,7 @@ int main(void) {
else if (dir=='D') p.y -= dist;
else if (dir=='L') p.x -= dist;
else if (dir=='R') p.x += dist;
- cw->lines[cw->len++] = normalize((Line){ .p1=prev, .p2=p });
+ cw->lines[cw->len++] = (Line){ .p1=prev, .p2=p, .len=dist };
prev = p;
if (c=='\n') {
cw = &w2;
@@ -105,9 +93,27 @@ int main(void) {
buf.data[buf.len++] = c-'0';
}
}
- int64_t p1 = calc_p1(&w1, &w2);
+ int64_t p1 = 0;
int64_t p2 = 0;
- printf("p1: %"PRIi64"\np2: %"PRIi64"\n", p1, p2);
-
+
+ int64_t steps1 = 0;
+ for (int i=0;i<w1.len;++i) {
+ int64_t steps2 = 0;
+ for (int j=0;j<w2.len;++j) {
+ Vec2 p = {0};
+ int64_t steps = 0;
+ if (get_intersection(&w1.lines[i], &w2.lines[j], &p, &steps)) {
+ int dist = abs(p.x)+abs(p.y);
+ if (p1==0) p1 = dist;
+ else if (dist<p1) p1 = dist;
+ int64_t steps_sum = steps1+steps2+steps;
+ if (p2==0) p2 = steps_sum;
+ else if (steps_sum<p2) p2 = steps_sum;
+ }
+ steps2 += w2.lines[j].len;
+ }
+ steps1 += w1.lines[i].len;
+ }
+ printf("p1: %"PRIi64"\np2: %"PRIi64"\n", p1, p2);
return 0;
}