summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2019-12-03 01:03:42 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:26 -0500
commitdee5e979aa5eaf38d360c3fc801ea16f639db659 (patch)
treebe732ebc230857bda5e7857cbac14a1809bb6255
parentSolve day 2 part 2 (diff)
downloadaoc-dee5e979aa5eaf38d360c3fc801ea16f639db659.tar.gz
aoc-dee5e979aa5eaf38d360c3fc801ea16f639db659.zip
Solve day 3 part 1
Diffstat (limited to '')
-rw-r--r--2019/day03.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/2019/day03.c b/2019/day03.c
new file mode 100644
index 0000000..5d45bc0
--- /dev/null
+++ b/2019/day03.c
@@ -0,0 +1,76 @@
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+struct Point {
+	int x, y;
+};
+
+struct Line {
+	struct Point a, b;
+};
+
+static void normalize(struct Line *l) {
+	if (l->a.x > l->b.x || l->a.y > l->b.y) {
+		struct Point p = l->a;
+		l->a = l->b;
+		l->b = p;
+	}
+}
+
+static size_t parse(struct Line *lines) {
+	char dir;
+	int dist;
+	size_t len = 0;
+	struct Point point = { 0, 0 };
+	while (EOF != scanf("%c%d,", &dir, &dist)) {
+		if (dir == '\n') break;
+		lines[len].a = point;
+		switch (dir) {
+			break; case 'U': point.y += dist;
+			break; case 'D': point.y -= dist;
+			break; case 'L': point.x -= dist;
+			break; case 'R': point.x += dist;
+			break; default: abort();
+		}
+		lines[len].b = point;
+		normalize(&lines[len++]);
+	}
+	return len;
+}
+
+static int intersect(struct Point *p, struct Line v, struct Line h) {
+	if (v.a.x != v.b.x) {
+		struct Line l = v;
+		v = h;
+		h = l;
+	}
+	if (v.a.y == v.b.y) return 0;
+	if (h.a.x == h.b.x) return 0;
+	if (h.a.y < v.a.y) return 0;
+	if (h.a.y > v.b.y) return 0;
+	if (h.a.x > v.a.x) return 0;
+	if (h.b.x < v.a.x) return 0;
+	p->x = v.a.x;
+	p->y = h.a.y;
+	return 1;
+}
+
+int main(void) {
+	struct Line aLines[512];
+	struct Line bLines[512];
+	size_t aLen = parse(aLines);
+	size_t bLen = parse(bLines);
+
+	int min = INT_MAX;
+	for (size_t a = 0; a < aLen; ++a) {
+		for (size_t b = 0; b < bLen; ++b) {
+			struct Point p;
+			if (!intersect(&p, aLines[a], bLines[b])) continue;
+			if (!p.x && !p.y) continue;
+			int dist = abs(p.x) + abs(p.y);
+			if (dist < min) min = dist;
+		}
+	}
+	printf("%d\n", min);
+}
the git installed by MacPorts. 2020-09-12Move /opt/local behind /usr againJune McEnroe The reason I did this with pkgsrc was because I actually don't want the man(1) from mandoc, since it won't follow MANSECT. Same applies to MacPorts. I wish I could disable its man(1) with a variant or whatever. 2020-09-12Enable toc in cgit renderings of man pagesJune McEnroe But keep it disabled for READMEs since they always use non-standard sections and the TOC is just distracting there, I think. Also add the style so its h1 is the same size as the ones inside sections... 2020-09-11Install mandoc on macOSJune McEnroe 2020-09-11Rewrite install script yet againJune McEnroe 2020-09-11Remove NetBSD from install scriptJune McEnroe I never use it. 2020-09-11Use MacPorts rather than pkgsrcJune McEnroe My system is probably such a mess now... 2020-09-11Add debian VM name to sshJune McEnroe 2020-09-11Add influencer tweetJune McEnroe 2020-09-10Add The Kingdom of GodsJune McEnroe Reading has really slowed down :( 2020-09-07Add SunglassesJune McEnroe An IRC find. 2020-09-06Add Between the BreathsJune McEnroe One of those good songs from a soundtrack of a film that probably isn't? The summary sounds a lot more interesting than the title implies, at least. 2020-09-04Open /dev/tty in nudgeJune McEnroe This makes it work even when it's run connected to a pipe, i.e. as the notify command of catgirl... 2020-09-04Add nudgeJune McEnroe 2020-09-03Build fbclock with -lzJune McEnroe I guess this got lost somewhere, long ago... 2020-08-29Add tweets from retweetsJune McEnroe