summary refs log tree commit diff homepage
path: root/2019/day10.c
diff options
context:
space:
mode:
Diffstat (limited to '2019/day10.c')
-rw-r--r--2019/day10.c42
1 files changed, 40 insertions, 2 deletions
diff --git a/2019/day10.c b/2019/day10.c
index dbc2352..04743b4 100644
--- a/2019/day10.c
+++ b/2019/day10.c
@@ -1,3 +1,4 @@
+#include <math.h>
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -25,6 +26,21 @@ static struct Point reduce(struct Point p) {
 	return p;
 }
 
+static double angle(struct Point p) {
+	double theta = atan2(p.x, -p.y);
+	return (theta < 0.0 ? theta + 2.0 * M_PI : theta);
+}
+
+static int compar(const void *_a, const void *_b) {
+	struct Point a = *(const struct Point *)_a;
+	struct Point b = *(const struct Point *)_b;
+	if (angle(a) == angle(b)) {
+		return copysign(1.0, hypot(a.x, a.y) - hypot(b.x, b.y));
+	} else {
+		return copysign(1.0, angle(a) - angle(b));
+	}
+}
+
 int main(void) {
 	struct Point ps[2048];
 	size_t len = 0;
@@ -38,7 +54,7 @@ int main(void) {
 		}
 	}
 
-	size_t max = 0;
+	size_t m, max = 0;
 	for (size_t i = 0; i < len; ++i) {
 		struct Point ds[2048];
 		size_t dlen = 0;
@@ -51,7 +67,29 @@ int main(void) {
 			}
 			if (d == dlen) ds[dlen++] = a;
 		}
-		if (dlen > max) max = dlen;
+		if (dlen > max) {
+			max = dlen;
+			m = i;
+		}
 	}
 	printf("%zu\n", max);
+
+	struct Point o = ps[m];
+	ps[m] = ps[--len];
+	for (size_t i = 0; i < len; ++i) {
+		ps[i] = sub(ps[i], o);
+	}
+	qsort(ps, len, sizeof(ps[0]), compar);
+	for (int n = 0;;) {
+		for (size_t i = 0; i < len; ++i) {
+			if (eq(ps[i], Point(0, 0))) continue;
+			if (++n == 200) {
+				printf("%d\n", 100 * (o.x + ps[i].x) + (o.y + ps[i].y));
+				return 0;
+			}
+			double theta = angle(ps[i]);
+			ps[i] = Point(0, 0);
+			while (i + 1 < len && angle(ps[i + 1]) == theta) i++;
+		}
+	}
 }
_print_error_page() instead of html_status()John Keeping 2015-08-14plain: use cgit_print_error_page() instead of html_status()John Keeping 2015-08-14clone: use cgit_print_error_page() instead of html_status()John Keeping 2015-08-14cgit: use cgit_print_error_page() where appropriateJohn Keeping 2015-08-14ui-shared: add cgit_print_error_page() functionJohn Keeping 2015-08-14ui-patch: make sure to send http headersChristian Hesse 2015-08-13Makefile: make "git/config.mak.uname" inclusion optionalJohn Keeping 2015-08-13ui-shared: show full date in tooltip if longer ago than max_relativeJohn Keeping 2015-08-13ui-shared: use common function in print_rel_date()John Keeping 2015-08-13ui-shared: extract date formatting to a functionJohn Keeping 2015-08-13filter: don't use dlsym unnecessarilyJohn Keeping 2015-08-13ui-tree: use "sane" isgraph()John Keeping 2015-08-13cgit.h: move stdbool.h from ui-shared.hJohn Keeping 2015-08-13cache.c: fix header orderJohn Keeping 2015-08-13configfile.c: don't include system headers directlyJohn Keeping 2015-08-13Remove redundant includesJohn Keeping 2015-08-13Makefile: include Git's config.mak.unameJohn Keeping 2015-08-13tests: allow shell to be overriddenJohn Keeping 2015-08-13redirect: cleanlinessJason A. Donenfeld 2015-08-13redirect: be more careful for different cgi setupsJason A. Donenfeld 2015-08-12ui-log: fix double countingJohn Keeping 2015-08-12log: allow users to follow a fileJohn Keeping 2015-08-12shared: make cgit_diff_tree_cb publicJohn Keeping 2015-08-12t0110: Chain together using &&Jason A. Donenfeld 2015-08-12about: always ensure page has a trailing slashJason A. Donenfeld 2015-08-12filters: apply HTML escapingLazaros Koromilas 2015-08-12git: update to v2.5.0Christian Hesse 2015-08-12Fix processing of repo.hide and repo.ignoreDaniel Reichelt