summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-12-21 04:29:50 -0500
committerJune McEnroe <june@causal.agency>2020-11-22 00:14:25 -0500
commitc6fe3e2f18fe4a57cfa817d0aafb593a2378b215 (patch)
tree2c3633e8c8c5b229d7f32b4a53da945441c684a8
parentSolve day 18 part 1 (diff)
downloadaoc-c6fe3e2f18fe4a57cfa817d0aafb593a2378b215.tar.gz
aoc-c6fe3e2f18fe4a57cfa817d0aafb593a2378b215.zip
Solve day 18 part 2
-rw-r--r--2018/day18.c56
1 files changed, 49 insertions, 7 deletions
diff --git a/2018/day18.c b/2018/day18.c
index 686ae70..6bc55f7 100644
--- a/2018/day18.c
+++ b/2018/day18.c
@@ -1,5 +1,7 @@
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 typedef unsigned uint;
 
@@ -52,6 +54,42 @@ static struct Map step(struct Map prev) {
 	return next;
 }
 
+static uint value(struct Map map) {
+	uint wood = 0, lumber = 0;
+	for (uint y = 0; y < 50; ++y) {
+		for (uint x = 0; x < 50; ++x) {
+			if (map.acres[y][x] == '|') wood++;
+			if (map.acres[y][x] == '#') lumber++;
+		}
+	}
+	return wood * lumber;
+}
+
+enum { RingLen = 64 };
+static_assert(!(RingLen & (RingLen - 1)), "power of two");
+
+static struct {
+	struct Map buf[RingLen];
+	uint end;
+} ring;
+
+static struct Map *ringMap(uint i) {
+	return &ring.buf[(ring.end + i) & (RingLen - 1)];
+}
+
+static void ringPush(struct Map map) {
+	*ringMap(0) = map;
+	ring.end++;
+}
+
+static uint ringFind(struct Map map) {
+	uint i;
+	for (i = 0; i < RingLen; ++i) {
+		if (!memcmp(ringMap(i), &map, sizeof(map))) break;
+	}
+	return i;
+}
+
 int main(void) {
 	struct Map map;
 	for (uint y = 0; y < 50; ++y) {
@@ -61,12 +99,16 @@ int main(void) {
 	for (uint i = 0; i < 10; ++i) {
 		map = step(map);
 	}
-	uint wood = 0, lumber = 0;
-	for (uint y = 0; y < 50; ++y) {
-		for (uint x = 0; x < 50; ++x) {
-			if (map.acres[y][x] == '|') wood++;
-			if (map.acres[y][x] == '#') lumber++;
-		}
+	printf("%u\n", value(map));
+
+	uint i;
+	for (i = 11; i < 1000000000; ++i) {
+		map = step(map);
+		if (ringFind(map) < RingLen) break;
+		ringPush(map);
 	}
-	printf("%u\n", wood * lumber);
+	uint skip = ringFind(map);
+	uint period = RingLen - skip;
+	uint final = skip + (1000000000 - i) % period;
+	printf("%u\n", value(*ringMap(final)));
 }
l only show pre-rename commits. I consider this a reasonable trade-off since the "Back" button still works and the log matches the path displayed in the top bar. Since following renames requires running diff on every commit we consider, I've added a knob to the configuration file to globally enable/disable this feature. Note that we may consider a large number of commits the revision walking machinery no longer performs any path limitation so we have to examine every commit until we find a page full of commits that affect the target path or something related to it. Suggested-by: René Neumann <necoro@necoro.eu> Signed-off-by: John Keeping <john@keeping.me.uk> 2015-08-12shared: make cgit_diff_tree_cb publicJohn Keeping This will allow us to use this nice wrapper function elsewhere, avoiding dealing with the diff queue when we only need to inspect a filepair. Signed-off-by: John Keeping <john@keeping.me.uk> 2015-08-12t0110: Chain together using &&Jason A. Donenfeld 2015-08-12about: always ensure page has a trailing slashJason A. Donenfeld Otherwise we can't easily embed links to other /about/ pages. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> 2015-08-12filters: apply HTML escapingLazaros Koromilas http://www.w3.org/International/questions/qa-escapes#use 2015-08-12git: update to v2.5.0Christian Hesse Update to git version v2.5.0. * Upstream commit 5455ee0573a22bb793a7083d593ae1ace909cd4c (Merge branch 'bc/object-id') changed API: for_each_ref() callback functions were taught to name the objects not with "unsigned char sha1[20]" but with "struct object_id". * Upstream commit dcf692625ac569fefbe52269061230f4fde10e47 (path.c: make get_pathname() call sites return const char *) Signed-off-by: Christian Hesse <mail@eworm.de> 2015-08-12Fix processing of repo.hide and repo.ignoreDaniel Reichelt